It prints the elements of the list entered properly. But it doesn't print them in the reverse order as expected from the code starting at line 56. How should I modify it to print them in reverse?(if at all a modification is absolutely necessary)
#include<stdio.h>
#include<conio.h>
typedef struct tagnode
{
int data;
struct tagnode *next;
struct tagnode *prev;
}node;
node* create(int);
void print(node *);
int main(void)
{
int n;
node *head;
printf("Enter number of nodes to create");
scanf("%d",&n);
head=create(n);
print(head);
return 0;
}
node* create(int n)
{
int data,i;
node *localhead=(node *)malloc(sizeof(node));
node *movinghead=localhead;
movinghead->prev=NULL;
movinghead->next=NULL;
printf("\nEnter data:");
scanf("%d",&data);
movinghead->data=data;
for(i=1;i<n;i++)
{
movinghead->next=(node *)malloc(sizeof(node));
movinghead->next->prev=movinghead;
movinghead=movinghead->next;
scanf("%d",&data);
movinghead->data=data;
movinghead->next=NULL;
}
return localhead;
}
void print(node *p)
{
node *movinghead;
for(movinghead=p;movinghead!=NULL;movinghead=movinghead->next)
{
printf("\n%d",movinghead->data);
}
while(movinghead->prev!=NULL)
{
printf("%d",movinghead->data);
movinghead=movinghead->prev;
}
getch();
}

New Topic/Question
Reply


MultiQuote




|