Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Modify the Stack class to include appropriate error messages if invalid conditions occurfor example, trying to pop an item when the stack is empty.

C++ Modify the Stack class to include appropriate error messages if invalid conditions occurfor example, trying to pop an item when the stack is empty.

A SIMPLE STACK CLASS

main.cpp

/*******************************

* Week 2 lesson: *

* a simple Stack class *

*******************************/

#include

#include "stack.h"

using namespace std;

int main()

{

Stack s;

cout << "Insertion of 10 characters in s" << endl;

for (int i = 0; i < s.getSize(); i++)

{

char x = 32 + rand()%95;

cout << x << endl;

s.push(x);

}

cout << endl

<< "Displaying and deleting elements from s" << endl;

while(!s.isEmpty())

{

cout << "Item at the top: " << s.peek() << endl;

s.pop();

}

return 0;

}

Stack.cpp

/*******************************

* Week 2 lesson: *

* a simple Stack class *

*******************************/

#include "Stack.h"

/*

* Constructor. Initializes the stack.

*/

Stack::Stack()

{

top = 0;

}

/*

* Determines whether the stack is empty.

*

* Returns true if the stack is empty, false otherwise.

*/

bool Stack::isEmpty()

{

return top == 0;

}

/*

* Adds an element to the top of the stack.

*

* x: element to be added to the stack.

*/

void Stack::push(char x)

{

list[top] = x;

top++;

}

/*

* Removes the element at the top of the stack.

*/

void Stack::pop()

{

top--;

}

/*

* Returns the element at the top of the stack. Does not remove it.

*/

char Stack::peek()

{

return list[top-1];

}

/*

* Returns the size of the stack.

*/

int Stack::getSize()

{

return SIZE;

}

Stack.h

/*******************************

* Week 2 lesson: *

* a simple Stack class *

*******************************/

/*

* Class implementing a Stack ADT.

*/

class Stack

{

public:

Stack();

bool isEmpty();

void push(char);

void pop();

char peek();

int getSize();

private:

static const int SIZE = 10; //size of the queue array

char list[SIZE]; //array to store the stack items

int top; //amount of elements in the array

};

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Information And Database Systems Asian Conference Aciids 2012 Kaohsiung Taiwan March 2012 Proceedings Part 2 Lnai 7197

Authors: Jeng-Shyang Pan ,Shyi-Ming Chen ,Ngoc-Thanh Nguyen

2012th Edition

3642284892, 978-3642284892

More Books

Students also viewed these Databases questions

Question

2. What do you believe is at the root of the problem?

Answered: 1 week ago