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

How to excel in data structures and algorithms?

543


Why is reflection slower?

554


Write the importance of hashing?

611


Tell me why can't constant values be used to define an array's initial size

534


What is a stable algorithm?

484






How do you separate zeros from non-zeros in an array?

530


Explain the internal working of a hash map?

515


What is an example of an array?

566


What is data structure? Explain.

543


Does treemap sort on key or value?

493


What actions are performed when a function returns?

507


What is the best complexity of bubble sort?

476


What are common data structures?

599


What is the need of sorting?

520


Can we use Generics with the array?

603