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


Please Help Members By Posting Answers For Below Questions

What is data algorithm?

798


How can you insert a node to the beginning of a singly linked list?

725


What is list and its types?

664


Which is better merge sort or quick sort?

675


Write a Program for Insert in a sorted list

696


Can we store null in arraylist?

669


Can array store heterogeneous data?

643


What is an algorithm in coding?

621


Which interfaces are implemented by hashmap?

739


Which sorting algorithm is the slowest?

659


What are the applications of priority queues?

729


Which is the parent class of printerstatereasons class?

687


Are the expressions arr and &arr same for an array of integers?

790


How many parts are there in a declaration statement?

749


How do stacks work?

719