ALLInterview.com :: Home Page            
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
Google
   
 
Categories >> Software >> Operating Systems >> Data Structures
 
 


 

 
 Windows interview questions  Windows Interview Questions (339)
 Linux interview questions  Linux Interview Questions (449)
 Unix interview questions  Unix Interview Questions (455)
 Solaris interview questions  Solaris Interview Questions (781)
 RTOS interview questions  RTOS Interview Questions (43)
 Bulnex interview questions  Bulnex Interview Questions (4)
 Operating Systems General Concepts interview questions  Operating Systems General Concepts Interview Questions (261)
 Data Structures interview questions  Data Structures Interview Questions (66)
 Operating Systems AllOther interview questions  Operating Systems AllOther Interview Questions (47)
Question
Write the programs for Linked List (Insertion and Deletion)
operations
 Question Submitted By :: Guest
I also faced this Question!!     Rank Answer Posted By  
 
  Re: Write the programs for Linked List (Insertion and Deletion) operations
Answer
# 1
/*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 ?    225 Yes 45 No
Arnoldindia
 
  Re: Write the programs for Linked List (Insertion and Deletion) operations
Answer
# 2
true 
Is This Answer Correct ?    59 Yes 28 No
Naveen
 
 
 
  Re: Write the programs for Linked List (Insertion and Deletion) operations
Answer
# 3
#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 ?    27 Yes 20 No
Shailesh Pratapwar
 
  Re: Write the programs for Linked List (Insertion and Deletion) operations
Answer
# 4
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 ?    9 Yes 21 No
Vikram_tp
 
  Re: Write the programs for Linked List (Insertion and Deletion) operations
Answer
# 5
#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 10 No
Indrajeet Kumar Ray
 
  Re: Write the programs for Linked List (Insertion and Deletion) operations
Answer
# 6
dimag maran naal khush nhi hona program nhi aaune buhut
difficult aa bye bye
 
Is This Answer Correct ?    11 Yes 10 No
Priya
 
  Re: Write the programs for Linked List (Insertion and Deletion) operations
Answer
# 7
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 ?    0 Yes 0 No
Bhargav
 
  Re: Write the programs for Linked List (Insertion and Deletion) operations
Answer
# 8
how to deleted specific node in a linked list with example
program?
 
Is This Answer Correct ?    0 Yes 0 No
R.manju
 

 
 
 
Other Data Structures Interview Questions
 
  Question Asked @ Answers
 
Write programs for Bubble Sort, Quick sort Cognizent 14
Let the G be a graph with 100 vertices numbered 1 to 100 Two vertices i and j are adjecnt if | i-j| =8 or | i-j| =12. The Number of connected components in G is ?   4
what is R-B tree Wipro 4
Convert the following infix expression to post fix notation ((a+2)*(b+4)) -1   9
what is mean by d-queue?   10
Stack can be described as a pointer. Explain. Wipro 5
how a polynomial such as 6x^6+4x^3-2x+10 can be represnted by linked list?write an algorithm that reads such an polynomial   1
what is the different between B-tree and B+ tree. Syntel 7
How will inorder, preorder and postorder traversals print the elements of a tree?   11
what is hashing Infosys 7
Evaluate the following prefix expression " ++ 26 + - 1324" Patni 22
How many different binary trees and binary search trees can be made from three nodes that contain the key values 1, 2 & 3? iGate 28
 
For more Data Structures Interview Questions Click Here 
 
 
 
 
 


   
Copyright Policy  |  Terms of Service  |  Articles  |  Site Map  |  RSS Site Map  |  Contact Us
   
Copyright 2013  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  KalAajKal.com