Solved

Pointers and Linked Lists

Posted on 1998-07-03
2
183 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Which Linux flavors will this run on? 6 116
FMX TCameraComponent Problem 2 106
c++ aadding data to a list box vs2005 mfc project 7 40
using interface in TLB 3 31
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…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

751 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