DOUBLY CIRCULAR LINKED LIST

‎//Program to perform various operations in DOUBLY CICULAR LINKED LIST
#include
#include
#include
#include
#include
struct dcllnode
{
int info;
dcllnode *next,*prev;
};
dcllnode *start;

void insertbeg()
{
dcllnode *p,*q;
p=(dcllnode*)malloc(sizeof(dcllnode));
printf("\nEnter element to insert at beginning:");
scanf("%d",&p->info);
if(start==NULL)
{
start=p;
p->prev=start;
p->next=start;
}
else
{
q=start;
while(q->next!=start)
{
q=q->next;
}
p->next=start;
start=p;
p->prev=q;
q->next=start;
}
}

void insertend()
{
dcllnode *p,*q;
p=(dcllnode*)malloc(sizeof(dcllnode));
printf("\nEnter element to insert at end:");
scanf("%d",&p->info);
if(start==NULL)
{
start=p;
p->prev=start;
p->next=start;
}
else
{
q=start;
while(q->next!=start)
{
q=q->next;
}
p->prev=q;
p->next=start;
q->next=p;
q=start;
q->prev=p;
}
}

void deletend()
{
dcllnode *p,*q;
p=start;
if(start==NULL)
{
printf("\nDoubley Circular linked list is underflow");
}
else
if(p->prev==start && p->next==start)
{
start=NULL;
printf("\nElement deleted, Doubly linked list is now empty");
}
else
{
q=start;
while(q->next->next!=start)
{
q=q->next;
}
q->next=start;
p->prev=q;
printf("\nElement is deleted");
}
}

void deletbeg()
{
dcllnode *p,*q;
p=start;
if(start==NULL)
{
printf("\nDoubley Cicular linked list is underflow");
}
else
if(p->prev==start && p->next==start)
{
start=NULL;
printf("\nDoubly linked list is now empty");
}
else
{
q=start;
while(q->next!=start)
{
q=q->next;
}
p=start;
p=p->next;
start=p;
p->prev=q;
q->next=start;
printf("\nElement is deleted");
}
}

void display()
{
dcllnode *p;
p=start;
cout<<"\nDoubly linked list is:\n\n";
printf("\nAddress of start is: %u\n",start);
while(p->next!=start)
{
printf("\nAddress of previous node is: %u",p->prev);
printf("\nValue of node is: %d",p->info);
printf("\nAddress of node is: %u",p);
printf("\nAddress of next node is: %u",p->next);
printf("\n");
p=p->next;
}

if(p->next==start)
{
printf("\nAddress of previous node is: %u",p->prev);
printf("\nValue of node is: %d",p->info);
printf("\nAddress of node is: %u",p);
printf("\nAddress of next node is: %u",p->next);
}
}

void main()
{
int ch;
clrscr();
while(1)
{
cout<<"\n1:Insertion from beginning";
cout<<"\n2:Insertion from end";
cout<<"\n3:Deletion from beginning";
cout<<"\n4:Deletion from end";
cout<<"\n5:Display";
cout<<"\0:Exit";

cout<<"\nEnter your choice:";
cin>>ch;
clrscr();
switch(ch)
{
case 1:
insertbeg();
getch();
break;

case 2:
insertend();
getch();
break;

case 3:
deletbeg();
getch();
break;

case 4:
deletend();
getch();
break;

case 5:
display();
getch();
break;

default:exit(0);
}
}
getch();
}

Popular Posts