Why is my code for reversing doubly linked list not working

This is my code:

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

int main(){
 struct Node *list, *head, *prev;
 head = list = (struct Node *)malloc(sizeof(struct Node));
 prev = NULL;
 int i,n;
 printf("Enter no. of nodes: ");
    list->prev = prev;
    prev = list;
       list->next = (struct Node *)malloc(sizeof(struct Node));
       list = list->next;
 list->next = NULL;
 int j=0;
 list = head;
    prev = list->prev;
    list->prev = list->next;
    list->next = prev;
    list = list->prev;
 list = head;
 printf("Reversed list: ");
    list = list->next;
 return 0;

it is printing only first element of the reversed list.

I’m not a C++ developer, but my first guess would be that before you start your second while loop you are setting list to head, but that head never had its value changed to the head of the reversed list, so you are starting at the end. Could that be the case?

+1 for ArielLeslie’s comment - Remove list = head; before printing the results and do a check in your main while loop before list = list->prev; so list doesn’t get assigned to null at the end (or you assign head to that last node).

The last node in the original list should be the head of the reversed list so make sure you have a pointer to it and start printing from there. (A standard list reversal function would return that pointer, I think.)