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

Answer Posted / shailesh pratapwar

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

void insert(struct node **start,int num,int data);
void displayList(struct node *start);
void insertSL(struct node **s,int info);
void search(struct node *s,struct node **loc,struct node
**locp,int item);
void deleteMe(struct node **s,int item);

struct node
{
int info;
struct node *link;
};

void main()
{
struct node *start;
int req=0,i,num,data;
char choice;
clrscr();
start=NULL;
a: printf("Enter no. of items to insert : \n");
scanf("%d",&req);
for(i=0;i<req;i++)
{
printf("\nEnter element no. %d ",i);
scanf("%d",&num);
insert(&start,num,12000);
}

displayList(start);
printf("\nEnter element to search for ");
scanf("%d",&num);
deleteMe(&start,num);
displayList(start);

/* printf("\nWould you like to insert more .. 1/2");
choice=getch();

if(choice=='1')
goto a;
else
if(choice=='2')
{
printf("Enter data item to search for & item to insert :");
scanf("%d%d",&data,&num);
insert(&start,num,data);
}
displayList(start);
*/
getch();
}

void displayList(struct node *start)
{
printf("The entire list is :\n");
while(start!=NULL)
{
printf("%d ",start->info);
start=start->link;
}
}

void deleteMe(struct node **s,int item)
{
struct node *loc,*locp;
loc=NULL;
locp=NULL;
search(*s,&loc,&locp,item);
// printf("1st Element %d is at location
%u\n",locp->info,&(locp->info));
// printf("2nd Element %d is at location
%u\n",loc->info,&(loc->info));

if(loc==NULL && locp==NULL)
{
printf("list is already empty");
return;
}
else
if(locp==NULL)
{
printf("Deleting the first node");

*s=(*s)->link;
return;
}
else
{
printf("Deleting the second node");

locp->link=(loc->link);
return;
}

}


void search(struct node *s,struct node **loc,struct node
**locp,int item)
{
struct node *ptr=NULL,*save=NULL;

if(s==NULL)
{
*loc=NULL;
*locp=NULL;
return;
}
if(s->info==item)
{
*loc=s;
*locp=NULL;
return;
}
else
{
save=s;
ptr=s->link;
while(ptr!=NULL)
{
if(ptr->info==item)
{
*loc=ptr;
*locp=save;
return;
}
else
{

save=ptr;
*locp=save;
ptr=ptr->link;
}

}
*loc=NULL;

}


}


struct node *getNewNode()
{
struct node *newNode=malloc(sizeof(struct node));
newNode->link=NULL;
return newNode;
}



void insertSL(struct node **s,int info)
{
struct node *newNode,*ptr=NULL,*prev=NULL;
newNode=getNewNode();
if(newNode==NULL)
{
printf("Not enough space :-(");
exit(1);
}
else
{
newNode->info=info;

if(*s==NULL)
{
*s=newNode;
// printf("First case");
return;
}
else
if(info<((*s)->info))
{
// printf("second case");
newNode->link=*s;
*s=newNode;
return;
}

prev=*s;
ptr=(*s)->link;

while(ptr->link!=NULL && (ptr->info)<info)
{
prev=ptr;
ptr=ptr->link;
}
// printf("Third case");
newNode->link=prev->link;
prev->link=newNode;
return;
}
}


void insert(struct node **start,int num,int data)
{
struct node *newNode,*ptr,*save;
newNode=malloc(sizeof(struct node));

if(newNode==NULL)
{
printf("It's memory overflow for gods sake ... Cant move
forward.");
getch();
exit(1);
}
else //Yiepi, i got the new empty node.
{
newNode->info=num;
//Let's insert this item at the end of the list.

if(*start==NULL)
{
newNode->link=NULL;
*start=newNode;
printf("%u",&(*start)->info);
return;
}
else
{ ptr=*start;
while(ptr->link!=NULL && ptr->info!=data)
{
ptr=ptr->link;
}

newNode->link=ptr->link;
ptr->link=newNode;


}

}
}

Is This Answer Correct ?    30 Yes 22 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

List the data structures which are used in hierarchical data model.

512


How do you find the depth of a binary tree?

555


How do you find the height of a binary tree?

474


How would you reverse the characters of an array?

524


Does hashmap maintain insertion order?

490






Define double linked list?

562


Differentiate between list and map.

588


What is hash value of a string?

467


Is it possible to insert different type of elements in a stack? How?

525


Why is the isempty() member method called?

558


Which data structures are used in bfs and dfs algorithm?

549


Does arraylist maintain insertion order?

460


Draw a binary Tree for the expression : A * B - (C + D) * (P / Q)

952


What is the use of substring?

457


write a program to accept name & sort them?

508