Here's a very primitive code for a doubly linked list. Although its only very rudimentary, whats wrong with this code? the code's supposed to print the elements sequentially starting from the first node and later print them in reverse.
Code:
#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();
}
Any help would be appreciated!!