3. Extend the class LinkedListclass by adding the following operations: a. Find
ID: 3725162 • Letter: 3
Question
3. Extend the class LinkedListclass by adding the following operations: a. Find and delete the node with the smallest info in the list. (Delete only the first occurrence. Traverse the list only once.) Call the method implementing this opera- tion deleteSmallest and include it as an abstract method in the class LinkedListClass.Write the definition of this method for the class UnorderedLinkedList.Also, write a program to test your method. b. Find and delete all the occurrences of a given info from the list. (Traverse the list only once.) Call the method implementing this operation deleteAll and include it as an abstract method in the class LinkedListClass.Write the definition of this method for the class UnorderedLinkedList. Also, write a program to test your method.Explanation / Answer
# include<iostream>
#define INT_MAX 3000;
using namespace std;
class node
{
public:
int info;
node *next;
};
class llist
{
public:
node *head;
node *tail;
public:
llist();
void prepend(int);
void display();
void deletesmallest();
};
llist::llist()
{
head=tail=NULL;
}
void llist::prepend(int x)
{
node *new1;
new1=new node;
new1->info=x;
new1->next=head;
if(head==NULL)
tail=new1;
head=new1;
// cout<<"at end of prepend x="<<x;
}
void llist::display()
{
node *temp;
temp=head;
if(head==NULL)
cout<<" List empty";
else
{
cout<<" Element of list are:";
while(temp!=NULL)
{
cout<<temp->info<<" ";
temp=temp->next;
}
}
}
void llist::deletesmallest()
{
node *temp;
node *prv;
int min=0;
min=INT_MAX;
while(head!=NULL)
{
if(min>=(head->info))
min=head->info;
else
head=head->next;
}
cout<<" Min of list="<<min;
temp=head;
prv=NULL;
while(temp!=NULL && temp->info!=min)
{
prv=temp;
temp=temp->next;
}
if(temp==head)
head=temp->next;
else
prv->next=temp->next;
if(temp==tail)
tail=prv;
delete temp;
cout<<min<<"Deleted";
}
int main()
{
llist s;
int x,n;
n=1;
cout<<" ENTER 5 ELEMENTS TO ENTER IN LIST";
while(n<=5)
{
cin>>x;
s.prepend(x);
n++;
}
s.display();
s.deletesmallest();
return 0;
}
Sample output:
Enter 5 elements to enter
67 8 7 1 98
Elements of list=98 1 7 8 67
1 Deleted
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.