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 do you mean by free pool?

506


On clicking a node in a tree, all the adjacent edges are turned on. Calculate min of clicks such that all the edges are turned on.

599


How do I sort a hashmap key?

493


Does linkedhashset allow null values?

492


What are the types of sorting?

467






What is difference between while and do while?

457


Explain linked list in short.

515


What is the difference between array and stack?

528


Which is better arraylist or linkedlist?

451


Is array a linked list?

471


Write program for Quick sort ?

589


What is the difference between ienumerable and list?

466


Tell me what is quick sort?

539


What is dynamic data structure?

701


Calculate the address of a random element present in a 2d array, given base address as ba.

1061