I have a list class that is used to create a linked list. What I need to do is d
ID: 3539877 • Letter: I
Question
I have a list class that is used to create a linked list. What I need to do is derive a new class "stack" from my list class. I am able to create them both individually, but I get stuck on the deriving part.
// List Class
#ifndef LIST_H
#define LIST_H
#include<iostream>
using namespace std;
class list
{
struct node
{
int info;
node *next;
}*v;
public:
void insert(int);
void append(int);
void delFirst();
void delLast();
void delSpec(int);
void display();
list(){v = NULL;}
~list();
};
void list::insert(int x) // Insert into the front of the list.
{
node *w;
w = v;
v = new node;
v->info = x;
v->next = w;
cout << " Inserted successfully at the begining.. " << endl;
display();
}
void list::append(int x) // Append to the end of the list
{
node *w, *u;
if(v == NULL)
{
v = new node;
v->info = x;
v->next = NULL;
}
else
{
w = v;
while(w->next != NULL){
w = w->next;
}
u = new node;
u->info = x;
u->next = NULL;
w->next = u;
}
cout << " Inserted successfully at the end.. " << endl;
display();
}
void list::delSpec(int x) // Delete specific value
{
node *w, *u;
w = v;
if(w->info == x)
{
v = w->next;
delete w;
return;
}
u = w;
while(w != NULL)
{
if(w->info == x)
{
u->next = w->next;
delete w;
return;
}
u = w;
w = w->next;
}
cout << " The element you entered was not found.. " << endl;
}
void list::delFirst() // Delete the first value
{
cout << " The list before deletion: " << endl;
display();
node *w;
w = v;
if(w == NULL)
{
cout << " There is no data in the list.. " << endl;
return;
}
v = w->next;
delete w;
return;
}
void list::delLast() // Delete the last value
{
cout << " The list before deletion: " << endl;
display();
node *w;
w = v;
if(w == NULL)
{
cout << " There is no data in the list.. " << endl;
return;
}
if(w->next == NULL)
{
v = w->next;
delete w;
return;
}
while(w->next != NULL)
w = w->next;
w->next = NULL;
return;
}
list::~list()
{
node *w;
if(v == NULL) return;
while(v != NULL)
{
w = v->next;
delete v;
v = w;
}
}
void list::display() // Display the list
{
node *w;
w = v;
if(w == NULL)
{
cout << " There is no data in the list.. " << endl;
return;
}
cout << " The items present in the list are : ";
while(w != NULL)
{
cout << " " << w->info;
w = w->next;
}
cout << endl;
}
#endif
Explanation / Answer
Use
class Stack::public list{
//methods
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.