Part 1 #include <iostream> using namespace std; class linklist { private: struct
ID: 3619636 • Letter: P
Question
Part 1#include <iostream>
using namespace std;
class linklist
{
private:
struct node
{
int data;
node *link;
}*p;
public:
linklist();
void append( int num );
void add_as_first(...
#include <iostream>
using namespace std;
class linklist
{
private:
struct node
{
int data;
node *link;
}*p;
public:
linklist();
void append( int num );
void add_as_first( int num );
void addafter( int c, int num );
void del( int num );
void display();
int count();
~linklist();
};
linklist::linklist()
{
p=NULL;
}
void linklist::append(int num)
{
node *q,*t;
Porblem 2 is were I need help
Create a program that uses a derived class based on the list class you’ve created in the
first program. This program will use a stack data type.
class node {
void *info;
node *next;
public:
node (void *v) {info = v; next = 0; }
void put_next (node *n) {next = n;}
node *get_next ( ) {return next;}
void *get_info ( ) {return info;}
};
class list {
node *head;
int node_num;
public:
list ( ) { node_num = 0; head = 0;}
void remove (int);
void insert (void *, int);
void append (void * v) {insert (v, node_num + 1); }
void *find (int);
void display ( );
};
Write functions to push and pop the stack. Write a driver main program which gives 5
values (5 nodes created) that will push, pop and display data stored.
Final
Explanation / Answer
Dear,The code provided here is to implement stack operations using linked list.
Here is the code.
#include<iostream>
using namespace std;
class DynIntStack
{
private:
// Structure for stack nodes
struct StackNode
{
int value; // Value in the node
StackNode *next; // Pointer to the next node
};
StackNode *top; // Pointer to the stack top
public:
// Constructor
DynIntStack()
{ top = NULL; }
// Destructor
~DynIntStack();
// Stack operations
void push(int);
void pop(int &);
bool isEmpty();
};
//**************************************************
// Destructor *
// This function deletes every node in the list. *
//**************************************************
DynIntStack::~DynIntStack()
{
StackNode *nodePtr, *nextNode;
// Position nodePtr at the top of the stack.
nodePtr = top;
// Traverse the list deleting each node.
while (nodePtr != NULL)
{
nextNode = nodePtr->next;
delete nodePtr;
nodePtr = nextNode;
}
}
//************************************************
// Member function push pushes the argument onto *
// the stack. *
//************************************************
void DynIntStack::push(int num)
{
StackNode *newNode; // Pointer to a new node
// Allocate a new node and store num there.
newNode = new StackNode;
newNode->value = num;
// If there are no nodes in the list
// make newNode the first node.
if (isEmpty())
{
top = newNode;
newNode->next = NULL;
}
else // Otherwise, insert NewNode before top.
{
newNode->next = top;
top = newNode;
}
}
//****************************************************
// Member function pop pops the value at the top *
// of the stack off, and copies it into the variable *
// passed as an argument. *
//****************************************************
void DynIntStack::pop(int &num)
{
StackNode *temp; // Temporary pointer
// First make sure the stack isn't empty.
if (isEmpty())
{
cout << "The stack is empty. ";
}
else // pop value off top of stack
{
num = top->value;
temp = top->next;
delete top;
top = temp;
}
}
//****************************************************
// Member function isEmpty returns true if the stack *
// is empty, or false otherwise. *
//****************************************************
bool DynIntStack::isEmpty()
{
bool status;
if (!top)
status = true;
else
status = false;
return status;
}
int main()
{
int catchVar; // To hold values popped off the stack
// Create a DynIntStack object.
DynIntStack stack;
// Push 5, 10, and 15 onto the stack.
cout << "Pushing 5 ";
stack.push(5);
cout << "Pushing 10 ";
stack.push(10);
cout << "Pushing 15 ";
stack.push(15);
// Pop the values off the stack and dispaly them.
cout << "Popping... ";
stack.pop(catchVar);
cout << catchVar << endl;
stack.pop(catchVar);
cout << catchVar << endl;
stack.pop(catchVar);
cout << catchVar << endl;
// Try to pop another value off the stack.
cout << " Attempting to pop again... ";
stack.pop(catchVar);
return 0;
}
Hope this will help you