In: Computer Science
Please Use C++ to finish as the requirements.
Implement a class called SinglyLinkedList. In the main function, instantiate the SinglyLinkedList class. Your program should provide a user loop and a menu so that the user can access all the operators provided by the SinglyLinkedList class.
If You have Any Query Regarding this please ask in comment section I will be there to solve all your query in comment section immediately hope you will like it
#include "stdafx.h"
#include<iostream>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
node *link;
}*p;
class list
{
node *next;
public:
void InsertLast(int);
void InsertFirst(int);
void Insert(int,int);
void Delete(int);
void DeleteFirst();
void DeleteLast();
void Print();
int seek(int);
list(){p=NULL;}
~list();
bool IsEmpty();
void reverse();
int Length();
};
bool IsEmpty()
{
if(p==NULL)
return true;
else
return false;
}
void list :: reverse()
{
int a[50];
next =p;
int i=0;
while(next->link!=NULL)
{
a[i]=next->data;
next=next->link;
i=i+1;
}
next=p;
while(next->link!=NULL)
{
next->data=a[i];
next=next->link;
i=i-1;
}
Print();
}
void list::InsertLast(int x)
{
node *q,*t;
if(p==NULL)
{
p=new node;
p->data=x;
p->link=NULL;
}
else
{
q=p;
while(q->link!=NULL)
q=q->link;
t=new node;
t->data=x;
t->link=NULL;
q->link=t;
}
cout<<" Inserted successfully at the end..";
Print();
}
void list:: InsertFirst(int x)
{
node *q;
q=p;
p=new node;
p->data=x;
p->link=q;
cout<<" Inserted successfully at the begining..";
Print();
}
void list::Delete(int x)
{
node *q,*r;
q=p;
if(q->data==x)
{
p=q->link;
delete q;
return;
}
r=q;
while(q!=NULL)
{
if(q->data==x)
{
r->link=q->link;
delete q;
return;
}
r=q;
q=q->link;
}
cout<<"
Element u entered "<<x<<" is not found..
";
}
void list:: DeleteFirst()
{
cout<<" The list before deletion:";
disp();
node *q;
q=p;
if(q==NULL)
{
cout<<" No data is present..";
return;
}
p=q->link;
delete q;
return;
}
void list:: DeleteLast()
{
cout<<"
The list before deletion:
";
disp();
node *q,*t;
q=p;
if(q==NULL)
{
cout<<" There is no data in the list..";
return;
}
if(q->link==NULL)
{
p=q->link;
delete q;
return;
}
while(q->link->link!=NULL)
q=q->link;
q->link=NULL;
return;
}
list::~list()
{
node *q;
if(p==NULL) return;
while(p!=NULL)
{
q=p->link;
delete p;
p=q;
}
}
void list::Print()
{
node *q;
q=p;
if(q==NULL)
{
cout<<" No data is in the list..";
return;
}
cout<<" The items present in the list are :";
while(q!=NULL)
{
cout<<" "<<q->data;
q=q->link;
}
}
void list :: Insert(int value,int position)
{
node *temp,*temp1;
temp=p;
if(temp1==NULL)
{
temp1= new node;
temp1->data=value;
temp1->link=NULL;
p=temp1;
return;
}
for(int i=0;((i<position)&&(temp->link!=NULL)) ;i++)
{
if(i==(position-1))
{
temp1= new node;
temp1->data= value;
temp1->link=temp->link;
temp->link=temp1;
}
temp=temp->link;
}
Print();
}
int list::seek(int value)
{
node *temp;
temp=p;
int position=0;
while(temp!=NULL)
{
if(temp->data==value)
return position+1;
else
{
temp=temp->link;
position=position+1;
}
}
cout<<" Element "<<value<<" not found";
return 0;
}
int list::GetFirst()
{
return p->data;
}
int list::Length(int value)
{
node *temp;
temp=p;
int position=0;
while(temp!=NULL)
{
position++
}
cout<<" Element "<<value<<" not found";
return position;
}
void main()
{
list l;
int ch,v,p,ps,n;
bool flag;
do
{
clrscr();
cout<<" Operations on List.."<<"\n";
cout<<"1.Destroy List"<<"\n";
cout<<"2.Initialize List"<<"\n";
cout<<"3.Get First"<<"\n";
cout<<"4.Insert First,Insert Last,Insert"<<"\n";
cout<<"5.Delete First,Delete Last,Delete"<<"\n";
cout<<"6.IsEmpty"<<"\n";
cout<<"7.Length"<<"\n";
cout<<"8.Print";
cout<<"9.Reverse Print"<<"\n";
cout<<"10.Search"<<"\n";
cout<<"11.Exit";
cout<<"Enter ur choice:";
cin>>ch;
switch(ch)
{
case 1:
~l();
cout<<"List has been destroyed";
case 2:
l=new list();
cout<<"List has been initialized";
case 2:
n= GetFirst();
cout<<"First element in list is: "<<n;
case 4:
cout<<" 1.Insertion at begining"<<"\n";
cout<< "2.Insertion at the end"<<"\n";
cout<<"3.Insertion after the mentioned position"<<"\n";
cout<<" Enter ur choice:";
cin>>ps;
cout<<"Enter the value to insert:";
cin>>v;
switch(ps)
{
case 1:
l.InsertFirst(v);
break;
case 2:
l.InsertLast(v);
break;
case 3:
cout<<" Enter the position to insert the value:";
cin>>p;
l.Insert(v,p);
break;
default:
cout<<"The choice is invalid";
return;
}
break;
case 5:
cout<<"1.Delete the first element ";
cout<<"2.Delete the last element"
cout<<"3.Enter the element to delete from the list";
cout<<" Enter ur choice:";
cin>>ps;
switch(ps)
{
case 1:
l.DeleteFirst();
cout<<" The list after deletion:";
l.Print();
break;
case 2:
l.DeleteLast();
cout<<" The list after deletion:";
l.Print();
break;
case 3:
l.Print();
cout<<" Enter the element to delete : ";
cin>>v;
l.Delete(v);
cout<<" The list after deletion:";
l.Print();
break;
default:
cout<<" The option is invalid...";
break;
}
break;
case 6:
flag= l.IsEmpty();
if(flag==true)
cout<<" List is Empty";
else
cout<<" List is not Empty";
break;
case 7:
n=l.Length();
cout<<"number of elements : "<<n;
break;
case 8:
l.Print();
break;
case 9:
l.reverse();
break;
case 10:
l.Print();
cout<<" Enter the element to search:";
cin>>v;
cout<<" The position of the element "<< v<<" is "<<l.seek(v);
getch();
break;
case 11:
exit(1);
default:
cout<<" The option is invalid...";
return;
}
getch();
}while(ch!=11);
getch();
return;
}