Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Stacks a) For each of the following stack operations, fill in the chart below. A

ID: 3730298 • Letter: S

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(), 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 <iostream>

#include <string>

#include "ArrayStack.h"

int main( ) {

   // set up the stack, string, and flag variables

   ArrayStack<char> checker;

   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;

}

          

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()

      

Explanation / Answer

Operation Output Stack Contents

push(5)   --     (5)
push(7)   --     (7,5)
push(5)   --     (5,7,5)
push(22) --     (22,5,7,5)
push(15) --     (15,22,5,7,5)
size()    5
empty()   false
pop()     --     (22,5,7,5)
top()     22     (22,5,7,5)
top()     22     (22,5,7,5)
push(17) --     (17,22,5,7,5)
top()     17     (17,22,5,7,5)
pop()     --     (22,5,7,5)
pop()     --     (5,7,5)
empty()   false
pop()     --     (7,5)
pop()     --     (5)
empty()   false
pop()     --     ()
empty()   true


The relevant code block

for (int i =0; i < input.length() && !unbalanced; i++) {
        if (input[i] == '{' || input[i] == '(' || input[i] == '['){
           checker.push(input[i]);
        }
        if (input[i] == ')'){
           if (!checker.empty()){
              char a = ' ';
              while(a != '(' && !checker.empty()){
                   a = checker.top();
                   checker.pop();
                  
              }
              if (checker.empty()){
                  unbalnced = true;
                  break;
              }

           }
           else {
               unbalanced = true;
               break;
           }
        }
        if (input[i] == '}'){
           if (!checker.empty()){
              char a = ' ';
              while(a != '{' && !checker.empty()){
                   a = checker.top();
                   checker.pop();
                  
              }
              if (checker.empty()){
                  unbalnced = true;
                  break;
              }

           }
           else {
               unbalanced = true;
               break;
           }

        }
        if (input[i] == ']'){
           if (!checker.empty()){
              char a = ' ';
              while(a != '[' && !checker.empty()){
                   a = checker.top();
                   checker.pop();
                  
              }
              if (checker.empty()){
                  unbalnced = true;
                  break;
              }

           }
           else {
               unbalanced = true;
               break;
           }

        }

    }
    if (!checker.empty())
       unbalanced = true;

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote