Solved

Pointers and Linked Lists

Posted on 1998-07-03
2
180 Views
Last Modified: 2013-12-14
(Beware-This is a long question)
I am trying to accomplish something that I have already done with a 2 dimensional array.  I am basically drawing some lines on the screen in a MFC application.  I have recently found that I made need more than 2 dimensions and the actual size of the arrays will be unknown until run time.  Therefore, I have decided using a linked list with pointers is the best method.  Unfortunately, I am not very good with pointers.  The following declarations is what I am using;

  int first;               // used to tell me if I am at the first secondary or not

  struct tap_link                   // this structure is used to store info about each tap
  {
      tap_link *next_tap;                          // pointer to next tap
      tap_link *next_sec;                         // pointer to next secondary
      int tap_type;              // 0 if tap_link is a load or 1 if tap_link is a secondary or 2 if unknown
                  bool PRESEC_BLACK;        // true if previous section of secondary is entered
                  bool TAP_BLACK;              // true if tap info is entered
                 
         };

  class tap_list
  {
      private:

      public:
        tap_link *first_sec, *prev_sec, *first_tap, *prev_tap;
        tap_list()
        {
            first_sec = NULL;
        }
        void makenewsec();
        void makenewtap();
        void removeall();
  };

  void tap_list::makenewsec()                  //  function for adding new secondary
  {
      tap_link *newlink = new tap_link;      //  make a new link
      if (first_sec == NULL)                       //  first secondary
      {
        first_sec = newlink;
        first=1;                                  // if first = 1 then I am at the first secondary
      }
      else
      {
        prev_sec->next_sec = newlink;
        first=0;                            // if first != 1 then I am not at the first secondary
      }
      newlink->next_sec = NULL;
      prev_sec = newlink;
      first_tap = NULL;  .//  everytime a secondary is made a new tap will be connected to the new secondary
  }

  void tap_list::makenewtap()                  //  function for adding new tap
  {
      tap_link* newlink = new tap_link;      //  make a new link
      if (first_tap == NULL)
      {
        first_tap = newlink;
        if (first == 1)
            first_sec->next_tap = newlink;
        else
            prev_sec->next_tap = newlink;
      }
      else
        prev_tap->next_tap = newlink;
      newlink->next_tap = NULL;
      prev_tap = newlink;
      newlink->tap_type = 2;                        //  set tap type to unknown
      newlink->PRESEC_BLACK = FALSE;            //  previous secondary info is not known for new tap
      newlink->TAP_BLACK = FALSE;                  //  tap info is not known for new tap
  }



Some of the above information is not important.  The pointers are of importance to my question along with the 2 functions for making a new secondary and a new tap.  The 2 dimensional array had one dimension specifying the secondary and one for the taps on each secondary.  (Many taps on each secondary)
The program will always make a secondary and then some number of taps.  It will then make another secondary and some number of taps.  It will do this until the desired number of secondaries have been made.

My program works great if I have 1 secondary and any number of taps.  However, when I have more than one secondary the program crashes.  I believe I have a pointer error.  What I want is some info (code) on how to use to pointers in a linked list to make it perform as a 2 dimensional array would.  I must be able to keep up with the first secondary (there will be one) and the first tap of each secondary (there is one for each secondary).

Thanks for the help
0
Comment
Question by:scs1
2 Comments
 
LVL 2

Accepted Solution

by:
rayb earned 100 total points
ID: 1167085
I highly recommend scrapping your implementation of the linked list and adopting an STL container.  You'll save yourself much heartache now as well as later.  Look at the list, vector or queue for this particular application.
0
 
LVL 5

Expert Comment

by:yonat
ID: 1167086
I second that. To use the STL list class, you need to #include <list> . The class name is std::list.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now