Thread: Linked list part 2 (Syntax error free version)

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

    Linked list part 2 (Syntax error free version)

    Hello, now, i am creating a program to create 3 lists. (List1, list2, list3)

    my program will read from a txt file which contains letters.

    for example:
    Code:
    a
    b
    c
    d
    my goal is to create 2 identical list, each node contain one letter. After that, i have to put each letter from my list2 to list3, but in reverse order.

    therefore, list 1 should contain:
    abcd

    list 2 should contains
    abcd

    and list 3 should contains
    dcba

    Here is my program, i think i have some logical error in my "insertFront" funcion..

    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <stdlib.h>
    
    struct NodeType;
    typedef NodeType* Nodeptr;
    struct NodeType
    {
    	char letter;
    	Nodeptr link;
    };
    
    char DelBeginning(Nodeptr&);
    void insertFront(Nodeptr&, char);
    Nodeptr head3;
    	
    int main()
    {
    	
    	Nodeptr head1, current1, newptr1, head2, current2, newptr2;
    	ifstream myFile;
    	int count=0;
    	char value, letter;
    	myFile.open("ulist.txt");
    	
    	if (myFile.fail())
    	{
    		cout<<"Cannot open file";
    		exit(-1);
    	}
    	
    	head1 = new NodeType;
    	myFile>>head1->letter;
    	head2 = new NodeType;
    	head2->letter=head1->letter;
    	myFile>>value;
    	current1=head1;
    	current2=head2;
    	
    	while (!myFile.eof())
    	{
    		newptr1=new NodeType;
    		newptr1->letter=value;
    		newptr2=new NodeType;
    		newptr2->letter=newptr1->letter;
    		current1->link=newptr1;
    		current1=newptr1;
    		current2->link=newptr2;
    		current2=newptr2;
    		myFile>>value;
    		count++;
    	}
    	count++;
    	current1->link=NULL;
    	current2->link=NULL;
    	
    
    	for (int i=0; i<count; i++)
    	{
    		letter=DelBeginning(head2);
    		insertFront(head2,letter);
    		
    	}
    	myFile.close();
    	return 0;
    }
    
    char DelBeginning(Nodeptr& head)		//Remove an item from the front of list 2 and returns it to the main function
    {
    	char value;
    	value=head->letter;
    	Nodeptr current=head->link;
    	delete head;
    	head=current;
    	return (value);
    
    }
    void insertFront(Nodeptr& head, char letter)		//Inserts the item at the front of list3
    {
    	Nodeptr newptr;
    	newptr=new NodeType;
    	newptr->letter=letter;
    	
    	
    	if (head==NULL)
    	{
    		newptr->link=NULL;
    		head3=newptr;
    	}
    	else
    	{
    		newptr->link=head3;
    		head3=newptr;
    	}
    }
    I think i will got a NULL in the beginning instead at the end.

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    In insertFront( ), you want to check whether head3 is NULL, not whether head is NULL.

    Also, you need to delete all of the nodes you created.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 09:27 PM
  2. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 06:40 PM
  3. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  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