link list

This is a discussion on link list within the C++ Programming forums, part of the General Programming Boards category; I have to use a link list to pull 10 names from a txt file and then put them in ...

  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 01:27 PM.

  2. #2
    Registered User
    Join Date
    Jun 2002
    Posts
    151
    Have you decided on a method to sort your list and are having trouble implementing it? Or do you just need a sorting algorithm that you'll have a shot at? Or do you have no sorting algorithm to use, and no idea how to implement it (in which case you want someone to do your homework)?


    Assuming the first is correct, post some code.

    Assuming the second one is correct, a very simple way would be to maintain a sorted list. Every time, a new element is a candidate for insertion, iterate through the existing list and determine the location it should occupy, inserting it at that location.

    Assuming the third one is correct, either have a go at the above (or any of the numerous alternative methods for sorting stuff) and post some code.

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

    Question

    this is all the code I have so far , I know how to output to a file. So I dont need help with that part. The part I have is pulling the 10 names from a txt file and putting them into 10 different nodes on the link list and sorting them in order. My teacher has not explained how to do this yet he expects us to complete this kind of program. I cannot figure out how to put the names in the nodes on the list as I pull them from the file. I figure it must be some kind of FOR loop but I am lost. And I have never sorted charicter strings before. I have sorted integers with arrays in my 120 c++ class last year is it simular?

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    151
    >My teacher has not explained how to do this yet he expects us to complete this kind of program

    I believe this is probably a lie (even if your teacher hasn't explained it he/she's probably fobbed you off with a text book).

    Even so, as this homework question is short, have you not read it before? Did you not realise the day it was given to you that the teacher hadn't told you how to do it? There's no reason for the feeble 'this is my last day' excuse. I suspect you're just lazy; chew on this crap -

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    struct node
    {
    	string name;
    	node* next;
    	node* prev;
    };
    
    class list
    {
    	node *head;
    
    public:
    
    	list(string n)
    	{
    		head = new node;
    		head->name = n;
    		head->next=0;
    		head->prev=0;
    	}
    
    	void insert(string n)
    	{
    		node* ne = new node;
    		ne->name = n;
    		node* it = head;
    
    		bool inserted=false;
    		
    		while(it!=0)
    		{
    			if(it->name>n)
    			{
    				if(it==head)
    				{
    					ne->next=head;
    					head->prev=ne;
    					ne->prev=0;
    					head=ne;
                        inserted=true;
    					break;
    				}
    				else
    				{
    					it->prev->next=ne;
    					ne->prev=it->prev;
                        it->prev=ne;
    					ne->next=it;
    					inserted=true;
    					break;
    				}
    			}
    			it=it->next;
    		}
    
    		if(inserted==false)
    		{
    			node* end=head;
    	
    			while(end->next!=0)
    				end=end->next;
    
    			end->next=ne;
    			ne->prev=end->next;
    			ne->next=0;
    		}
    
    	}
    
    	void print()
    	{
    		node* it = head;
    		while(it!=0)
    		{
    			cout << it->name << '\n';
    			it=it->next;
    		}
    	}
    
    	~list()
    	{
    		
    		while(head!=0)
    		{
    			node* t = head;
    			head=head->next;
    			delete t;
    		}
    	}
    			
    };
    
    int main()
    {
    	list i("ed");
    	i.insert("an");
    	i.insert("ze");
    	
    	i.print();
    
    	return 0;
    }

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    17
    Thanks for you help since im a nubee im not real good at this yet.
    and for your s/a lazy comment I have 2 kids work 50+ hours a weekdriving for the post office, full time college and my wife works full time to. so I dought am a very lazy person and my text book does not have anything about sorting by strings just by numbers and my class is down to 3 people in it becouse my teacher teaches like he does. And he did not explain it I would not lie about it. Why would I???? He explained about creating a link list using stuct which I sort of can do but he did not explain the question that I am asking becouse if he did I would not have wasted my time on the internet. And I only ask help on here as a last resort I have been trying to figure this out for the last 2 days. Thanks for the help I will try and chew on some of your crap and see what I can do.


    Fastlane

  6. #6
    Registered User
    Join Date
    Jun 2002
    Posts
    151
    >and for your s/a lazy comment I have 2 kids work 50+ hours a weekdriving for the post office, full time college and my wife works full time to.<

    Perhaps you're spreading yourself a little thin then. Either that or follow your classmates out of the door.

    >And he did not explain it I would not lie about it. Why would I????<

    You'd be amazed how many hard luck stories and tales of bad teachers there are on internet newsgroups/messageboards spread by students asking for help with there homework. People tend to get a little cynical.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    17
    hahahah! Boy your a real character. I never ask you to write my code I only ask for help. A example of a FOR loop similar to what might apply for my application would have been great or directions to a program with simular coding. And for the follow out the door statement, I have never give up on anything in my life and I am not about to start now over some confusing code. It seems to me you need to practice on your people skills or get away from your computer for a while and experience a reality check. But I cannot gripe about help becouse I will take it any way I can get it. Your code was a little different than what I was looking for because I cannot use a CLASS but I will look at it closer and see if I can use it to figure out what I need.

    Thanks Again,

    Fastlane

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    There's some link list papers here you might find of use. There's also some papers on Trees which might do the job nicely, but I expect that's a lesson or two away just yet. Have fun!

    Note that Enmeduranki's code inserts new items into the list in the correct alphabetic order, whereas a lot of link list code assumes you just add new nodes at the start of the list.

    As you want yours in order (like Enmeduranki's), just iterate through the list comparing each node with the new item, when you get to one that has a higher value, insert into the list. It's more complicated than adding it to the start, but still reasonably easy
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User
    Join Date
    Jun 2002
    Posts
    151
    >>Boy your a real character

    That's 'you're'.

    >> never ask you to write my code I only ask for help. A example of a FOR loop similar to what might apply for my application would have been great or directions to a program with simular coding<<

    If only everything in life could be expressed with a for loop.

    >I have never give up on anything in my life <

    While this is no doubt an endearing statement. The fact that you drive for a living raises a doubt over its validity. Not that there's anything wrong with driving; or shooting a horse that's lame (not you; the horse/course).

    >Your code was a little different than what I was looking for because I cannot use a CLASS but I will look at it closer and see if I can use it to figure out what I need.<

    Just change it to a struct then.
    Last edited by Enmeduranki; 09-14-2002 at 08:18 PM.
    rock me, joe

  10. #10
    Registered User
    Join Date
    Mar 2002
    Posts
    17
    If a loop is not possible then you should have just told me, how should I know im a newbee and for your taxi statement I must drive the only taxi with 18 wheels "that would be a sight" and I would compare what I make a year with you're W2 statement anytime. And I am only 29 I own my house sports cars new ford supercrew completley debt free can you say the same.

    Hammer, thanks for the help I will look the stuff and see what I can do wiht it.


    And for you Endemesadkj or what ever "Have a nice day"

  11. #11
    fry
    fry is offline
    Registered User fry's Avatar
    Join Date
    Mar 2002
    Posts
    128
    fastlane29 >> You may mean well, but your really not doing yourself any favours by trying to make you situation inlife look bad. Nor is it helping that you suddenly brag about all your accomplishments.

    Enmeduranki was trying to help you the same way that people here help anyone else. Nooone wants to do a persons homework, but if they can show that they've had a go, people are more than happy to give a nudge in the right direction Bringing in your life story was unnecessary, even if you were called lazy. Enmeduranki is right in questioning why you left it to the last day.
    IDE: Dev C++ 5
    Lib: Allegro
    OS: Windows 2000

  12. #12
    Registered User
    Join Date
    Jun 2002
    Posts
    151
    >If a loop is not possible then you should have just told me<

    I asked you what you wanted in my first response.

    >and for your taxi statement I must drive the only taxi with 18 wheels "that would be a sight" and I would compare what I make a year with you're W2 statement anytime.<

    Sorry for any offense, it was not meant. I'm not sure how this turned into a ........ing contest so I bid you fastlane29, a good day (or night/whatever).
    rock me, joe

  13. #13
    Registered User
    Join Date
    Mar 2002
    Posts
    17
    Fry,


    I never said anything negative about his help I was greatfull for it then and I still am I just had to respond to him trying to put down what I do for a living, Im sure any one would. And I would hate to think my life story would be a few things that I have worked hard for. And I also stated this is not the first day that I have tried to complete this program and that I only tried for help on this site as a last resort.


    Fastlane

  14. #14
    Registered User
    Join Date
    Mar 2002
    Posts
    17
    Hey Enme,

    No offense taken, I just figured you were haveing a bad day. Besides im one of those hard headed guys that like arguing.


    Fastlane

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, 10: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, 11:21 PM

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