13626 - Stack Basic   

Description

Please implement three basic stack operations, which are "push", "pop", and "peak". And implement the function "isEmpty" to check if a stack is empty or not.
The maximum size of the stack should be 1000.

You may use the code template below and replace /*TODO*/ section with your code.

Template.cpp

#include <iostream>
#include <sstream>
#include <stdexcept>

#define MAX 1000

class Stack{
  int top; //This is a variable that help us keep track of the current top position of the stack.

public:
  int array[MAX]; //This is the array that we used to implement the stack.

  Stack(){ top = -1; }
  bool push(int x);
  int pop();
  int peek();
  bool isEmpty();
};

bool Stack::push(int x){
    if (top >= (MAX -1)){
      throw std::overflow_error("error: stack overflow");
    }
    else {
      /*TODO*/ //Hint: You should modify "array", "top" to update the stack.
      std::cout << x << " is pushed\n";
      return true;
    }
}

int Stack::pop(){
  if (/*TODO*/){ //Hint: You should make a comparison between the variable "top" and a number.
    throw std::underflow_error("error: stack underflow");
  }
  else{
    /*TODO*/ //Hint: You should modify "array", "top", and return a value at the end of this block.
    return x;
  }
}

int Stack::peek(){
  if (/*TODO*/){ //Hint: You should make a comparison between the variable "top" and a number.
    throw std::out_of_range("error: stack is empty");
  }
  else{
    /*TODO*/ //Hint: Return the value on the top of the array.
  }
}

bool Stack::isEmpty(){
  /*TODO*/ //Hint: Return true if the stack is empty, return false otherwise.
}

int main(){
  std::string line_string;
  class Stack my_stack;

  while(std::getline(std::cin, line_string)){
    std::istringstream iss(line_string);
    std::string instruction;
    
    iss >> instruction;

    if(instruction == "push"){
      try{
        int number;
        iss >> number;
        my_stack.push(number);
      }
      catch(std::overflow_error& e){
        std::cout << e.what() << std::endl;
      }
    } else if (instruction == "pop"){
      try{
        int number = my_stack.pop();
        std::cout << number << " is poped" << std::endl;
      }
      catch(std::underflow_error& e){
        std::cout << e.what() << std::endl;
      }
    } else if (instruction == "peek"){
      try{
        int number = my_stack.peek();
        std::cout << number << " is on the top" << std::endl;
      }
      catch(std::out_of_range& e){
        std::cout << e.what() << std::endl;
      }
    } else if (instruction == "empty?"){
      std::cout << ( my_stack.isEmpty() ? "yes" : "no" ) << std::endl;
    }
  }
  
  return 0;
}

Input

 The inputs are constructed as follows:

  • push i: Push an integer i on the stack.

  • pop: Remove and return the top element if there is any.

  • peek: Return the top element, do not modify the stack.

  • empty?: Return true if the stack is empty, otherwise, return false.

Output

The corresponding outputs should be constructed as follows:

  • Print i is pushed after a successful push i.

  • Print i is poped after a successful pop.

  • Print yes to empty? if the stack is empty.

  • Print no to empty? if the stack is not empty.

  • Print error: stack overflow when user wants to push i on a full stack. (The stack size is 1000)

  • Print error: stack underflow when the user wants to pop an empty stack.

  • Print error: stack is empty when the user wants to peek at an empty stack.

Note: You should append a newline character(\n) after each output.

Sample Input  Download

Sample Output  Download

Tags




Discuss