Thread: help with linked list

  1. #1
    Registered User
    Join Date
    Nov 2006

    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

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


    struct NodeType  
    	string	  info;
    	NodeType*   link;
    typedef  NodeType*  NodePtr;
    // Variable DECLARATIONS
    NodePtr  head;
    NodePtr  ptr; 
    class SortedList
    	void Insert(string); 
    	void TraverseList();
    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;
    				prevPtr->link = location;

  2. #2
    The larch
    Join Date
    May 2006
    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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Rishon LeZion, Israel
    Also you can pass item to Insert function as const reference to avoid copying data to the temporary var
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Aug 2005
    If you think you have to put function implementations into the header mark them as inline.

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, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM