Write the programs for Linked List (Insertion and Deletion)
operations
Answers were Sorted based on User's Feedback
/*OPERATIONS ON SINGLY LINKED LIST*/
#include<stdio.h>
#include<conio.h>
struct link
{
int item;
struct link *next;
};
typedef struct link node;
void addfirst();
void addlast();
void addmid();
void delfirst();
void dellast();
void delmid();
void display();
node *head=NULL;
void main()
{
int ch;
clrscr();
do
{
printf("\nSINGLY LINKED LIST OPERATIONS\n");
printf("\n1.Addfirst\n2.AddMid\n3.AddLast\n4.DeleteFirst\n5.DeleteMiddle\n6.DeleteLast\n7.Display\n8.Exit\n");
printf("Enter your option:\t");
scanf("%d",&ch);
switch(ch)
{
case 1:
addfirst();
display();
break;
case 2:
addmid();
display();
break;
case 3:
addlast();
display();
break;
case 4:
delfirst();
display();
break;
case 5:
delmid();
display();
break;
case 6:
dellast();
display();
break;
case 7:
display();
break;
case 8:
exit(0);
break;
default:
printf("Invalid Choice\n");
}
}
while(ch<=8);
getch();
}
void addfirst()
{
node *temp;
temp=(node *)malloc(sizeof(node));
printf("Enter the data....\t");
scanf("%d",&temp->item);
temp->next=head;
head=temp;
}
void addmid()
{
int i=1,pos;
node *cur=head,*temp;
printf("\nEnter the position\t");
scanf("%d",&pos);
while(pos!=i+1&&cur!=NULL)
{
cur=cur->next;
i++;
}
if(pos==i+1)
{
temp=(node *)malloc(sizeof(node));
printf("Enter the data...");
scanf("%d",&temp->item);
temp->next=cur->next;
cur->next=temp;
}
}
void addlast()
{
node *temp,*cur=head;
temp=(node *)malloc(sizeof(node));
printf("\nEnter the data....");
scanf("%d",&temp->item);
while(cur->next!=NULL)
{
cur=cur->next;
}
temp->next=cur->next;
cur->next=temp;
}
void delfirst()
{
node *temp=head;
head=head->next;
printf("Deleted item is %d\n",temp->item);
free(temp);
}
void delmid()
{
int i=1,pos;
node *cur=head,*temp;
printf("Enter the position to be deleted\t");
scanf("%d",&pos);
while(pos!=i+1&&cur->next!=NULL)
{
cur=cur->next;
i++;
}
if(pos==i+1)
{
temp=cur->next;
cur->next=temp->next;
printf("Deleted item is %d\n",temp->item);
free(temp);
}
}
void dellast()
{
node *temp,*cur=head;
while(cur->next->next!=NULL)
{
cur=cur->next;
}
temp=cur->next;
cur->next=NULL;
printf("Deleted item is %d\n",temp->item);
free(temp);
}
void display()
{
node *cur=head;
printf("\nHead->");
while(cur!=NULL)
{
printf("\t%d",cur->item);
cur=cur->next;
}
printf("<-NULL\n");
}
Is This Answer Correct ? | 247 Yes | 53 No |
Answer / 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 |
Answer / bhargav
In Answer 1 i think there is error in dellast()
void dellast()
{
node *temp,*cur=head;
while(cur->next->next!=NULL) -------> error
{
cur=cur->next;
}
temp=cur->next;
cur->next=NULL;
printf("Deleted item is %d\n",temp->item);
free(temp);
}
Is This Answer Correct ? | 7 Yes | 0 No |
Answer / priya
dimag maran naal khush nhi hona program nhi aaune buhut
difficult aa bye bye
Is This Answer Correct ? | 15 Yes | 13 No |
Answer / 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 |
Answer / r.manju
how to deleted specific node in a linked list with example
program?
Is This Answer Correct ? | 1 Yes | 1 No |
Answer / 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 |
Answer / vikram_tp
import java.util.*;
class Node
{
int n;
Node nx;
Node()
{
nx=null;
}
}
class Linklist
{
Node f,l;
Linklist()
{
f=l=null;
}
void inserte(int x)
{
Node nr=new Node();
nr.nx=null;
nr.n=x;
if(f==null)
f=nr;
else
l.nx=nr;
l=nr;
}
void insertf(int x)
{
Node nr=new Node();
nr.nx=f;
nr.n=x;
f=nr;
}
void insertb(int x, int c)
{
Node nr=new Node();
nr.n=x;
Node p;
p=f;
while(p.n!=c)
p=p.nx;
nr.nx=p.nx;
p.nx=nr;
}
void delete(int x)
{
Node p,q;
p=q=f;
while(p!=null&&p.n!=x)
{
q=p;
p=p.nx;
}
if(p!=null)
{
if(p==f)
f=f.nx;
else
if(p==l)
{
q.nx=null;
l=q;
}
else
q.nx=p.nx;
}
else
System.out.println("element not present ");
}
void search(int x)
{
Node p;
p=f;
while(p!=null&&p.n!=x)
p=p.nx;
if(p==null)
System.out.println("element not found");
else
System.out.println("element found");
}
void display()
{
Node p;
p=f;
if(p==null)
{
System.out.println("Link list is empty ");
return;
}
while(p!=null)
{
System.out.println(p.n);
p=p.nx;
}
}
}
public class Link
{
public static void main(String args[])
{
int ch1,ch2,n;
Linklist L=new Linklist();
Scanner bv=new Scanner(System.in);
do
{
System.out.println(" menu\n1. insert a node\n2. Delete a node\n3. Display\n4. Search\n5. Exit");
System.out.print("enter ur choice : ");
ch1=bv.nextInt();
switch(ch1)
{
case 1:
System.out.print("enter the data : ");
n=bv.nextInt();
System.out.println(" menu\n1. insert at start\n2. insert at end\n3. insert in between");
System.out.print("enter ur choice : ");
ch2=bv.nextInt();
switch(ch2)
{
case 1: L.insertf(n);
break;
case 2: L.inserte(n);
break;
case 3:
System.out.print("enter the data after which you want to insert : ");
int c=bv.nextInt();
L.insertb(n,c);
break;
default:
System.out.println("wrong choice");
break;
}
break;
case 2:
System.out.print("enter the data : ");
n=bv.nextInt();
L.delete(n);
break;
case 3:
L.display();
break;
case 4:
System.out.print("enter the data : ");
n=bv.nextInt();
L.search(n);
break;
case 5 :break;
default :
System.out.println("wrong choice entered");
break;
}
}while(ch1!=5);
}
}
Is This Answer Correct ? | 10 Yes | 22 No |
List the types of rotations available in splay tree?
What is breadth first tree?
What is the use of sorting the data?
Why are b trees used?
Why quicksort is faster than merge sort?
Differentiate between hashmap and treemap.
Can you store different types in an array?
What are the disadvantages of linear list?
Write an algorithm to show the reverse of link list?
What do you mean by spanning tree?
What do you mean by Syntax Error
How many types of lists are there?