Sunday, June 10, 2012

Pin It

Circular Linked List – Model 2

/*Circular Linked List*/

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void insert(int a,int b);
void dis();
void search(int c);
void del(int d);
void len();
void empty();
int l=0,f=0;
struct list
{
int data;
struct list *link;
}*first,*cir;
void dis()
{
struct list *cu;
for(cu=first;cu->link!=cir->link;cu=cu->link)
{
printf(“%d”,cu->data);
if(cu->link!=cir)
printf(“->”);
}
}
void len()
{
struct list *cur;
for(cur=first;cur->link!=cir->link;cur=cur->link)
{
l++;
}
printf(“\n The size of the list is:%d\n”,l);
}
void empty()
{
if(first==0)
{
printf(“\n The List is empty”);
}
else
{
printf(“The list is not empty\n”);
}
}
void search(int g)
{
struct list *p=first;
for(p=first;p->link!=cir->link;p=p->link)
{
if(p->data==g)
{
printf(“\n The element is Present\n”);
f=0;
break;
}
else
{
f=1;
}
}
if(f==1)
{
printf(“\n Element is not present\n”);
}
}
void del(int u)
{
int i;
struct list *cu=first,*q=first;
for(i=1;i<u;i++)
cu=cu->link;
for(i=1;i<=u;i++)
q=q->link;
if(u)
{
cu->link=q->link;
delete(q);
}
else if(cu->link!=NULL && q->link==cir->link)
{
cu->link=cir;
delete(q);
}
if(u==0)
{
first=q->link;
cir->link=first;
delete(q);
}
}
void insert(int a,int b)
{
int i;
struct list *p=first,*q;
q=(struct list*)malloc(sizeof(struct list*));
for(i=1;i<a;i++)
{
p=p->link;
}
q->data=b;
if(a)
{
q->link=p->link;
p->link=q;
}
else
{
q->link=cir;
//first->link=cir;
cir->link=first;
first=q;
}
}
void main()
{
first=(struct list*)malloc(sizeof(struct list*));
cir=(struct list*)malloc(sizeof(struct list*));
first=NULL;
//cir=0;
int c,p,v,s,d;
clrscr();
do
{
printf(“\n Enter your Choice:\n”);
printf(“1.Insert\n2.Delete\n3.display\n4.search\n5.Lenth\n6.Empty\n7.Exit\n”);
scanf(“%d”,&c);
switch(c)
{
case 5:
len();
break;
case 6:
empty();
break;
case 1:
printf(“\n Enter the position and value:\n”);
scanf(“%d%d”,&p,&v);
insert(p,v);
break;
case 2:
printf(“\nenter the position to delete:\n”);
scanf(“%d”,&d);
del(d);
break;
case 3:
printf(“\n The Elements are:\n”);
dis();
break;
case 4:
printf(“\n Enter the element to search:\n”);
scanf(“%d”,&s);
search(s);
break;
default:
printf(“\n Exit Choice\n”);
}
}while(c!=7);
getch();
}

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...