Thread: link list

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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, 04:09 AM
  2. Link List Insert prob
    By Bebs in forum C Programming
    Replies: 8
    Last Post: 12-03-2008, 10: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, 10:16 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM