Thread: Traversing linked list help

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    22

    Traversing linked list help

    Hi all, I'm in need of some help.
    I made a simple linked list, but when I try to traverse through it in reverse, I keep getting a "exception error"...can someone shed any light on where I'm going wrong?
    Please and thank you

    Code:
    struct Node
    {
    	int Data; 
    	Node *next; 
    	Node *prev; 
    }; 
    
    int main()
    {
    
            Node *head = new Node; 
    	Node *current, *tail; 
    	current = head; 
    	tail = head; 
    	cout << current << endl; 
    	cout << tail << endl; 
    	cout << head << endl; 
    	//all current,head,and tail point to the same location in the heap 
    
    	//NOW COMES THE HARD PART
    	current->Data = 0; 
    	current->prev = NULL; 
    	//the head's previous is now point to null 
    
            for(int i = 1; i <= 10; i++)
    	{
    		current->next = new Node;
    		current->next->Data = i; 
    		current = current->next; 
    		tail = current->prev; 
    		tail = current; 
    	}
    
    	current->next = NULL; 
    
    	while(tail != NULL)
    	{
    		cout << tail->Data << endl; 
    		tail = tail->prev; //why is tail not being able to capture the previous location?
    	}
    
    	cout << "data in tail: " <<  tail->Data << endl;
    	//cout << tail->prev->Data << endl; //at this line, I receive compile error
    	//a bunch of question marks pop up
    	//why is tail not being able to hold the previous node's address???

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    You omitted one step in this loop:

    Code:
    for(int i = 1; i <= 10; i++)
    	{
    		current->next = new Node;
    		current->next->Data = i; 
    		current = current->next;   // You went wrong here. You have now dropped your handle on the
                                               // "old" current node without setting the prev pointer of the new node
    		tail = current->prev; // You don't know where current->prev is pointing.
    		tail = current; 
    	}
    Instead, you should have:
    Code:
    for(int i = 1; i <= 10; i++)
    	{
    		current->next = new Node;
                    current->next->prev = current;  // You NEED this.
                    current->next->next = NULL;    // This would be a good idea too.  All pointers should be set
                                                   // when the new node is created.  Then you can eliminate the
                                                   // "current->next = NULL;" line that you had after the end of the loop.
    		current->next->Data = i;
    		current = current->next; 
    		tail = current; 
    	}

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    22
    Thanks Stiltskin that fixed the error I kept on receiving!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding directory/file names to a linked list
    By thoseion in forum C Programming
    Replies: 13
    Last Post: 12-08-2006, 01:13 PM
  2. Replies: 5
    Last Post: 11-04-2006, 06:39 PM
  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