standard-library-in-x

Notes and readings for STL workshop

View on GitHub

Stacks

INTRODUCTION

Stacks are dynamic data structures that follow the Last In First Out (LIFO) principle. The last item to be inserted into a stack is the first one to be deleted from it.

For example, you have a stack of trays on a table. The tray at the top of the stack is the first item to be moved if you require a tray from that stack.

Stacks have restrictions on the insertion and deletion of elements. Elements can be inserted or deleted only from one end of the stack i.e. from the top. The element at the top is called the top element. The operations of inserting and deleting elements are called push() and pop() respectively.

CONSTRUCTION

stack <datatype> name;

FUNCTIONS

Function
What it does ?
Complexity
empty() Returns whether the stack is empty O(1)
size() Returns the size of the stack O(1)
top() Returns a reference to the top most element of the stack O(1)
push(g) Adds the element ā€˜gā€™ at the top of the stack O(1)
pop() Deletes the top most element of the stack O(1)

Implementation

#include <iostream>
#include <stack>

using namespace std;

void showstack(stack <int> kj)
{
    stack <int> c = kj;
    while (!c.empty())
    {
        cout << '\t' << c.top();
        c.pop();
    }
    cout << '\n';
}

int main ()
{
    stack <int> KJC;
    KJC.push(10);
    KJC.push(30);
    KJC.push(20);
    KJC.push(5);
    KJC.push(1);

    cout << "The stack KJC is : ";
    showstack(KJC);

    cout << "\nKJC.size() : " << KJC.size();
    cout << "\nKJC.top() : " << KJC.top();


    cout << "\nKJC.pop() : ";
    KJC.pop();
    showstack(KJC);

    return 0;
}

Output

The stack KJC is : 	1	5	20	30	10

KJC.size() : 5
KJC.top() : 1
KJC.pop() : 	5	20	30	10

Problems