This is my code:
#include<stdio.h>
#include<stdlib.h>
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: ");
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%d",&list->data);
list->prev = prev;
prev = list;
if(i<n-1){
list->next = (struct Node *)malloc(sizeof(struct Node));
list = list->next;
}
}
list->next = NULL;
int j=0;
list = head;
while(list!=NULL){
prev = list->prev;
list->prev = list->next;
list->next = prev;
list = list->prev;
}
list = head;
printf("Reversed list: ");
while(list!=NULL){
printf("%d\n",list->data);
list = list->next;
}
return 0;
}
it is printing only first element of the reversed list.