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 package is arraylist?

555


Can a null element added to a treeset or hashset?

458


State the demerits of linked representation of binary trees?

533


What is a multidimensional array?

526


What is worst case complexity algorithm?

471






How do I start preparing for placement?

488


What is 2 dimensional linked list?

580


How to find the missing element in integer array of 1 to 7?

520


What is bubble insertion selection sort?

476


Write any two principles of gradation of structures.

577


Which is faster hashmap or linkedhashmap?

522


Write a data structure for a queue.

561


In what areas do data structures applied?

496


State the properties of a binary tree?

562


Does concat mutate array?

495