Stack

16.1 - Stack basic 1


Stack is an abstract data type with these operations:

  • push, which takes an element and puts it on top of the stack.
  • pop, which removes the element on top of the stack.
  • top, which returns the element on top of the stack.
  • empty, which returns whether the stack is empty or not.
  • size, which returns the number of elements stored in the stack.

The C++ standard library has defined a stack implementation for you to use. It is available in the header.

Your task is to create a program which takes a series of letters and asterisks (*). If the program encounters a letter, it should push it into a stack. If it encounters an asterisk, it should print out the letter on top of the stack and a space then pop it.

Note that the input will never have an asterisk when the stack is empty at that point.

Sample input

a b c d e * f g h * * i j * * * * * * *

Sample output

e h g j i f d c b a
#include <iostream> #include <stack> int main() { }