Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Pointers and Linked Lists

Posted on 1998-07-03
2
Medium Priority
?
187 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 300 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

926 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