They will ask u question about single linked list?. Write
Code for to insert delete node.

Answer Posted / soumya

Represent a list of integers as singly linked list. Write
functions for ADD, DELETE and SEARCH operations.
#include<iostream>
using namespace std;
class student
{
int age;
student *nxt;
public:
void add_at_front();
void add_at_end();
void add_at_any();
void del_at_front();
void del_at_end();
void del_at_any();
void display();
void search();
};
student *start=NULL;
void student::add_at_front()
{
student *s1;
s1=new student;
cout<<"Enter the age";
cin>>s1-­>age;
s1-­>nxt=start;
start=s1;
}
void student:: add_at_end()
{
student *s1;
s1=new student;
cout<<"Enter the age";
cin>>s1-­>age;
s1­>nxt=NULL;
if(start==NULL)
{
start=s1;
}
else
{
student *temp;
temp=new student;
temp=start;
while(temp­->nxt!=NULL)
{
temp=temp-­>nxt;
}
temp­->nxt=s1;
}
}
void student::add_at_any()
{
student *s1;
s1=new student;
int x;
cout<<"Enter age of the node you want to specify:";
cin>>x;
cout<<"Enter the age of the new node";
cin>>s1­>age;
student *temp;
temp=new student;
temp=start;
while((temp­->age!=x)&&(temp-­>nxt!=NULL))
{
temp=temp-­>nxt;
}
if(temp-­>nxt=NULL)
{
cout<<"Error";
}
else
{
s1­->nxt=temp­->nxt;
temp-­>nxt=s1;
}
}
void student::del_at_front()
{
student *temp;
if(start==NULL)
{
cout<<"Empty list";
}
else
{
temp=start;
start=start­->nxt;
delete temp;
}
}
void student::del_at_end()
{
student *temp,*temp1;
if(start==NULL)
{
cout<<"Empty list";
}
else
{
temp=start;
temp1=start;
while(temp­->nxt!=NULL)
{
temp1=temp;
temp=temp-­>nxt;
}
}
delete temp;
temp1-­>nxt=NULL;
}
void student::del_at_any()
{
student *temp,*temp1;
int x;
cout<<"Enter the age of the node to be deleted:";
cin>>x;
temp=start;
temp1=start;
while((temp-­>nxt!=NULL)&&(temp­->age!=x))
{
temp1=temp;
temp=temp­->nxt;
}
if(temp­->nxt==NULL)
{
cout<<"Error in inputs:";
}
else
{
temp1­->nxt=temp­->nxt;
delete temp;
}
}
int main()
{
int opt;
char ch;
student s;
do
{
cout<<"*******OPERATIONS******\n:1.add_front\n2.add_end\n3.add_any\
n4.del_fround\n5.del
_end\n6.del_any\n7.display\n8.search\n9.exit\nEnter your
choice:";
cin>>opt;
switch(opt)
{
case 1:s.add_at_front();
break;
case 2:s.add_at_end();
break;
case 3:s.add_at_any();
break;
case 4:s.del_at_front();
break;
case 5:s.del_at_end();
break;
case 6:s.del_at_any();
break;
case 7:s.display();
break;
case 8:s.search();
break;
case 9:break;
default : cout<<"Error";
break;
}
//cout<<"Do you want to continue (Y/N)";
//cin>>ch;
}
while(opt!=9);
}
void student::display()
{
student *temp;
temp=start;
while(temp!=NULL)
{
cout<<temp-­>age<<"\n" ;
temp=temp-­>nxt;
}
}
void student::search()
{
student *s1;
s1=new student;
int x,n,f=0;
n=1;
cout<<"Enter the age you want to search ";
cin>>x;
s1=start;
while(s1­->age!=x)
{
s1=s1­->nxt;
n=n+1;
if((s1­->nxt==NULL)&&(s1-­>age!=x))
{
f=1;
break;
}
}
if(f==1)
cout<<"Element not fount";
else
cout<<s1­->age<<"is at position"<<n<<"\n\n";
}

Is This Answer Correct ?    2 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Differentiate between C and C++.

703


What is type of 'this' pointer?

588


What is implicit pointer in c++?

590


Define a constructor - what it is and how it might be called (2 methods)?

596


Write an algorithm that determines whether or not an almost complete binary tree is a heap.

3421






What is iterator in c++?

601


A prime number is a number which is divisible only by itself and 1. Examples of the first few primes are 2, 3, 5, 7, 11. Consider writing a program which can generate prime numbers for you. Your program should read in and set a maximum prime to generate and a minimum number to start with when looking for primes. This program should be able to perform the following tasks: 1. Read the maximum number from user (keyboard input) to look for primes. The program should not return any primes greater than this number. 2. Read the minimum number from user (keyboard input) to look for primes. The program should not return any primes less than this number. 3. Generate and print out every prime number between the maximum prime and minimum number specified by the user.

1736


How can you quickly find the number of elements stored in a a) static array b) dynamic array ? Why is it difficult to store linked list in an array?how can you find the nodes with repetetive data in a linked list?

604


How did c++ start?

602


Is it possible to provide special behavior for one instance of a template but not for other instances?

626


Will c++ be replaced?

539


Differentiate between the manipulator and setf( ) function?

586


Does c++ have foreach?

515


What is the most powerful coding language?

601


Explain how a pointer to function can be declared in C++?

572