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*

3 comments:

Mhacom said...

1.) Show is not work when program first start.
2.) Menu 4. Insert at position is not work at the beginning ( when program first start)
3.) Insert at beginnig is not work if on the first time user try to insert at last it accpeted it can keep insert at last but not at the beginning. But it work fine if user insert at last first then menu 2 and 3 will work correctly.
4.) Created list (Menu1) can not perform more than one time. Eg. if user want to created new list ,it won't created, it keep using the previous list.
5.) while deleting elements ,there is no free memory, this created memory leaking.

Mhacom said...
This comment has been removed by a blog administrator.
Gaurav Singhal said...

thanks....i will remove these bugs