Linked list problem....

This is a discussion on Linked list problem.... within the C++ Programming forums, part of the General Programming Boards category; I have encounter trouble while printing my link list. it displays nothing on screen....i don't know why? Here is my ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    56

    Linked list problem....

    I have encounter trouble while printing my link list.
    it displays nothing on screen....i don't know why?
    Here is my code:

    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <stdlib.h>
    
    
    typedef int ComponentType;
    
    struct NodeType;
    typedef NodeType* NodePtr;
    
    struct NodeType
    {
    	ComponentType component;
    	NodePtr link;
    };
    
    void PrintList(NodePtr);
    
    int main()
    {
    
    	ifstream myFile;
    	NodePtr head;
    	NodePtr newPtr;
    	NodePtr current;
    	ComponentType values;
    
    	head = new NodeType;
    	cin>>head->component;
    	current=head;
    
    	myFile.open("link.txt");
    	if (myFile.fail())
    	{
    		cout<<"Cannot open file"<<endl;
    		exit(-1);
    	}
    
    
    	myFile>>values;
    	while (!myFile.eof())
    	{
    
    		newPtr= new NodeType;
    		newPtr->component = values;
    		current->link=newPtr;
    		current=newPtr;
    		myFile>>values;
    	}
    	current->link = NULL;
    
    	PrintList(head);
    
    
    	myFile.close();
    	return 0;
    }
    
    void PrintList(NodePtr head)
    {
    	NodePtr current = head;
    	while(current)
    	{
    		cout<<current->component<<endl;
    		current= current->link;
    	}
    }
    This little program read from a text file call "link.txt", inside this text file is only four number:

    Code:
    24
    32
    34
    45

    Unfortunately, my program print nothing on the screen.
    (Since i can compile it, i think it would be a logical error)
    Pleas help.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    First of all, what is this line doing for you...

    Code:
    cin>>head->component;
    I'd remove that first of all...

    Also it looks like you are mixing C/C++. Why do you do this..

    Code:
    typedef NodeType* NodePtr;
    You can ( in C++ ) just as easily say:

    Code:
    struct NodeType
    {
      ComponentType component;
      NodeType *link;
    };
    Last edited by MrWizard; 03-07-2003 at 09:29 PM.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    I'm surprised it compiles at all. Anyway, take a look at this. Now it works.

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <fstream.h>
    
    typedef int ComponentType;
    
    //struct NodeType;      // This doesn't look right... declaring the same struct twice
    //typedef NodeType* NodePtr;   //I don't think you can use typedef to give a pointer
    //                                           a name that has no *
    
    struct NodeType
    {
    	ComponentType component;
    //	NodePtr link;
    	NodeType* link;        // pointer definition with *
    };
    
    void PrintList(NodeType*);
    
    int main()
    {
    
    	ifstream myFile;
    	NodeType* head;          // replacing NodePtr with NodeType*
    	NodeType* newPtr;      // same
    	NodeType* current;      // same
    	ComponentType values;
    
    	head = new NodeType;
    	head -> link = NULL;  // initialize link when node is created
    
    	//	cin>>head->component; //  what was this line for?
    
    	current=head;
    
    	myFile.open("link.txt");
    	if (myFile.fail())
    	{
    		cout<<"Cannot open file"<<endl;
    		exit(-1);
    	}
    
    
    	myFile>>values;
    	if(myFile.eof())
    	{
    		cout << "Input file is empty.";
    		return 0;
    	}
    	head ->component=values;		// you weren't using the first node
    //	myFile>>values;			// this line would prevent last value from being stored
    	while (!myFile.eof())
    	{
    		myFile>>values;
    		newPtr= new NodeType;
    		newPtr->link=NULL;                  // initialize link
    		newPtr->component = values;
    		current->link=newPtr;
    		current=newPtr;
    //		myFile>>values;
    	}
    //	current->link = NULL;
    
    
    	PrintList(head);
    
    
    	myFile.close();
    	return 0;
    }
    
    void PrintList(NodeType* head)
    {
    	NodeType* current = head;
    	while(current)
    	{
    		cout<<current->component<<endl;
    		current= current->link;
    	}
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 07:16 PM
  2. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 09:55 PM
  3. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 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