Write the programs for Linked List (Insertion and Deletion)
operations
Answer Posted / indrajeet kumar ray
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
typedef struct linearlinkedlist
{
int data;
struct linearlinkedlist *next;
}node;
void createempty(node **head)
{
*head=NULL;
}
void insertbegin(int element,node **head)
{
node *ptr;
ptr=(node*)malloc(sizeof(node));
ptr->data=element;
if(*head==NULL)
{
ptr->next=NULL;
*head=ptr;
}
else
{
ptr->next=*head;
*head=ptr;
}
}
void insertend(int element,node **head)
{
node *ptr,*loc;
ptr=(node*)malloc(sizeof(node));
ptr->data=element;
if(*head==NULL)
{
ptr->next=NULL;
*head=ptr;
}
else
{
loc=*head;
while(loc->next!=NULL)
{
loc=loc->next;
}
ptr->next=NULL;
loc->next=ptr;
}
}
void insertbefore(int element,int before,node **head)
{
node *ptr,*loc;
ptr=(node *)malloc(sizeof(node));
ptr->data=element;
if(*head==NULL)
{
printf("Given element is not present.");
return;
}
else
{
loc=*head;
if(loc->data==before)
{
ptr->next=*head;
*head=ptr;
}
else
{
while((loc->next->data!=before)&&(loc->next!=NULL))
{
loc=loc->next;
}
if(loc->next==NULL)
{
printf("Given element is not present.");
return;
}
else
{
ptr->next=loc->next;
loc->next=ptr;
}
}
}
}
void insertafter(int element,int after,node **head)
{
node *ptr,*loc;
ptr=(node *)malloc(sizeof(node));
ptr->data=element;
if(*head==NULL)
{
printf("Given element is not present.");
return;
}
else
{
loc=*head;
while((loc->data!=after)&&(loc->next!=NULL))
{
loc=loc->next;
}
if(loc->data==after)
{
ptr->next=loc->next;
loc->next=ptr;
}
else
{
printf("Given element is not present.");
return;
}
}
}
void insertatpos(int element,int position,node **head)
{
int i,count=0;
node *ptr,*loc;
ptr=(node *)malloc(sizeof(node));
ptr->data=element;
if(position==1)
{
ptr->next=*head;
*head=ptr;
}
else
{
if(*head==NULL)
{
printf("Insertion can't be done.");
return;
}
else
{
loc=*head;
count=1;
while(loc->next!=NULL)
{
loc=loc->next;
count=count+1;
}
if(position>=count+2)
{
printf("Insertion can't be done.");
return;
}
else
{
loc=*head;
for(i=1;i<=position-2;i++)
{
loc=loc->next;
}
ptr->next=loc->next;
loc->next=ptr;
}
}
}
}
void deletebegin(node **head)
{
int x;
node *temp;
temp=*head;
if(*head==NULL)
{
printf("There is no element in the list.");
return;
}
else
{
*head=(*head)->next;
x=temp->data;
free(temp);
printf("%d is deleted successfully.",x);
return;
}
}
void deleteend(node **head)
{
int x;
node *temp,*loc;
if(*head==NULL)
{
printf("There is no element in the list.");
return;
}
else
{
temp=*head;
if((*head)->next==NULL)
{
*head=NULL;
x=temp->data;
free (temp);
printf("%d is deleted successfully.",x);
}
else
{
while(temp->next!=NULL)
{
loc=temp;
temp=temp->next;
}
loc->next=NULL;
x=temp->data;
free(temp);
printf("%d is deleted successfully.",x);
}
}
}
void deleteelement(int element,node **head)
{
int x;
node *temp,*loc;
if(*head==NULL)
{
printf("There is no element to delete.");
return;
}
else
{
temp=*head;
if((*head)->data==element)
{
*head=(*head)->next;
x=temp->data;
free(temp);
printf("%d is deleted successfully.",x);
}
else
{
while((temp->data!=element)&&(temp->next!=NULL))
{
loc=temp;
temp=temp->next;
}
if(temp->data!=element)
{
printf("Given element is not present.");
return;
}
else
{
loc->next=temp->next;
x=temp->data;
free(temp);
printf("%d is deleted successfully.",x);
}
}
}
}
void deleteatpos(int position,node **head)
{
int i,count=0,x;
node *temp,*loc;
if(*head==NULL)
{
printf("There is no element to delete.");
return;
}
else
{
temp=*head;
count=1;
while(temp->next!=NULL)
{
temp=temp->next;
count=count+1;
}
if(position>count)
{
printf("Position is not present.");
return;
}
else
{
temp=*head;
for(i=1;i<=position-1;i++)
{
loc=temp;
temp=temp->next;
}
loc->next=temp->next;
x=temp->data;
free(temp);
printf("%d is deleted successfully.",x);
}
}
}
void peep(node **head)
{
node *loc;
if(*head==NULL)
{
printf("No element to Display.");
return;
}
else
{
loc=*head;
printf("The Elements are : \n\n");
while(loc->next!=NULL)
{
printf("%d\t",loc->data);
loc=loc->next;
}
printf("%d",loc->data);
}
}
void main()
{
int choice=0,element,before,after,position;
node *head;
createempty(&*head);
while(choice!=11)
{
clrscr();
printf(" 1.Insert at Begin \n 2.Insert at End \n 3.Insert
before an Element \n 4.Insert after an Element \n 5.Insert
at given Position \n 6.Delete from Begin \n 7.Delete from
End \n 8.Delete a Given Element \n 9.Delete from a Given
Position \n 10.Peep \n 11.Exit \n\n Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter the element : ");
scanf("%d",&element);
insertbegin(element,&*head);
peep(&*head);
break;
case 2:
printf("Enter the element : ");
scanf("%d",&element);
insertend(element,&*head);
peep(&*head);
break;
case 3:
printf("Enter before which you want to insert: ");
scanf("%d",&before);
printf("Enter the element : ");
scanf("%d",&element);
insertbefore(element,before,&*head);
peep(&*head);
break;
case 4:
printf("Enter after which u want to insert: ");
scanf("%d",&after);
printf("Enter the element : ");
scanf("%d",&element);
insertafter(element,after,&*head);
peep(&*head);
break;
case 5:
printf("Enter the position : ");
scanf("%d",&position);
printf("Enter the element : ");
scanf("%d",&element);
insertatpos(element,position,&*head);
peep(&*head);
break;
case 6:
deletebegin(&*head);
peep(&*head);
break;
case 7:
deleteend(&*head);
peep(&*head);
break;
case 8:
printf("Enter the element : ");
scanf("%d",&element);
deleteelement(element,&*head);
peep(&*head);
break;
case 9:
printf("Enter the position : ");
scanf("%d",&position);
deleteatpos(position,&*head);
peep(&*head);
break;
case 10:
peep(&*head);
break;
case 11:
printf("Press any key to exit...");
break;
default:
printf("Wrong Choice!");
}
getch();
}
}
Is This Answer Correct ? | 11 Yes | 13 No |
Post New Answer View All Answers
How do you explain bubble sort?
What is 1d array?
How can you insert a node at the end of linked list?
List out the advantages of using a linked list?
Are lists mutable?
Write a code for dynamic allocation of array.
What do you mean by level of the tree?
Explain the Stack
Write an algorithm through which the inserting and deleting of elements can take place in circular queue?
What is quick sort example?
Can we use ordered set for performing binary search?
Can a stack be described as a pointer? Explain.
Write the postfix form of the expression: (a + b) * (c - d)
Define double linked list?
Define a queue?