Solved

Pointers and Linked Lists

Posted on 1998-07-03
2
179 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

744 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

12 Experts available now in Live!

Get 1:1 Help Now