Linked List. Design your own linked list class to hold a series of integers. – T
ID: 3938116 • Letter: L
Question
Linked List.
Design your own linked list class to hold a series of integers.
– The class should have the following member functions:
• Append nodes Add a new node to the end of the list
• Inserte nodes Insert a new node before the node whose value is greater or equal to the value of the new node
• Delete nodes Delete the node that has a specific value
• Search nodes Return the position of the node that has a specific value in the linked list
• Print the list Display all the values in the linked list • Copy the list Make a copy of the list •
Destroy the list Delete the list
– Demonstrate the class with a C++ program
Explanation / Answer
/* This is the program for linked list using C++ */
#include<iostream>
using namespace std;
class node {
friend class linked ;
private :
int data;
public :
node *link;
node(){
data=0;
link=NULL;
}
};
class linked {
public :
node *start=NULL, *ptr, *temp=NULL, *start1=NULL;
void append (int item);
void insertelement(int item);
void display();
void srch (int item);
void del(int item);
void copylst();
void dest();
};
void linked::append(int item){
ptr= new node();
ptr->data=item;
if(start==NULL){
start=ptr;
ptr->link=NULL;
temp=start;
}
else{
while(temp->link!=NULL){
temp=temp->link;
}
temp->link=ptr;
ptr->link=NULL;
}
}
void linked::insertelement(int item){
node *tmp=start->link,*tmp1,*tmp2;
tmp1=start;
while(tmp->link!=NULL){
if(tmp->data==item||temp->data>=item){
tmp2->data=item;
tmp1->link=tmp2;
tmp2->link=tmp;
}
tmp=tmp->link;
tmp1=tmp1->link;
}
}
void linked::display(){
node *tmp=start;
if(start==NULL){
cout<<"Empty list"<<endl;
}
while(tmp!=NULL){
cout<<" "<<tmp->data<<" ";
tmp=tmp->link;
}
}
void linked::srch(int item){
node *tmp=start;
int flag=0;
while (tmp!= NULL){
if(tmp->data==item){
flag++;
}
tmp=tmp->link;
}
if(flag>0){
cout<<"Number found in the list"<<endl;
}
else{
cout<<"Number not found "<<endl;
}
}
void linked::del(int item){
node *tmp=start,*tmp1;
int flag=0;
tmp1=tmp->link;
while(tmp1!=NULL){
if(tmp1->data==item){
flag++;
tmp->link=tmp1->link;
}
tmp=tmp->link;
tmp1=tmp1->link;
}
if(flag==0){
cout<<"Item not found to delete"<<endl;
}
}
void linked::copylst(){
node *tmp=start;
linked lin;
int a=0;
while(tmp=NULL){
a=tmp->data;
lin.append(a);
}
}
void linked::dest(){
start=NULL;
}
int main(){
int item=0,n=0,c=0;
linked fun;
while(1){
cout<<" *****Menu*****"<<endl;
cout<<"1.Insert Element :"<<endl;
cout<<"2.Display the elements :"<<endl;
cout<<"3.Delete the elements :"<<endl;
cout<<"4.Search Element :"<<endl;
cout<<"5.Copy the list :"<<endl;
cout<<"6.destroy the list :"<<endl;
cout<<"7.exit"<<endl;
cout<<"Enter your choice: "<<endl;
cin>>n;
cout<<endl;
switch(n){
case 1:
cout<<"Enter the number : "<<endl;
cin>>item;
cout<<" 1.Append the item"<<endl;
cout<<"2.insert the item"<<endl;
cout<<"Enter you choice : "<<endl;
cin>>c;
if(c==1){
fun.append(item);
}
else if (c==2){
fun.insertelement(item);
}
else{
cout<<"Enter a valid choice"<<endl;
}
break;
case 2:
cout<<"Displaying the elements"<<endl;
fun.display();
break;
case 3:
cout<<"Enter the number to delete: "<<endl;
cin>>item;
fun.del(item);
break;
case 4:
cout<<"Enter the number : "<<endl;
cin>>item;
fun.srch(item);
break;
case 5:
cout<<"List is being copied"<<endl;
fun.copylst();
break;
case 6:
cout<<"Destroying the list"<<endl;
fun.dest();
break;
case 7:
cout<<" Thank YOu"<<endl;
return 0;
default:
cout<<"*****ERROR*****"<<endl<<"Enter a valid choice"<<endl;
}
}
return 0;
}
this is to demonstrate the all the funtions of linked list using the c++ classes .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.