Sunday, April 27, 2008

Link List Progarm Implemaentin all its features


#include
#include
#include stdlib.h
#define Null 0

/*Structure Definition*/
struct node
{int val;
struct node * next;};

/*Declarations of Functions Used*/
node* find(node*,int);
node* find1(node*,int);
node* find2(node*,int);
void menu(int);
node* create(node*,int);
void display(node*);
node* insert(node*);
node* adelete(node*);
node* hdelete(node*);
void access(node*);
int size(node*);

/*Main Program*/
void main()
{
int i,s,c=10;
node * head;

/* Link-list Input from User*/
printf("enter initial size of llist\n ->");
scanf("%d",&s);
head=Null;
head=create(head,s);
display(head);

/*Operations On Link-List*/
while(c!=5)
{
menu(0);
scanf("%d",&c);
switch(c)
{
case 1 : head=insert(head);;break;
case 2 : head=adelete(head);break;
case 3 : access(head);break;
case 4 : printf("\nSize of link-list is %d\n",size(head));break;
case 0 : display(head);break;
case 5 : break;
default : printf("Invalid Option \n Try again Choice should be between 1 and 5\n");
}
s=size(head);
}
}

/*Function to dispaly operations on link-list.*/
void menu(int c)
{
if(c==0)
printf("\n Enter option \n 1.Insert \n 2.Delete \n 3.Access \n 4.Size\n 5.Exit\n");
else if(c==1)
printf("\n Enter option \n 1.First \n 2.Before \n 3.After \n 4.end\n");
else if(c==2)
printf("\n Enter option \n 0.Given value \n 1.First \n 2.Before \n 3.After \n 4.End\n");
}


/*Funtion to create Link-List.*/
node* create(node* head,int s)
{
int i;node * point;
if(s==0)
return(head);
else
{
printf("Enter Values to insert\n");
head=(node*) malloc(sizeof(node));
scanf("%d",&(head->val));
point=head;
for(i=1;inext=(node*) malloc(sizeof(node));
scanf("%d",&(point->next->val));
point->next->next=Null;
point=point->next;
}

return(head);
}
}

/*Fuction to display Complete Link-list.*/
void display(node * head)
{
int i;int s=size(head);
for(i=0;i",head->val);
head=head->next;
}
}

/*Function to insert Node in link-list.*/
node* insert(node* head)
{
int c,kv;node * point;node * temp;node* key;
printf("What value you want to Insert?\n");
node* anew=(node*)malloc(sizeof(node));
scanf("%d",&(anew->val));
key=find1(head,anew->val);
if(key!=Null)
{
printf("Value Alraedy exist\n"); free(anew); return(head);
}
anew->next=Null;
menu(1);
scanf("%d",&c);

switch(c)
{
case 2:
printf("Enter key value");
scanf("%d",&kv);
if(head->val!=kv)
{
key=find(head,kv);
anew->next=key->next;
key->next=anew; break;
}
else c=1;
case 1:
anew->next=head;
head=anew;break;
case 3:
printf("enter key value");
scanf("%d",&kv);
if(head->val!=kv)
{
key=find(head,kv);
anew->next=key->next->next;
key->next->next=anew; break;}
else
{
anew->next=head->next;
head->next=anew; break;}
case 0:
case 4:
{
if(head==Null)
{
printf("head is null");
head=anew;}
else
{
point=head;
while((point->next)!=Null)
{
point=point->next;
}
point->next=anew;
}
}
break;
default : printf("Invalid Option \n Choice should be between 1 and 5\n");
break;

}
return(head);
}

/*Fuction to perform deletion on a link-list*/
node* adelete(node* head)
{
int c,kv;node * point;node * temp;node* key;
menu(2);
scanf("%d",&c);

switch(c)
{
case 0:
if(head->val!=kv)
{
printf("Enter key value");
scanf("%d",&kv);
key=find(head,kv);
if(key==Null)
c=8;
else
key->next=hdelete(key->next);
break;
}
else c=1;
case 1:
head=hdelete(head);break;
case 2:
if(kv==head->val)
{
printf("there no item before %d",kv);break;}
else if(head->next->val==kv)
{
head=hdelete(head);break;}
else{key=find2(head,kv);key->next=hdelete(key->next);}
case 3:
printf("Enter key value");
scanf("%d",&kv);
key=find(head,kv);
if(key==Null)
c=8;
else if(key->next==Null)
break;
key->next=hdelete(key->next); break;
case 4:
point=head;
if(point==Null)
printf("List is Empty");
else if(point->next==Null)
head=Null;
else
{
while((point->next->next)!=Null)
{
point=point->next;
}
point->next=hdelete(point->next);
}break;
case 8:
printf("Value Not Found");
default :
printf("Invalid Option \n Choice should be between 0 and 4\n");
break;

}
return(head);
}


/*Fuction to delete node*/
node* hdelete(node* point)
{
node* temp;
temp=point;
point=point->next;
free(temp);
return(point);
}

/*Fuction to access link-list*/
void access(node* point)
{
int kv,c;
printf("1.Display Whole link list?[yes=1,No=0]");
scanf("%d",&c);
if(c==1)
{
display(point);return;
}
else
printf("Enter key value");
scanf("%d",&kv);
while(kv!=-99)
{
if(point->val==kv)
{
printf("%d found",point->val);
kv=-99;
}
else if(point->next!=Null)
point=point->next;
else
{
printf("not found");
kv=-99;
}
}
}

/* Fuction to get size of Link-list*/
int size(node* head)
{
int s=0;
while(head!=Null)
{
head=head->next;s++;
}
return(s);
}


/*Fuctions to search node in link-list*/
node* find(node* point,int kv)
{
if(point->next->val==kv)
return(point);
else if(point->next->next!=Null)
return(find(point->next,kv));
else
printf("not found");return(Null);
}

node* find1(node* point,int kv)
{
if(point->val==kv)
return(point);
else if(point->next!=Null)
return(find1(point->next,kv));
else
return(Null);
}

node* find2(node* point,int kv)
{
if(point->next->next->val==kv)
return(point);
else if(point->next->next->next!=Null)
return(find2(point->next,kv));
else
printf("not found");return(Null);
}

No comments:

Post a Comment