Linked list logic...

This is a discussion on Linked list logic... within the C++ Programming forums, part of the General Programming Boards category; Hello, now, i am creating a program to create 3 lists. (List1, list2, list3) my program will read from a ...

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

    Linked list logic...

    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
    {
    	char letter;
    	NodeType* link;
    };
    
    char DelBeginning(NodeType&);
    void insertFront(NodeType&, char);
    	
    int main()
    {
    	
    	NodeType* 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(NodeType& head)		//Remove an item from the front and returns it
    {
    	char value;
    	value=head->letter;
    	NodeType* current=head->link;
    	delete head;
    	head=current;
    	return (value);
    
    }
    void insertFront(NodeType& head, char letter)		//Inserts the item at the front of list3
    {
    	NodeType* newptr, * head3;
    	newptr=new NodeType;
    	newptr->item=value;
    	
    	
    	if (head==NULL)
    	{
    		newptr->link=NULL;
    		head3=newptr;
    	}
    	else
    	{
    		newptr->link=head3;
    		head3=newptr;
    	}
    }

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Code:
    NodeType* newptr, * head3;
    You're going to want to make head3 global, otherwise it will not be preserved between function calls.
    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

  3. #3
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ~

    Or make it static
    "He who makes a beast of himself, gets rid of the pain of being a man." Dr. Johnson

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718

    Re: ~

    Originally posted by rmullen3
    Or make it static
    If it was made global he would be able to access it from the main function.
    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

  5. #5
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ~

    sorry, picked through the code too quickly
    "He who makes a beast of himself, gets rid of the pain of being a man." Dr. Johnson

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    56
    ok thanks,
    now i make 'head3' to global...
    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <stdlib.h>
    
    
    struct NodeType
    {
    	char letter;
    	NodeType* link;
    };
    
    char DelBeginning(NodeType&);
    void insertFront(NodeType&, char);
    NodeType* head3;
    	
    int main()
    {
    	
    	NodeType* 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(NodeType& head)		//Remove an item from the front(List 2) and returns it to the main function
    {
    	char value;
    	value=head->letter;
    	NodeType* current=head->link;
    	delete head;
    	head=current;
    	return (value);
    
    }
    void insertFront(NodeType& head, char letter)		//Inserts the item at the front of list3
    {
    	NodeType* newptr;
    	newptr=new NodeType;
    	newptr->item=value;
    	
    	
    	if (head==NULL)         //make the node point to nothing
    	{
    		newptr->link=NULL;
    		head3=newptr;
    	}
    	else               //This part make the node in the front
    	{
    		newptr->link=head3;
    		head3=newptr;
    	}
    }
    but i think i have logical error in this part:
    Code:
    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.

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    56
    sorry guys,

    please go to my new topic instead of this messy one with syntax error....

    my new topic is "Linked list part 2 (Syntax error free version)"

  8. #8
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Instead of making head3 global, just declare it in main() and pass it by reference to insertFront().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list question
    By mikeman in forum C Programming
    Replies: 1
    Last Post: 11-30-2008, 01:56 PM
  2. singly linked to doubly linked
    By jsbeckton in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 07:47 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  4. How to use Linked List?
    By MKashlev in forum C++ Programming
    Replies: 4
    Last Post: 08-06-2002, 08:11 AM
  5. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM

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