2.3 Stacks (Last-In-First-Out)

stack()


void push(const value_type& x)


void pop()


value_type&top()


bool empty()

<..ex-stack.cxx..>
 #include <stack>
 #include <iostream>
 using namespace std;
 
 int main() {
    stack<int> s;
    s.push(1);  s.push(2);  s.push(3);
    while( ! s.empty() ){
       cout << s.top() << ' ';
       s.pop();
    }
    cout << endl;
    return 0;
 }
-_-_-
3 2 1

Array Representation

       |---|
  max  |10 |
       |---|
length |   |
       -5--
       |---|---|------|---|---|---|--|---|
       |e1 |e2 | ...   |en |   |   |  |   |
       ----------------------------------

One-way Linked Representation

    |---|
top | 5 |
    ----
     |---|                  |---|   |---   |---|
     |   ||----       -|------  ||---- |||---- |
     |---|                  |---|   |--|   |---|
     |e1 |                  en-2|  en- 1   |en |
     ----                   ----    ----   ----

[stacks]