Complete the following linked list implementation of STACK in C++ to include two
ID: 3674431 • Letter: C
Question
Complete the following linked list implementation of STACK in C++ to include two other member functions int peek() and void flush(). Function int peek() returns the top element of the stack without popping this element of the stack. Function void flush() displays the entire stack content, and empties the stack. Do not call empty() and pop() functions in flush(). Function flush() displays an empty line if the stack is empty.
class STACK
{
private:
struct node
{ int item; node* next;
node(int x, node* t)
{ item = x; next = t; }
};
typedef node *link;
link head;
public:
STACK(int)
{ head = 0; }
int empty() const
{ return head == 0; }
void push(int x)
{ head = new node(x, head); }
int peek()
{
/* implement this part */
}
int pop()
{ int v = head->item; link t = head->next;
delete head; head = t; return v; }
void flush()
{
/* implement this part */
}
};
Write a C++ program that creates a stack of integer numbers (for up to 100 numbers), and then it does the following on user’s input (your program must check for conditions: e.g. no pop when the stack is empty):
P x : pushes integer x onto the stack
R : pops off and displays the stack top
E : outputs “empty” if stack is empty; otherwise “not empty”
K : outputs the stack top integer x using function peek
F : outputs the entire content of the stack in last-in-first-out order; empties the stack using function flush
Example Dialogue (input can be read from a specific file, or can be directed to a file):
E <ENTER>
empty
P 1
P 10
E <ENTER>
not empty
K
10
P 19
R
19
R
10
E <ENTER>
not empty
P 5
P 20
F
20 5 1
E <ENTER>
empty
R
nothing to pop off; the stack is empty
P 2
E <ENTER>
not empty
Explanation / Answer
#ifndef STACK_H
#define STACK_H
#include<iostream>
#include<stdlib.h>
using namespace std;
class STACK
{
private:
struct node
{
int item;
node* next;
node(int x, node* t)
{
item = x;
next = t;
}
};
typedef node *link;
link head;
public:
STACK()
{
head = 0;
}
int empty() const
{
return head == 0;
}
//push method to insert element x
void push(int x)
{
head = new node(x, head);
}
//peek method
int peek()
{
//Create a temp node
node *temp=head;
//get value from temp node
int value=temp->item;
//move to last node
while(temp->next!=NULL)
{
int value=temp->item;
temp=temp->next;
}
//return value
return value;
}
int pop()
{
int v = head->item;
link t = head->next;
delete head;
head = t;
return v;
}
//The flush method that prints the elements
//of stack
void flush()
{
node *temp=head;
if(temp==NULL)
cout<<"EMPTY"<<endl;
else
{
while(temp!=NULL)
{
cout<<temp->item<<",";
temp=temp->next;
}
}
cout<<endl;
}
};
#endif STACK_H
---------------------------------------------------------------------------------------------------------
//driver.cpp
/**C++ program that tests the methods of STACK class
peek and flush methods and print the results to console*/
#include<iostream>
//include STACK header file
#include "STACK.h"
using namespace std;
int main()
{
//Create an object of STACK class
STACK stack;
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4); //peek element
//calling peek
cout<<"Peek of the stack : "<<stack.peek()<<endl;
cout<<"Calling flush method that prints the elements in stack "<<endl;
//calling flush
stack.flush();
system("pause");
return 0;
}
----------------------------------------------------------------------------------------------------------------
sample output:
Peek of the stack : 4
Calling flush method that prints the elements in stack
4,3,2,1,