Wednesday 22 August 2012

Circular linked list | malloc(sizeof) | deletion | insertion code




/* Program for implementation of circular linked list
and operation on cicular linked list */

# include <stdio.h>
# include <malloc.h>

struct node
{
int info;
struct node *link;
}*last;

void create(int data)
{
struct node *q,*temp;
temp=malloc(sizeof(struct node));
temp->info=data;
if(last==NULL)
{
last=temp;
temp->link=last;
}
else
{
temp->link=last->link;
last->link=temp;
last=temp;
}
}

void insbig(int data)
{
struct node *temp;
temp=malloc(sizeof(struct node));
temp->info=data;
temp->link=last->link;
last->link = temp;
}

void insmid(int data,int pos)
{

struct node *temp,*q;
int i;
q=last->link;
for(i=1;i<pos-1;i++)
q=q->link;
temp=malloc(sizeof(struct node));
temp->link=q->link;
temp->info=data;
q->link=temp;
}

void del(int data)
{
struct node *temp,*q;
if(last->link==last && last->info==data)
{
temp=last;
last=NULL;
free(temp);
}
q=last->link;
if(q->info==data)
{
temp=q;
last->link=q->link;
free(temp);
}
while(q->link!=last)
{
if(q->link->info==data)
{
temp=q->link;
q->link=temp->link;
free(temp);
printf("\n%d is deleted form list",data);
}
q=q->link;
}
if(q->link->info==data)
{
temp=q->link;
q->link=last->link;
free(temp);
last=q;
}
printf("\nElement %d not found",data);
}

void display()
{
struct node *q;
if(last==NULL)
printf("\nList is empty");
q=last->link;
while(q!=last)
{
printf("%d\t",q->info);
q=q->link;
}
printf("%d",last->info);
}

void main()
{
int ch,n,m,pos,i;
last=NULL;
clrscr();
while(1)
{
printf("\n\n1:For Create List");
printf("\n2:For Add at begining");
printf("\n3:For Add between");
printf("\n4:For Delete");
printf("\n5:For Display");
printf("\n6:For Exit");
printf("\nEnter your choice:- ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nHow many nodes you want into linked list:- ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the element:- ");
scanf("%d",&m);
create(m);
}
break;
case 2:
printf("\nEnter the element:- ");
scanf("%d",&m);
insbig(m);
break;
case 3:
printf("\nEnter the element:- ");
scanf("%d",&m);
printf("\nEnter the position at which this element is inserted:- ");
scanf("%d",&pos);
insmid(m,pos);
break;
case 4:
if(last==NULL)
printf("\nList is empty\n");
else
{
printf("\nEnter the number for deletion:- ");
scanf("%d",&m);
del(m);
}
break;
case 5:
display();
break;
case 6:
exit(0);
default:
printf("\nYou entered wrong choice");
}
}
}
Share This
Previous Post
Next Post

FYJC XI standard online admisson Process and declaraton of Merit list . Cut off List For prevous year also . 10 Th Results onlne declaraton Maharashtra Region .

0 comments: