The SimpleCxxLib package


#include "stack.h"

class Stack<ValueType>

This class models a linear structure called a stack in which values are added and removed only from one end. This discipline gives rise to a last-in/first-out behavior (LIFO) that is the defining feature of stacks. The fundamental stack operations are push (add to top) and pop (remove from top).
Constructor
Stack() Initializes a new empty stack.
Methods
clear() Removes all elements from this stack.
isEmpty() Returns true if this stack contains no elements.
peek() Returns the value of top element from this stack, without removing it.
pop() Removes the top element from this stack and returns it.
push(value) Pushes the specified value onto this stack.
size() Returns the number of values in this stack.
toString() Converts the stack to a printable string representation.

Constructor detail


Stack();
Initializes a new empty stack.

Usage:

Stack<ValueType> stack;
Stack<ValueType> stack = { bottomValue, middleValue, topValue };

Method detail


int size() const;
Returns the number of values in this stack.

Usage:

int n = stack.size();

bool isEmpty() const;
Returns true if this stack contains no elements.

Usage:

if (stack.isEmpty()) ...

void clear();
Removes all elements from this stack.

Usage:

stack.clear();

void push(ValueType value);
Pushes the specified value onto this stack.

Usage:

stack.push(value);

ValueType pop();
Removes the top element from this stack and returns it. This method signals an error if called on an empty stack.

Usage:

ValueType top = stack.pop();

ValueType peek() const;
ValueType & top();
Returns the value of top element from this stack, without removing it. This method signals an error if called on an empty stack. For compatibility with the STL classes, this method is also exported under the name top, in which case it returns the value by reference.

Usage:

ValueType top = stack.peek();

string toString();
Converts the stack to a printable string representation.

Usage:

string str = stack.toString();