#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
struct node *prev;
int info;
struct node *next;
}NODE;
NODE *get_node()
{
NODE *temp;
temp =(NODE* )malloc(sizeof(NODE));
printf("Enter info: ");
scanf("%d", &temp->info);
temp->next =temp->prev =NULL;
return(temp);
}
NODE* create()
{
NODE *first=NULL,*last,*temp;
int i,n;
printf("Enter no.of nodes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
temp=get_node();
if(first==NULL)
{
first = temp;
first->next = first->prev = first;
}
else
{
last->next=temp;
temp->prev=last;
temp->next=first;
first->prev=last;
}
last=temp;
}
return first;
}
void display(NODE *h)
{
NODE *p;
p=h;
while(1)
{
printf("%d ",p->info);
p=p->next;
if(p==h) break;
}
printf("\n");
}
void main()
{
NODE *start;
int ch;
while(1)
{
printf("1.Create\n");
printf("2.Display\n");
printf("3.Exit\n");
printf("Enter your choice (1-3):");
scanf("%d",&ch);
switch(ch)
{
case 1:
start = create();
break;
case 2:
display(start);
break;
case 3:
exit(0);
}
getch();
}
}
Output-
1.Create
2.Display
3.Exit
Enter your choice (1-3):1
Enter no.of nodes:2
Enter info: 2
Enter info: 3
1.Create
2.Display
3.Exit
Enter your choice (1-3):2
2 3
1.Create
2.Display
3.Exit
Enter your choice (1-3):3