Skip to content

Latest commit

 

History

History

Stack

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

Stack

Template arguments

T - Type of elements.

Functions

push

Return: none
Arguments: T

Pushes element with a given value on the top of a stack.

pop

Return: none
Arguments: none

Removes element from the top of a stack.

top

Return: T
Arguments: none

Gets value of the top element.

size

Return: unsigned int
Arguments: none

Gets size of the stack.

empty

Return: bool
Arguments: none

Returns whether the stack is empty.

begin

Return: iterator
Arguments: none

Returns iterator object to the first element.

end

Return: iterator
Arguments: none

Returns iterator object to the element past the last.

Usage

Library include

#include "path/to/Basic-Data-Structures/include/stack"

Stack declaration

Stack<int> s;

Basic push, pop and top usage

s.push(4);                      // stack:  4
s.push(1);                      // stack:  1 4
s.pop();                        // stack:  4
s.push(3);                      // stack:  3 4
std::cout << s.top() << "\n";   // prints: 3
s.push(2);                      // stack:  2 3 4
std::cout << s.top() << "\n";   // prints: 2

Print stack using empty, top and pop

while(!s.empty()) {
    std::cout << s.top() << "\n";
    s.pop();
}

Print stack using iterator (recommended way)

for(Stack<int>::iterator i = s.begin(); i != s.end(); ++i) {
    std::cout << *i << "\n";
}

Print stack using iterator with range-based for loop and auto (recommended way for C++11 and higher)

for(auto i : s) {
    std::cout << i << "\n";
}

Full example code