Solved

Pointers and Linked Lists

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
java. non-English characters encoding problem. intellij idea 3 90
eclipse formatting 6 106
Embarcadero C++ builder XE10.1 Berlin TRegistry declaration 1 40
Outlook 13 43
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall‚Ķ
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

830 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