Friday, March 25, 2011

Circular Doubly linked list


/*Program on circular doubly linked list without any header node.*/


#include<stdio.h>
#include<stdlib.h>
struct list
{
int num;
struct list *next;
struct list *prev;
};

struct list *node;
void create(struct list *n)
{
char ch;
node=n;
printf("\nWant to create location(y/n):-");
scanf("%c",&ch);
fflush(stdin);
while(ch!='n')
{
fflush(stdin);
node->next=(struct list *)malloc(sizeof(struct list));
node->next->prev=node;
node=node->next;
printf("\Enter value:-");
scanf("%d",&node->num);
fflush(stdin);
printf("\Any more (y/n)");
scanf("%c",&ch);
}

node->next=n;
n->prev=node;
}


void display(struct list *n)
{
node=n->next;
while(node!=n)
{
printf("%d\n",node->num);
node=node->next;
}
}


void main()
{
struct list *start=NULL;
clrscr();
create(start);
printf("Now display the value of the circular lists:-\n");
display(start);
getch();
}