help with linked list

This is a discussion on help with linked list within the C++ Programming forums, part of the General Programming Boards category; i have created a program to take last names into a linked list, now i have everything working except one ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    4

    help with linked list

    i have created a program to take last names into a linked list, now i have everything working except one thing, i need to output the files to a txt file, but currently when i do that , its only outputing my "end" word in order to stop the program below is the code please let me know what i have done wrong


    main.cpp
    Code:
    void main()
    {
    	string name;
    	SortedList Roster;
    	
    	ofstream output;
    	output.open("roster.txt");
    	
    	while (name != "end" )
    	{
    	cout << "Please enter your last name -- Type END when finished" << endl;
    	cin >> name;
    	Roster.Insert(name);
    
    	output << name << endl;
    
    	output.close();
    	}
    	
    	cout << "You entered:" << "\n" << "\n";
    	Roster.TraverseList();
    }


    linkedlist.h


    Code:
    struct NodeType  
    {
    	string	  info;
    	NodeType*   link;
    };
    
    typedef  NodeType*  NodePtr;
    
    // Variable DECLARATIONS
    
    NodePtr  head;
    NodePtr  ptr; 
    
    class SortedList
    {
    public:
    	SortedList();
    	void Insert(string); 
    	void TraverseList();
    
    private:
    		
    };
    
    SortedList::SortedList( )	   	// Constructor
    // Post:	  head == NULL
    {	
    	head = NULL;
    }
    
    
    // insert into the list
    void  SortedList::Insert( string item ) 
    {
    		NodePtr  currPtr;
    		NodePtr  prevPtr;
    		NodePtr  location;
    		
    		location = new  NodeType;
    		location->info  =  item;
    		prevPtr = NULL;
    		currPtr = head;
    		while ( currPtr != NULL  &&  item > currPtr->info  )
    		{	   prevPtr = currPtr;				// advance both pointers
    				currPtr = currPtr->link;
    		}
    		location->link = currPtr;		 // insert new node here 
    		if  ( prevPtr == NULL )
    				head = location;
    		else
    				prevPtr->link = location;
    }

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Could there be a problem with you closing the file in the while loop?

    Other problems with the code:
    1) main returns int, not void.
    2) SortedList needs a destructor to free the memory.
    3) NodePtr head must not be a global, but a private member of SortedList (consider someone instantiating more than one instance of SortedList).
    4) I don't think you should insert "end" in the SortedList.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Also you can pass item to Insert function as const reference to avoid copying data to the temporary var
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    If you think you have to put function implementations into the header mark them as inline.
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 08:07 PM
  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