Saturday 13 October 2012

CIRCULAR DOUBLY LINKED LIST


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define pf printf
#define sf scanf
struct node
{
  int info;
  struct node* next;
  struct node* prev;
}*temp,*last,*ptr;
void create()
{
temp=(struct node*)malloc(sizeof(struct node));
pf("Enter no: ");
sf("%d",&temp->info);
temp->next=temp;
temp->prev=temp;
last=temp;
}
void check()
{
if(last==NULL)
{
printf("No node present...");
getch();
exit(0);
}
}
void firstinsert()
{
check();
temp=(struct node*)malloc(sizeof(struct node));
pf("Enter no: ");
sf("%d",&temp->info);
temp->next=last;
last->next=temp;
last->prev=temp;
temp->prev=last;
}
void lastinsert()
{
check();
temp=(struct node*)malloc(sizeof(struct node));
pf("Enter no: ");
sf("%d",&temp->info);
temp->next=last->next;
last->next->prev=temp;
last->next=temp;
temp->prev=last;
last=temp;
}
void locinsert()
{
int i=1,loc;
check();
temp=(struct node*)malloc(sizeof(struct node));
pf("Enter no: ");
sf("%d",&temp->info);
pf("\nEnter loaction: ");
sf("%d",&loc);
if(loc==0)
{
firstinsert();
return;
}
ptr=last->next;
while(i++<loc && ptr!=last)
ptr=ptr->next;
temp->next=ptr->next;
ptr->next->prev=temp;
ptr->next=temp;
temp->prev=ptr;
}
int delcheck()
{
check();
if(last==last->next)
{
last=NULL;
free(last);
return 1;
}
return 0;
}
void firstdelete()
{
if(!delcheck())
{
temp=last->next;
last->next=temp->next;
temp->next->prev=last;
free(temp);
}
}
void lastdelete()
{
if(!delcheck())
{
temp=last;
last->prev->next=last->next;
last->next->prev=last->prev;
last=last->prev;
free(temp);
}
}
void numdelete()
{
int num;
pf("Enter item to be deleted: ");
sf("%d",&num);
ptr=last->next;
while(ptr->info!=num)
{
ptr=ptr->next;
if(ptr==last->next)
{
printf("Item not found");
getch();
return;
}
}
if(ptr==last)
{
lastdelete();
return;
}
else
{
ptr->prev->next=ptr->next;
ptr->next->prev=ptr->prev;
free(ptr);
}
}
void display()
{
       if(last==NULL)
       {
    pf("List Empty");
    getch();
    return;
       }
       else
       {
ptr=last->next;
while(ptr!=last)
{
pf("%d\t",ptr->info);
ptr=ptr->next;
}
pf("%d\t",ptr->info);
getch();
}
}
void main()
{
    int ch;
    clrscr();
    do
    {
clrscr();
pf("\n1.Create LL");
pf("\n2.Insert at Begin");
pf("\n3.Insert at last");
pf("\n4.Insert at location");
pf("\n5.Delete from Starting");
pf("\n6.Delete from last");
pf("\n7.Delete a no.");
pf("\n8.display");
pf("\n9.Exit");
pf("\nEnter your0 choice: ");
fflush(stdin);
sf("%d",&ch);
switch(ch)
{
   case 1: create(); break;
   case 2: firstinsert(); break;
   case 3: lastinsert(); break;
   case 4: locinsert(); break;
   case 5: firstdelete(); break;
   case 6: lastdelete(); break;
   case 7: numdelete(); break;
   case 8: display(); break;
   case 9: exit(0); break;
   default: pf("Wrong input");
}
      }while(ch!=9);
}


*If any doubt or bug regarding the solution then leave comment*

-Gaurav Singhal

2 comments:

Priya said...

Your program is short and nice, but are missing few functionality. I found this over net, Circular Doubly link list with insert, append, delete, copy, reverse and display operations.

Thanks for sharing your program.
Priya

Gaurav Singhal said...

this program is just to give basic concept and basic operations that are performed in LINKED LIST..