Link List Opearations
many opearations on link list :-
1] singly link list~
2] doubly link list~
3] Singly circular link list~
4]Doubly Circular link list~
1.create list
2.display
3.search
4.sort
5.insert at first position
6.insert at last position
7.insert at any position
8.delete from any position
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *next;
}NODE;
int creatlist(NODE * head)
{
printf("\ncreatlist:\n");
NODE *newnode,*last;
int i,n;
printf("how many nodes ur create:");
scanf("%d",&n);
last=head;
for(i=0;i<n;i++)
{
newnode=(NODE *)malloc(sizeof(NODE));
printf("\nenter the data:");
scanf("%d",&newnode->data);
newnode->next=NULL;
last->next=newnode;
last=newnode;
}
}
int display(NODE *head)
{
printf("\ndisplay:\n");
NODE *temp;
for(temp=head->next;temp!=NULL;temp=temp->next)
{
printf("%d\n",temp->data);
}
}
int search(NODE *head,int key)
{
printf("\nsearching:\n");
NODE *temp;
int pos;
for(temp=head->next,pos=1;temp!=NULL;temp=temp->next)
{
if(temp->data==key)
{
return pos;
}
pos++;
}
return -1;
}
int sort(NODE *head)
{
printf("\nsorting:\n");
NODE *temp,*temp1;
for(temp=head->next;temp!=NULL;temp=temp->next)
for(temp1=temp->next;temp1!=NULL;temp1=temp1->next)
{
if(temp->data>temp1->data)
{
int num=temp->data;
temp->data=temp1->data;
temp1->data=num;
}
}
}
int insertbegin(NODE *head,int n)
{
NODE *newnode;
newnode=(NODE *)malloc(sizeof(NODE));
newnode->data=n;
newnode->next=head->next;
head->next=newnode;
}
int insertlast(NODE *head,int n)
{
NODE *newnode,*temp;
newnode=(NODE *)malloc(sizeof(NODE));
newnode->data=n;
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
newnode->next=NULL;
temp->next=newnode;
}
int insert_any_position(NODE *head,int n,int p)
{
NODE *temp,*newnode;
int i;
newnode=(NODE *)malloc(sizeof(NODE));
for(temp=head,i=1;(temp!=NULL)&&(i<=p-1);i++ )
{
temp=temp->next;
if(temp==NULL)
printf("\nur input position is out of range!!!");
}
newnode->next=temp->next;
newnode->data=n;
temp->next=newnode;
}
int delete_from_any_position(NODE *head,int p)
{
NODE *temp,*nextnode;
int i=1;
temp=head;
while(i<=p-1)
{
if(temp->next==NULL)
printf("\nYour input position is out of range!!!");
temp=temp->next;
i++;
}
nextnode=temp->next;
temp->next=nextnode->next;
free(nextnode);
}
int main ()
{
NODE *head;
int key,ans,choice,n,p;
head=(NODE *)malloc(sizeof(NODE));
do
{
printf("\nYOUR MENUCARD:");
printf("\n1:Creatlist:");
printf("\n2:Display:");
printf("\n3:Search:");
printf("\n4:Sort:");
printf("\n5:Insert at the begin:");
printf("\n6:Insert at the LAST:");
printf("\n7:Insert at ANY POSITION:");
printf("\n8:Delete From ANY POSITION:");
printf("\n~~~Pls Select ur Journey~~~:");
scanf("%d",&choice);
switch(choice)
{
case 1: creatlist(head);
break;
case 2: display(head);
break;
case 3: printf("enter the key:");
scanf("%d",&key);
ans=search(head,key);
if(ans==-1)
printf("oh key not found");
else
printf("\nkey found at position %d\n",ans);
break;
case 4: sort(head);
display(head);
break;
case 5:printf("\nPls enter the element which is insert to BEGIN:");
scanf("%d",&n);
insertbegin(head,n);
display(head);
break;
case 6:printf("\npls enter the element which is insert to LAST:");
scanf("%d",&n);
insertlast(head,n);
display(head);
break;
case 7:printf("\nenter the element & position which is insert at any position:");
scanf("%d%d",&n,&p);
insert_any_position(head,n,p);
display(head);
break;
case 8:printf("\nEnter the position to be deleted:");
scanf("%d",&p);
delete_from_any_position(head,p);
display(head);
break;
default:
printf("\nOhh ur input is wrong!!!");
}
} while (choice<=10);
return 0;
}
2] doubly link list:-
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *next,*prev;
}NODE;
int creatlist(NODE * head)
{
printf("\ncreatlist:\n");
NODE *newnode,*last;
int i,n;
printf("how many nodes ur create:");
scanf("%d",&n);
last=head;
for(i=0;i<n;i++)
{
newnode=(NODE *)malloc(sizeof(NODE));
newnode->prev=newnode->next=NULL;
printf("\nenter the data:");
scanf("%d",&newnode->data);
last->next=newnode;
newnode->prev=last;
last=newnode;
}
}
int display(NODE *head)
{
printf("\ndisplay:\n");
NODE *temp;
for(temp=head->next;temp!=NULL;temp=temp->next)
{
printf("%d\n",temp->data);
}
}
int search(NODE *head, int key)
{
NODE *temp;
int pos;
for(temp=head->next,pos=1;temp!=NULL;temp=temp->next,pos++)
{ if(temp->data==key)
return pos;
}
return -1;
}
int sort(NODE *head)
{
printf("\nsorting:\n");
NODE *temp,*temp1;
for(temp=head->next;temp!=NULL;temp=temp->next)
for(temp1=temp->next;temp1!=NULL;temp1=temp1->next)
{
if(temp->data>temp1->data)
{
int num=temp->data;
temp->data=temp1->data;
temp1->data=num;
}
}
}
int insertbegin(NODE *head,int n)
{
NODE *newnode,*temp;
newnode=(NODE *)malloc(sizeof(NODE));
newnode->next=newnode->prev=NULL;
newnode->data=n;
temp=head->next;
temp->prev=newnode;
newnode->next=temp;
newnode->prev=head;
head->next=newnode;
}
int insertlast(NODE *head,int n)
{
NODE *newnode,*temp;
newnode=(NODE *)malloc(sizeof(NODE));
newnode->next=newnode->prev=NULL;
newnode->data=n;
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
newnode->next=NULL;
temp->next=newnode;
newnode->prev=temp;
}
int insert_any_position(NODE *head,int n,int p)
{
NODE *newnode,*temp;
int i;
newnode=(NODE *)malloc(sizeof(NODE));
for(temp=head,i=1;(temp!=NULL)&&(i<=p-1);i++)
{
temp=temp->next;
if(temp==NULL)
printf("\nposition is out of range");
}
newnode->next=newnode->prev=NULL;
newnode->data=n;
newnode->next=temp->next;
newnode->prev=temp;
temp->next=newnode;
}
int delete_from_any_position(NODE *head,int p)
{
NODE *temp,*temp1;
int i=1;
temp=head;
while(i<=p-1)
{
if(temp->next==NULL)
printf("\nYour input position is out of range!!!");
temp=temp->next;
i++;
}
temp1=temp->next;
temp->next=temp1->next;
if(temp1->next!=NULL)
temp1->next->prev=temp;
free(temp1);
}
int delete_by_value(NODE *head,int n)
{
NODE *temp,*temp1;
for(temp=head;temp!=NULL;temp=temp->next)
{
if(temp->next->data==n)
{
temp1=temp->next;
temp->next=temp1->next;
if(temp1->next!=NULL)
temp1->next->prev=temp;
free(temp1);
return;
}
printf("\nelement not found!");
}
}
int main ()
{
NODE *head;
int key,ans,choice,n,p;
head=(NODE *)malloc(sizeof(NODE));
do
{
printf("\nYOUR MENUCARD:");
printf("\n1:Creatlist:");
printf("\n2:Display:");
printf("\n3:Search:");
printf("\n4:Sort:");
printf("\n5:Insert at the begin:");
printf("\n6:Insert at the LAST:");
printf("\n7:Insert at ANY POSITION:");
printf("\n8:Delete From ANY POSITION:");
printf("\n9:delete by value:");
printf("\n~~~Pls Select ur Journey~~~:");
scanf("%d",&choice);
switch(choice)
{
case 1: creatlist(head);
break;
case 2: display(head);
break;
case 3: printf("enter the key:");
scanf("%d",&key);
ans=search(head,key);
if(ans==-1)
printf("oh key not found");
else
printf("\nkey found at position %d\n",ans);
break;
case 4: sort(head);
display(head);
break;
case 5:printf("\nPls enter the element which is insert to BEGIN:");
scanf("%d",&n);
insertbegin(head,n);
display(head);
break;
case 6:printf("\npls enter the element which is insert to LAST:");
scanf("%d",&n);
insertlast(head,n);
display(head);
break;
case 7:printf("\nenter the element & position which is insert at any position:");
scanf("%d%d",&n,&p);
insert_any_position(head,n,p);
display(head);
break;
case 8:printf("\nEnter the position to be deleted:");
scanf("%d",&p);
delete_from_any_position(head,p);
display(head);
break;
case 9:printf("\nenter the value to be deleted:");
scanf("%d",&n);
delete_by_value(head,n);
display(head);
break;
default:
printf("\nOhh ur input is wrong!!!");
}
} while (choice<=10);
return 0;
}
3] singly circular link list:-
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *next;
}NODE;
int creatlist(NODE * head)
{
printf("\ncreatlist:\n");
NODE *newnode,*last;
int i,n;
printf("how many nodes ur create:");
scanf("%d",&n);
last=head;
for(i=0;i<n;i++)
{
newnode=(NODE *)malloc(sizeof(NODE));
printf("\nenter the data:");
scanf("%d",&newnode->data);
newnode->next=head;
last->next=newnode;
last=newnode;
}
}
int display(NODE *head)
{
printf("\ndisplay:\n");
NODE *temp;
for(temp=head->next;temp!=head;temp=temp->next)
{
printf("%d\n",temp->data);
}
}
int search(NODE *head,int key)
{
printf("\nsearching:\n");
NODE *temp;
int pos;
for(temp=head->next,pos=1;temp!=head;temp=temp->next)
{
if(temp->data==key)
{
return pos;
}
pos++;
}
return -1;
}
int sort(NODE *head)
{
printf("\nsorting:\n");
NODE *temp,*temp1;
for(temp=head->next;temp!=head;temp=temp->next)
for(temp1=temp->next;temp1!=head;temp1=temp1->next)
{
if(temp->data>temp1->data)
{
int num=temp->data;
temp->data=temp1->data;
temp1->data=num;
}
}
}
int insertbegin(NODE *head,int n)
{
NODE *newnode;
newnode=(NODE *)malloc(sizeof(NODE));
newnode->data=n;
newnode->next=head->next;
head->next=newnode;
}
int insertlast(NODE *head,int n)
{
NODE *newnode,*temp;
newnode=(NODE *)malloc(sizeof(NODE));
newnode->data=n;
temp=head;
while(temp->next!=head)
{
temp=temp->next;
}
newnode->next=head;
temp->next=newnode;
}
int insert_any_position(NODE *head,int n,int p)
{
NODE *temp,*newnode;
int i;
newnode=(NODE *)malloc(sizeof(NODE));
for(temp=head,i=1;(temp->next!=head)&&(i<=p-1);i++ )
{
temp=temp->next;
if(temp==head)
printf("\nur input position is out of range!!!");
}
if(temp->next==head)
printf("\nposition is out of range!!!");
else
{
newnode->next=temp->next;
newnode->data=n;
temp->next=newnode;
}
}
int delete_from_any_position(NODE *head,int p)
{
NODE *temp,*nextnode;
int i=1;
temp=head;
while(i<=p-1)
{
if(temp->next==head)
{
printf("\nYour input position is out of range!!!");
break;
}
temp=temp->next;
i++;
}
if(temp->next==head)
printf("\nYour input position is out of range...");
else
{
nextnode=temp->next;
temp->next=nextnode->next;
free(nextnode);
}
}
int delete_by_value(NODE *head,int n)
{
NODE *temp,*temp1;
for(temp=head;temp->next!=head;temp=temp->next)
{
if(temp->next->data==n)
{
temp1=temp->next;
temp->next=temp1->next;
free(temp1);
return;
}
}
printf("\nelement not found!");
}
int main ()
{
NODE *head;
int key,ans,choice,n,p;
head=(NODE *)malloc(sizeof(NODE));
do
{
printf("\nYOUR MENUCARD:");
printf("\n1:Creatlist:");
printf("\n2:Display:");
printf("\n3:Search:");
printf("\n4:Sort:");
printf("\n5:Insert at the begin:");
printf("\n6:Insert at the LAST:");
printf("\n7:Insert at ANY POSITION:");
printf("\n8:Delete From ANY POSITION:");
printf("\n9:delete by value:");
printf("\n~~~Pls Select ur Journey~~~:");
scanf("%d",&choice);
switch(choice)
{
case 1: creatlist(head);
break;
case 2: display(head);
break;
case 3: printf("enter the key:");
scanf("%d",&key);
ans=search(head,key);
if(ans==-1)
printf("oh key not found");
else
printf("\nkey found at position %d\n",ans);
break;
case 4: sort(head);
display(head);
break;
case 5:printf("\nPls enter the element which is insert to BEGIN:");
scanf("%d",&n);
insertbegin(head,n);
display(head);
break;
case 6:printf("\npls enter the element which is insert to LAST:");
scanf("%d",&n);
insertlast(head,n);
display(head);
break;
case 7:printf("\nenter the element & position which is insert at any position:");
scanf("%d%d",&n,&p);
insert_any_position(head,n,p);
display(head);
break;
case 8:printf("\nEnter the position to be deleted:");
scanf("%d",&p);
delete_from_any_position(head,p);
display(head);
break;
case 9:printf("\nenter the value to be deleted:");
scanf("%d",&n);
delete_by_value(head,n);
display(head);
break;
default:
printf("\nOhh ur input is wrong!!!");
}
} while (choice<=10);
return 0;
}
4]doubly circular link list:-
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *next,*prev;
}NODE;
int creatlist(NODE * head)
{
printf("\ncreatlist:\n");
NODE *newnode,*last;
int i,n;
printf("how many nodes ur create:");
scanf("%d",&n);
last=head;
for(i=0;i<n;i++)
{
newnode=(NODE *)malloc(sizeof(NODE));
newnode->prev=last;
newnode->next=head;
printf("\nenter the data:");
scanf("%d",&newnode->data);
last->next=newnode;
last=newnode;
head->prev=newnode;
}
}
int display(NODE *head)
{
printf("\ndisplay:\n");
NODE *temp;
for(temp=head->next;temp!=head;temp=temp->next)
{
printf("%d\n",temp->data);
}
}
int search(NODE *head, int key)
{
NODE *temp;
int pos;
for(temp=head->next,pos=1;temp!=head;temp=temp->next,pos++)
{ if(temp->data==key)
return pos;
}
return -1;
}
int sort(NODE *head)
{
printf("\nsorting:\n");
NODE *temp,*temp1;
for(temp=head->next;temp!=head;temp=temp->next)
for(temp1=temp->next;temp1!=head;temp1=temp1->next)
{
if(temp->data>temp1->data)
{
int num=temp->data;
temp->data=temp1->data;
temp1->data=num;
}
}
}
int insertbegin(NODE *head,int n)
{
NODE *newnode,*temp;
newnode=(NODE *)malloc(sizeof(NODE));
newnode->next=newnode->prev=head;
newnode->data=n;
temp=head->next;
temp->prev=newnode;
newnode->next=temp;
newnode->prev=head;
head->next=newnode;
}
int insertlast(NODE *head,int n)
{
NODE *newnode,*temp;
newnode=(NODE *)malloc(sizeof(NODE));
newnode->next=newnode->prev=head;
newnode->data=n;
temp=head;
while(temp->next!=head)
{
temp=temp->next;
}
newnode->next=head;
temp->next=newnode;
newnode->prev=temp;
}
int insert_any_position(NODE *head,int n,int p)
{
NODE *temp,*newnode;
int i;
temp=head;
newnode=(NODE *)malloc(sizeof(NODE));
for(temp=head,i=1;(temp->next!=head)&&(i<=p-1);i++ )
{
if(temp->next==head)
printf("\nur input position is out of range!!!");
temp=temp->next;
}
if(temp->next==head)
printf("\nposition is out of range!!!");
else
{
newnode->data=n;
newnode->prev=temp;
newnode->next=temp->next;
temp->next->prev=newnode;
temp->next=newnode;
}
}
int delete_from_any_position(NODE *head,int p)
{
NODE *temp,*nextnode;
int i=1;
temp=head;
while(i<=p-1)
{
if(temp->next==head)
{
printf("\nYour input position is out of range!!!");
break;
}
else
{
temp=temp->next;
i++;
}
}
if(temp->next==head)
printf("\nYour input position is out of range...");
else
{
nextnode=temp->next;
temp->next=nextnode->next;
nextnode->next->prev=temp;
free(nextnode);
}
}
int delete_by_value(NODE *head,int n)
{
NODE *temp,*temp1;
for(temp=head;temp->next!=head;temp=temp->next)
{
if(temp->next->data==n)
{
temp1=temp->next;
temp->next=temp1->next;
if(temp1->next!=head)
temp1->next->prev=temp;
free(temp1);
}
printf("\nelement not found!");
}
printf("\nelement not found!!");
}
int main ()
{
NODE *head;
int key,ans,choice,n,p;
head=(NODE *)malloc(sizeof(NODE));
do
{
printf("\nYOUR MENUCARD:");
printf("\n1:Creatlist:");
printf("\n2:Display:");
printf("\n3:Search:");
printf("\n4:Sort:");
printf("\n5:Insert at the begin:");
printf("\n6:Insert at the LAST:");
printf("\n7:Insert at ANY POSITION:");
printf("\n8:Delete From ANY POSITION:");
printf("\n9:delete by value:");
printf("\n~~~Pls Select ur Journey~~~:");
scanf("%d",&choice);
switch(choice)
{
case 1: creatlist(head);
break;
case 2: display(head);
break;
case 3: printf("enter the key:");
scanf("%d",&key);
ans=search(head,key);
if(ans==-1)
printf("oh key not found");
else
printf("\nkey found at position %d\n",ans);
break;
case 4: sort(head);
display(head);
break;
case 5:printf("\nPls enter the element which is insert to BEGIN:");
scanf("%d",&n);
insertbegin(head,n);
display(head);
break;
case 6:printf("\npls enter the element which is insert to LAST:");
scanf("%d",&n);
insertlast(head,n);
display(head);
break;
case 7:printf("\nenter the element & position which is insert at any position:");
scanf("%d%d",&n,&p);
insert_any_position(head,n,p);
display(head);
break;
case 8:printf("\nEnter the position to be deleted:");
scanf("%d",&p);
delete_from_any_position(head,p);
display(head);
break;
case 9:printf("\nenter the value to be deleted:");
scanf("%d",&n);
delete_by_value(head,n);
display(head);
break;
default:
printf("\nOhh ur input is wrong!!!");
}
} while (choice<=10);
return 0;
}
Comments
Post a Comment
hey