Write the programs for Linked List (Insertion and Deletion)
operations

Answer Posted / anitha

#include<stdio.h>
#include<conio.h>
#include<alloc.h>

struct node
{
int data;
struct node*next;
};
int main()
{
struct node *head=NULL,*temp,*tp;
int element,ch,target;
clrscr();
do
{
printf("1.insert@first\n2.@last\n3.@middle\n4.delete\n5.display\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
scanf("%d",&element);
temp=(struct node *)malloc(sizeof(struct node));
temp->data=element;
temp->next=NULL;
if(head==NULL)
{
head=temp;//creation of one node
}
else
{
temp->next=head; //insert @first
head=temp;
}

break;

case 2:

scanf("%d",&element);
temp=(struct node *)malloc(sizeof(struct node));
temp->data=element;
temp->next=NULL;
if(head==NULL)
{
head=temp;//creation of one node
}

else
{
tp=head;
while(tp->next!=NULL)
{
tp=tp->next; //traversing the list
}
tp->next=temp; //insert @last
}
break;
case 3:

scanf("%d%d",&element,&target);
temp=(struct node *)malloc(sizeof(struct node));
temp->data=element;
temp->next=NULL;
if(head==NULL)
{
head=temp;//creation of one node
}

else
{
tp=head;
while(tp->data!=target && tp!=NULL)
{
tp=tp->next; //traversing the list
}
temp->next=tp->next;
tp->next=temp; //insert @middle
}
break;
case 4:
scanf("%d",&target);

if(head==NULL)
{
printf("list is empty");
}

else
{
tp=head;
while(tp->data!=target && tp!=NULL)
{
temp=tp;
tp=tp->next; //traversing the list
}

if(tp!=NUlL)
{
if(tp==head)//delete @first
{
head=head->next;
free(tp);
}
else
{

temp->next=tp->next;
tp->next=temp; //delete @middle and @last
}
else
{
printf("target not found");
}
}
break;
}
}while(ch<=4);
getch();
}

Is This Answer Correct ?    2 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is list and its types?

655


How many sorting techniques are there?

645


How can we delete any specific node from the linked list?

704


Which is faster hashmap or hashset?

663


Is data structure a data type?

709


Mention one advantage and disadvantage of using quadratic probing?

787


What is the use of hashtable?

670


Define disjoint set adt?

711


What is variable size arrays?and why we use it?

795


What is difference between map and hashmap?

682


Is hashmap part of collection?

672


What is difference between arraylist and linkedlist?

756


Define a complete binary tree?

709


What is difference between hashmap and map?

693


What is doubly linked list?

700