Saturday, 6 October 2012

CIRCULAR LINKED LIST.......!!!

BLOCK DIAGRAM OF CIRCULAR LINKED LIST

#include<stdio.h>
#include<conio.h>
#define pf printf
struct node
{
int info;
struct node *next;
}*last,*temp;
void check()
{
temp=(struct node *)malloc(sizeof(struct node));
pf("Enter no: ");
scanf("%d",&temp->info);
if(last==NULL)
{
last=temp;
last->next=last;
}
}
void delcheck()
{
if(last==NULL)
{
pf("\nNo item to delete..");
getch();
exit(0);
}
}
void show()
{
clrscr();
for(temp=last->next;temp!=last;temp=temp->next)
pf("%d\t",temp->info);
pf("%d",temp->info);
getch();
}
void binsert()
{
check();
temp->next=last->next;
last->next=temp;
}
void linsert()
{
binsert();
last=temp;
}
void posinsert()
{
int pos,i=1;
struct node *ptr;
pf("Enter position: ");
scanf("%d",&pos);
ptr=last->next;
while(i++<pos)
ptr=ptr->next;
if(ptr==last)
linsert();
else
{
check();
temp->next=ptr->next;
ptr->next=temp;
}
}
void numdel(char ch)
{
int num;
struct node *ptr;
delcheck();
if(ch=='1')
{
temp=last->next;
last->next=temp->next;
free(temp);
}
else if(ch=='2')
{
temp=last->next;
while(temp->next!=last)
temp=temp->next;
temp->next=last->next;
last=temp;
}
else if(ch=='3')
{
pf("\nEnter no: ");
scanf("%d",&num);
temp=last->next;
while(temp!=last && temp->info!=num)
{
ptr=temp;
temp=temp->next;
}
if(ptr==last->next)
{
pf("\nitem not found");
getch();
}
else
{
ptr->next=temp->next;
last=ptr;
free(temp);
}
}
}
void main()
{
int ch;
while(1)
{
clrscr();
pf("1. Create");
pf("\n2. Insert at begin");
pf("\n3. Insert at last");
pf("\n4. Insert at pos");
pf("\n5. Delete from begin");
pf("\n6. Delete from end");
pf("\n7. Delete a no");
pf("\n8. Show");
pf("\n9. Exit");
pf("\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: check();
break;
case 2: binsert();
break;
case 3: linsert();
break;
case 4: posinsert();
break;
case 5: numdel('1');
break;
case 6: numdel('2');
break;
case 7: numdel('3');
break;
case 8: show();
break;
case 9: exit(0);
default:
pf("Wrong input..");
}
}
}



*if any doubt regarding then put your question in comment box...i will resolve it*