Thread: link list

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    17

    Unhappy link list

    I have to use a link list to pull 10 names from a txt file and then put them in alphabetical order by lastname and then output the list in another file. any help will be greatly appreciated I only have one more day to work on it. Here is what I have so far. I cant figure out how to create a loop to do this.

    Code:
    #include <stdio.h>
    #include <iostream.h>
    #include <fstream.h>
    
    const int ItemLen = 10;
    
    //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[25];
      char filelastname[25];
      int end;
      ListNodePtr p,q,r;
      Head= new ListNode;
    
      ifstream inFile;
    
        inFile.open("a:GOLFNAME.txt",ios::in);
    
      //verify that open was successful
      if(!inFile.fail()) //if open did not fail
        {
         cout<<"file Opened"<<endl;
          //read name
    
          while(!inFile.eof())
        {
         inFile>> filename>> filelastname;
         cout << "The name is: " << filename <<" "<< filelastname << endl;
    
    
          strcpy(Head->name,filename);
          strcpy(Head->lastname,filelastname);
          Head->link= NULL;
        }
        inFile.close();
        //display name
        }
        else
          cout<< "Error opening file." << endl;
         // open failed//end if
    
          p=Head;
       while(p !=NULL)
       {
         cout<< p->name << " " << p->lastname << endl;
         p= p->link; //Get the next node
       }
    
           cin>> end;
      return 0;
    }
    Code tags added by kermi3
    Last edited by fastlane29; 09-14-2002 at 12:27 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