Write a menu driven program to perform insertion and deletion operations in a single linked list

#include<iostream>
using namespace std;
struct node
{
int info;
struct node *link;
}*start;
class test
{
node *create_node(int);
public:
test()
{
start=NULL;
}
void insert_beg();
void insert();
void del();
void display();
};
node *test::create_node(int val)
{
node *temp;
temp=new(struct node);
if(temp==NULL)
{
cout<<"\nMemory not allocated\n";
}
else
{
temp->info=val;
temp->link=NULL;
return temp;
}
}
void test::insert_beg()
{
int val;
node *temp,*s;
cout<<"Enter the value:";
cin>>val;
temp=create_node(val);
s=start;
if(start==NULL)
{
start=temp;
temp->link=NULL;
}
else
{
temp->link=start;
start=temp;
}
cout<<"\nNode Inserted!!\n";
}
void test::insert()
{
int pos,val,count=0;
node *temp,*s,*ptr;
cout<<"\nEnter the position:";
cin>>pos;
cout<<"\nEnter the value:";
cin>>val;
temp=create_node(val);
s=start;
while(s!=NULL)
{
s=s->link;
count++;
}
if(pos==1)
{
if(start==NULL)
{
start=temp;
start->link=NULL;
}
else
{
ptr=start;
start=temp;
start->link=ptr;
}
}
else if(pos>1&&pos<=count)
{
s=start;
for(int i=0;i<pos;i++)
{
ptr=s;
s=s->link;
}
ptr->link=temp;
temp->link=s;
cout<<"\nNode Inserted!!\n";
}
}
void test::del()
{
int pos,val,count=0;
node *temp,*s,*ptr;
cout<<"\nEnter the position:";
cin>>pos;
s=start;
while(s!=NULL)
{
s=s->link;
count++;
}
if(pos>1&&pos<count)
{
for(int i=0;i<pos;i++)
{
ptr=s;
s=s->link;
}
ptr=s->link;
}
}
void test::display()
{
node *s;
if(start==NULL)
{
cout<<"\nList is Empty\n";
}
else
{
s=start;
while(s!=NULL)
{
cout<<s->info<<"->";
s=s->link;
}
cout<<"NULL\n";
}
}
int main()
{
test t;
int a=1;
while(a!=5)
{
cout<<"\n1. Insert at beginning\n2. Insert at Position\n3. Deleting at Position\n4. Display\n5. Exit";
cout<<"\nEnter your choice:";
cin>>a;
switch(a)
{
case 1:t.insert_beg();
break;
case 2:t.insert();
break;
case 3:t.del();
break;
case 4:t.display();
break;
case 5:cout<<"\n/*\nName-Sanjampreet Singh\nRoll Number-1507967\n*/";
break;
}
}
return 0;
}


Output:

Comments

Popular posts from this blog

Program to illustrate the concept of templates.

To create an html file to implement the concept of margin, padding using cascading style sheets.

To configure the IP address for a computer connected to LAN