Question
Stacks a) For each of the following stack operations, fill in the chart below. Also assuming the return value of each call to size(), top(),
Stacks
a) For each of the following stack operations, fill in the chart below. Also assuming the return value of each call to size(), top(), and empty() is sent to the screen, what is the output? (If there is nothing in the stack top() and pop() print error.). The first two are done for you.
Operation
|
Output
|
Stack Contents
|
push(5)
|
--
|
(5)
|
push(7)
|
--
|
(7,5)
|
push(5)
| ||
push(22)
| ||
push(15)
| ||
size()
| ||
empty()
| ||
pop()
| ||
pop()
| ||
top()
| ||
top()
| ||
push(17)
| ||
top()
| ||
pop()
| ||
pop()
| ||
pop()
| ||
empty()
| ||
pop()
| ||
pop()
| ||
empty()
| ||
pop()
| ||
empty()
|
b) Given the class ArrayStack, write a full program that would take in an arbitrary string of parentheses, curly braces, and square braces, and outputs if the string is balanced or not. The string is said to be balanced if all opening elements have a matching closing element of the same type. e.g. The string ( [ ] { } { } { ( ) } ) would be considered balanced, but ( { ) [ ( ] ) ) would not. Refer to Section 5.1.7 (pg 204-205) in your textbook for the algorithm.
Answer:
#include
#include
#include "ArrayStack.h"
int main( ) {
// set up the stack, string, and flag variables
ArrayStack
string input = "";
bool unbalanced = false;
// get the string
cin >> input;
// run through the loop
for (int i =0; i < input.length() && !unbalanced; i++) {
// Your code goes here.
}
// report to the user
cout << "The string is " << (unbalanced ? "unbalanced" : "balanced") << endl;
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started