Thread: Reversal of a Doubly Linked List !

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    51

    Reversal of a Doubly Linked List !

    Hi,

    I was trying to reverse a list by swapping the previous and the next address of the current list. The logic seems to be right, but i cannot point as to where exactly the segmentation fault occurs.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    typedef struct Node
    {
    	int data;
    	Node *next;
    	Node *prev;
    }Node;
    
    
    Node* createNode(int data)
    {
    	Node *newNode = (Node *)malloc(sizeof(Node));
    	newNode->data = data;
    	newNode->next = NULL;
    }
    void BuildList(Node **root, int data)
    {
    	Node *temp = (*root);
    	if(temp == NULL)
    	{
    		(*root) = createNode(data);
    	}
    	else
    	{
    		while(temp->next != NULL)
    			{
    				temp = temp->next;
    			}
    		Node *newNode = createNode(data);
    		temp->next = newNode;
    		newNode->prev = temp;
    	}	
    }
    
    void reverseListSwap(Node **root)
    {
    	Node *current = (*root);
    	Node *prev = NULL;
    	Node *next = current->next;
    	Node *loop = (*root);
    	while(current->next != NULL)
    	{
    		Node *temp = current;
    		temp->next = prev;
    		temp->prev = next;
    		prev = current;
    		next = current->next;
    		current = current->next;
    	}
    	current->next = prev;
    	current->prev = NULL;
    	(*root) = current;
    }
    int main()
    {
    	Node *root = NULL;
    	BuildList(&root, 45);
    	BuildList(&root, 46);
    	BuildList(&root, 47);
    	BuildList(&root, 48);
    	BuildList(&root, 49);
    	reverseListSwap(&root);
    	printList(&root);
    	return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It seems to me that you have too many variables. For example, it looks like you could simplify to something like this:
    Code:
    void reverseListSwap(Node **root)
    {
        Node *previous = NULL;
        Node *current = *root;
        while (current)
        {
            Node *next = current->next;
            current->next = current->previous;
            current->previous = next;
    
            previous = current;
            current = next;
        }
        *root = previous;
    }
    I am not keen on your use of current->next without checking for current because it does not account for an empty linked list.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubly linked list
    By BEN10 in forum C Programming
    Replies: 4
    Last Post: 07-21-2009, 09:32 AM
  2. doubly linked list
    By bazzano in forum C Programming
    Replies: 5
    Last Post: 04-26-2007, 03:41 AM
  3. Doubly-Linked List
    By jgs in forum C Programming
    Replies: 7
    Last Post: 04-18-2005, 01:39 PM
  4. singly linked list to doubly linked list
    By t48j in forum C Programming
    Replies: 3
    Last Post: 03-23-2005, 06:37 PM
  5. Linked List nodes reversal
    By gkev in forum C++ Programming
    Replies: 3
    Last Post: 02-06-2003, 10:39 AM