0

I wrote reverse logic but it is not giving desired output.Actually when I printed only linked list Traversal function then it prints finely all elements but when I called reverse function and after calling traversal function then it is not giving reverse linked list. I am not getting where i am wrong.Please resolve my issue. Thanks in advance!!

#include <stdio.h>
#include <stdlib.h>


struct Node
{
    int data;
    struct Node *next;
    struct Node *prev;
};


void linkedListTransversal(struct Node *ptr)
{
    while (ptr != NULL)//&& ptr->prev != NULL
    {
        printf("Element:%d\n", ptr->data);
        ptr = ptr->next;
        
    }
}



void linkedListRevTransversal(struct Node *head)
{
    
    struct Node * temp=NULL;
    struct Node * p=head;
    while (p != NULL)
    {
        
        temp=p->prev;
        p->prev=p->next;
        p->next=temp;
        p=p->prev;
        
    }
    if(temp != NULL )
        head = temp->prev;
    
    
}


int main()
{

    struct Node *head;
    struct Node *second;
    struct Node *third;
    struct Node *fourth;
    head = (struct Node *)malloc(sizeof(struct Node));
    second = (struct Node *)malloc(sizeof(struct Node));
    third = (struct Node *)malloc(sizeof(struct Node));
    fourth = (struct Node *)malloc(sizeof(struct Node));

    head->data = 10;
    head->prev=NULL;
    head->next = second;

    second->data = 20;
    head->prev=head;
    second->next = third;

    third->data = 30;
    head->prev=third;
    third->next = fourth;

    fourth->data = 40;
    head->prev=fourth;
    fourth->next = NULL;

    linkedListTransversal(head);
    linkedListRevTransversal(head);
    linkedListTransversal(head);
}
Output:
Element:10
Element:20
Element:30
Element:40
Element:10
Element:40
Andreas Wenzel
  • 12,860
  • 3
  • 14
  • 29
  • 2
    `"I am not getting where i am wrong."` -- Have you tried running your code line by line in a debugger while monitoring the values of all variables, in order to determine at which point your program stops behaving as intended? If you did not try this, then you probably want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Andreas Wenzel May 09 '21 at 18:30
  • In order to traverse a doubly-linked list backwards, you must start at the tail, not the head. – Andreas Wenzel May 09 '21 at 19:06
  • I assume that the function `linkedListTransversal` is supposed to traverse the list in the forward direction, printing every node it encounters. But what is the function `linkedListRevTransversal` supposed to do? Traverse the list in reverse, from tail to head, while printing every node encountered? Or is it supposed to simply reverse the list, while printing nothing? – Andreas Wenzel May 09 '21 at 19:11
  • If you want to actually reverse the linked list (instead of only printing it in reverse order), then you will need one pointer pointing to the head of the linked list and one to the tail. That way, you can swap both nodes. Afterwards, you move both pointers one node closer to the middle and repeat, until both pointers meet in the middle. – Andreas Wenzel May 09 '21 at 19:35
  • I have included struct Node * prev as a tail while making a node – sanchi gupta May 09 '21 at 19:55
  • linkedListTransversal() is printing the doubly linked list in forward direction and linkedListRevTransversal() is for reversing the doubly linked list. – sanchi gupta May 09 '21 at 19:58
  • You may want to change the name of the function `linkedListRevTransversal`, as that function name implies that it does the same as `linkedListTransversal`, only in reverse. – Andreas Wenzel May 09 '21 at 20:20
  • `tail` is the opposite of `head`, whereas `prev` is the opposite of `next`. `tail` should always point to the last node, just like `head` should always point to the first node. `tail` is not the same thing as `prev`, just like `head` is not the same thing as `next`. Therefore, your comment in which you consider `prev` to be the same thing as `tail` does not make sense. – Andreas Wenzel May 09 '21 at 20:27
  • Yeah,you are right. – sanchi gupta May 09 '21 at 20:54
  • Now i have made tail pointer`void reverseList(struct Node * head){ struct Node *tail=head; while(tail->next != NULL){ tail=tail->next; } while(tail != head){ printf("Element:%d\n", tail->data); tail=tail->prev; } printf("Element:%d\n", tail->data); }` – sanchi gupta May 09 '21 at 20:54
  • But this function only prints 40 – sanchi gupta May 09 '21 at 20:57
  • 1
    Please [edit] your question to update the code with your latest code. Normally, you should not update your question if such a change would invalidate one of the answers. But since your question has no answers (yet), this is not an issue, so it should be ok. – Andreas Wenzel May 09 '21 at 21:18
  • The name `tail` implies that it always points to the last node of the list, just like `head` implies that it always points to the first node of the list. Therefore, it does not seem appropriate to make that variable point somewhere else using the line `tail=tail->prev;`. You may want to create an additional pointer variable with a different name for traversing the list. – Andreas Wenzel May 09 '21 at 21:30
  • In the output of your program, you are mixing the output of several function calls, so that it is hard to see which function is causing which output. Therefore, you may want to add a few `printf` statements into the function `main`, for example `printf( "output from first function call:\n" );`. – Andreas Wenzel May 09 '21 at 21:40
  • In your function `main`, you are only setting `head->prev`, but not `second->prev`, `third->prev` or `fourth->prev`. – Andreas Wenzel May 09 '21 at 21:54

0 Answers0