link list

This is a discussion on link list within the C++ Programming forums, part of the General Programming Boards category; I need to create a circular linklist so that the last node in the list points at the head instead ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    17

    Exclamation link list

    I need to create a circular linklist so that the last node in the list points at the head instead of null. But I am a nubee and really have a hard time with the creation of link list. I tried to create a link list last week that ended with null but I could not get it to function properly so now I have a program to do that uses a circular link list to pull 10 names out of a txt file and put them into order alphabetically so im trying to convert my program from last week to help me with the circular list program. I probally should start from scratch but I thought I would ask someone on here before I started over. So if you know why this is not working properly please throw me a hint. please disreguard the cout statements I was using them just to trouble shoot.

    Thanks

    Fastlane 29

    Code:
    #include <stdio.h>
    #include <iostream.h>
    #include <fstream.h>
    #include <string.h>
    
    const int ItemLen = 12;
    
    //Node definition
    struct ListNode
    {
      char name[ItemLen];
      char lastname[ItemLen];
      ListNode *link;
     };
    
     typedef ListNode* ListNodePtr;
    
     ListNodePtr Head;
    
    int main(int argc, char *argv[])
    {
    
    
      char filename[ItemLen];
      char filelastname[ItemLen];
      char transcode[2];
      char transname[ItemLen];
      char translastname[ItemLen];
      char changedname[ItemLen],changedlastname[ItemLen];
      int i,j;
      ListNodePtr p,q,temp;
      Head= new ListNode;
    
      q=new ListNode;
      temp=new ListNode;
    
      ifstream inFile;
      ofstream output;
      ofstream out_stream;
    
      Head=NULL;
    
    inFile.open("a:GOLFNAME.txt",ios::in);
    
    
         inFile>> filename>> filelastname;
         cout<<filename<<" ? "<<filelastname<<endl;
         while(!inFile.eof())
         {
    
           p=new ListNode;
           strcpy(p->name,filename);
           strcpy(p->lastname,filelastname);
             if(Head==NULL)
                {
    
                  Head=p;
                  p->link=NULL;
               }
             else
             {
    
                q=Head;
                if(q->link==NULL)
                {
                  p->link=q;
                  Head=p;
                }
                else
                {
                  q=Head;
                  while((strcmp(p->name , q->name)>0)&& (strcmp(p->lastname , q->lastname)!=0))
                  {
                     cout<< "here I am";
                    temp=q;
                    q=q->link;
                  }
                  p->link=q;
                  temp->link=p;
               }}
    
    
            inFile>>filename>>filelastname;
            cout<<"2nd"<<filename<<" ? "<<filelastname<<endl;
            cout<<"plink= "<<p->name<<" "<<p->lastname<<endl;
          }
    
    inFile.close();
    
    //out_stream.open("a:GOLFNAME.alp");
    
           p=Head;
      while(j>10)
       {
         //out_stream<< p->name << " " << p->lastname << endl;
    
         cout<<"print "<<endl;
         cout<< p->name << " " << p->lastname << endl;
         p= p->link;
    
         j++;
         cin>>i;
      }
       cout<<"press any number it is the end of program"<<endl;
       cin>>i;
       return 0;
       }
    Last edited by fastlane29; 09-26-2002 at 08:22 PM.

  2. #2
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    Use Standard C++ if possible. That means do not use .h in your header files.

    Have you compiled your posted code. It may help to post your possible errors here as well.

    mr. c

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,641
    Use a function! This will make your life easier.

    Code:
    //...This function requires no preparation.
    
    AttachNew(ListNode *head, ListNode *node) {
    node = new ListNode;
    node->link = NULL;
    if(head == NULL) 
     head = node;
    else {
      ListNode *p = head;
      while(p->link != NULL) {
       p = p->link;
      }
      p->link = node;
     }
    }

    Using this type of list is now easy:

    Code:
    char name[100];
    
    ListNode 
     *head = NULL, //..this flags the function... 
     *node;
    
    do{
          
          cout << "enter a name or type 'bye' to exit" << endl; 
          cin >> name;
          if(strcmp(name, "bye"))
            break;
          else {  
          AttachNew(head, node);
          strcpy(node->name, name);     
         }while(1);

    You can easily restructure this to turn out circular lists, too.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    to have a singly linked circular list of three nodes, p, q , and r, you would do something like this:

    struct Node
    {
    //data members
    Node * link;
    };

    Node * p;
    Node * q;
    Node * r;

    p->link = q;
    q->link = r;
    r->link = p;

    Note now how p and r are linked together to create the circle. There must be one such link in your circular list but I can't find it in your code. Once you have that, you then need some way to tell where to start and where to stop searching the list since the usually stopping point if condition not met earlier, is the NULL pointer at the end of a straight list, but it isn't there anymore.

    It looks as if you are trying to list names alphabetically by one name, and then subsort alphabetically by another name. If so, then I think you need many more cases. It will probably get pretty messy by the time you're done setting up such a sorting routine, so if you don't have to do it, don't.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,641
    Not exactly. A circular list starts at the head, and the last node points to the head. As long as you keep track of this head, you will always know when to stop. Also, you can easily "rotate" the list thus allowing the head to be any node you desire. This is actually easier than it sounds, all that is needed is a pointer swap.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 03:09 AM
  2. Link List Insert prob
    By Bebs in forum C Programming
    Replies: 8
    Last Post: 12-03-2008, 09:28 PM
  3. reading data from a file - link list
    By peter_hii in forum C++ Programming
    Replies: 7
    Last Post: 10-25-2006, 09:11 AM
  4. compiler build error
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 09:16 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21