CppCommon  1.0.4.1
C++ Common Library
Classes | Public Types | Public Member Functions | Friends | List of all members
CppCommon::Stack< T > Class Template Reference

Intrusive stack container. More...

#include <stack.h>

Classes

struct  Node
 Stack node. More...
 

Public Types

typedef T value_type
 
typedef value_typereference
 
typedef const value_typeconst_reference
 
typedef value_typepointer
 
typedef const value_typeconst_pointer
 
typedef ptrdiff_t difference_type
 
typedef size_t size_type
 
typedef StackIterator< T > iterator
 
typedef StackConstIterator< T > const_iterator
 

Public Member Functions

 Stack () noexcept
 
template<class InputIterator >
 Stack (InputIterator first, InputIterator last) noexcept
 
 Stack (const Stack &) noexcept=default
 
 Stack (Stack &&) noexcept=default
 
 ~Stack () noexcept=default
 
Stackoperator= (const Stack &) noexcept=default
 
Stackoperator= (Stack &&) noexcept=default
 
 operator bool () const noexcept
 Check if the stack is not empty. More...
 
bool empty () const noexcept
 Is the stack empty? More...
 
size_t size () const noexcept
 Get the stack size. More...
 
T * top () noexcept
 Get the top stack item. More...
 
const T * top () const noexcept
 
iterator begin () noexcept
 Get the begin stack iterator. More...
 
const_iterator begin () const noexcept
 
const_iterator cbegin () const noexcept
 
iterator end () noexcept
 Get the end stack iterator. More...
 
const_iterator end () const noexcept
 
const_iterator cend () const noexcept
 
void push (T &item) noexcept
 Push a new item into the top of the stack. More...
 
T * pop () noexcept
 Pop the item from the top of the stack. More...
 
void reverse () noexcept
 Reverse the stack. More...
 
void clear () noexcept
 Clear the stack. More...
 
void swap (Stack &stack) noexcept
 Swap two instances. More...
 

Friends

template<typename U >
void swap (Stack< U > &stack1, Stack< U > &stack2) noexcept
 

Detailed Description

template<typename T>
class CppCommon::Stack< T >

Intrusive stack container.

Stack represents container with LIFO (last in - first out) stock algorithm. For example, you insert item A into stack. Then you insert item B into stack. There is an item B on the top of the stack. After removing item B, there will be an item A on the top of the stack.

Top-<--- Insert here
|
+-----+ +-----+ +-----+ +-----+
| | Next | | Next | | Next | | Next
| 1 |-------->| 2 |-------->| 3 |-------->| 4 |--------> NULL
| | | | | | | |
+-----+ +-----+ +-----+ +-----+
|
+-->--- Remove from here

Not thread-safe.

Overview

Stack

In computer science, a stack is a temporary abstract data type and data structure based on the principle of Last In First Out (LIFO). Stacks are used extensively at every level of a modern computer system. For example, a modern PC uses stacks at the architecture level, which are used to run an operating system. The operating system also uses stacks, which are used to run a Java Virtual Machine, which is stack oriented, and the Java language itself has a class called "Stack", which can be used by the programmer. The stack is ubiquitous.

A stack-based computer system is one that stores temporary information primarily in stacks, rather than hardware CPU registers (a register-based computer system).

Abstract data type
As an abstract data type, the stack is a container (data structure) of nodes and has two basic operations: push and pop. Push adds a given node to the top of the stack leaving previous nodes below. Pop removes and returns the current top node of the stack. A frequently used metaphor is the idea of a stack of plates in a spring loaded cafeteria stack. In such a stack, only the top plate is visible and accessible to the user, all other plates remain hidden. As new plates are added (pushed), each new plate becomes the top of the stack, and hides each plate below. As plates are removed (popped) from the stack, they can be used, and second plate becomes the top of the stack. Two important principles are illustrated by this metaphor, the Last In First Out principle is one. The second is that the contents of the stack are hidden. Only the top plate is visible, so to see what is on the third plate, the first and second plates will have to be removed.

Other Operations
In modern computer languages, the stack is usually implemented with more operations than just "push" and "pop". The length of a stack can often be returned as a parameter. Another helper operation peek can return the current top node of the stack without removing it from the stack.

Implementation
A typical storage requirement for a stack of n elements is O(n). The typical time requirement of O(1) operations is also easy to satisfy with an dynamic array or (singly) linked list implementation.

History
The stack method of expression evaluation was first proposed by early German computer scientist F.L. Bauer, who received the IEEE Computer Society Pioneer Award in 1988 for his work on Computer Stacks.

References

Taken from:
Stack (data structure) from Wikipedia, the free encyclopedia http://en.wikipedia.org/wiki/Stack_%28data_structure%29

Examples
containers_stack.cpp.

Definition at line 105 of file stack.h.

Member Typedef Documentation

◆ const_iterator

template<typename T >
typedef StackConstIterator<T> CppCommon::Stack< T >::const_iterator

Definition at line 117 of file stack.h.

◆ const_pointer

template<typename T >
typedef const value_type* CppCommon::Stack< T >::const_pointer

Definition at line 113 of file stack.h.

◆ const_reference

template<typename T >
typedef const value_type& CppCommon::Stack< T >::const_reference

Definition at line 111 of file stack.h.

◆ difference_type

template<typename T >
typedef ptrdiff_t CppCommon::Stack< T >::difference_type

Definition at line 114 of file stack.h.

◆ iterator

template<typename T >
typedef StackIterator<T> CppCommon::Stack< T >::iterator

Definition at line 116 of file stack.h.

◆ pointer

template<typename T >
typedef value_type* CppCommon::Stack< T >::pointer

Definition at line 112 of file stack.h.

◆ reference

template<typename T >
typedef value_type& CppCommon::Stack< T >::reference

Definition at line 110 of file stack.h.

◆ size_type

template<typename T >
typedef size_t CppCommon::Stack< T >::size_type

Definition at line 115 of file stack.h.

◆ value_type

template<typename T >
typedef T CppCommon::Stack< T >::value_type

Definition at line 109 of file stack.h.

Constructor & Destructor Documentation

◆ Stack() [1/4]

template<typename T >
CppCommon::Stack< T >::Stack ( )
inlinenoexcept

Definition at line 127 of file stack.h.

◆ Stack() [2/4]

template<typename T >
template<class InputIterator >
CppCommon::Stack< T >::Stack ( InputIterator  first,
InputIterator  last 
)
inlinenoexcept

Definition at line 13 of file stack.inl.

◆ Stack() [3/4]

template<typename T >
CppCommon::Stack< T >::Stack ( const Stack< T > &  )
defaultnoexcept

◆ Stack() [4/4]

template<typename T >
CppCommon::Stack< T >::Stack ( Stack< T > &&  )
defaultnoexcept

◆ ~Stack()

template<typename T >
CppCommon::Stack< T >::~Stack ( )
defaultnoexcept

Member Function Documentation

◆ begin() [1/2]

template<typename T >
Stack< T >::const_iterator CppCommon::Stack< T >::begin
inlinenoexcept

Definition at line 26 of file stack.inl.

◆ begin() [2/2]

template<typename T >
Stack< T >::iterator CppCommon::Stack< T >::begin
inlinenoexcept

Get the begin stack iterator.

Definition at line 20 of file stack.inl.

◆ cbegin()

template<typename T >
Stack< T >::const_iterator CppCommon::Stack< T >::cbegin
inlinenoexcept

Definition at line 32 of file stack.inl.

◆ cend()

template<typename T >
Stack< T >::const_iterator CppCommon::Stack< T >::cend
inlinenoexcept

Definition at line 50 of file stack.inl.

◆ clear()

template<typename T >
void CppCommon::Stack< T >::clear
inlinenoexcept

Clear the stack.

Definition at line 94 of file stack.inl.

◆ empty()

template<typename T >
bool CppCommon::Stack< T >::empty ( ) const
inlinenoexcept

Is the stack empty?

Definition at line 141 of file stack.h.

◆ end() [1/2]

template<typename T >
Stack< T >::const_iterator CppCommon::Stack< T >::end
inlinenoexcept

Definition at line 44 of file stack.inl.

◆ end() [2/2]

template<typename T >
Stack< T >::iterator CppCommon::Stack< T >::end
inlinenoexcept

Get the end stack iterator.

Definition at line 38 of file stack.inl.

◆ operator bool()

template<typename T >
CppCommon::Stack< T >::operator bool ( ) const
inlineexplicitnoexcept

Check if the stack is not empty.

Definition at line 138 of file stack.h.

◆ operator=() [1/2]

template<typename T >
Stack& CppCommon::Stack< T >::operator= ( const Stack< T > &  )
defaultnoexcept

◆ operator=() [2/2]

template<typename T >
Stack& CppCommon::Stack< T >::operator= ( Stack< T > &&  )
defaultnoexcept

◆ pop()

template<typename T >
T * CppCommon::Stack< T >::pop
inlinenoexcept

Pop the item from the top of the stack.

Returns
The top item popped from the stack
Examples
containers_stack.cpp.

Definition at line 64 of file stack.inl.

◆ push()

template<typename T >
void CppCommon::Stack< T >::push ( T &  item)
inlinenoexcept

Push a new item into the top of the stack.

Parameters
item- Pushed item
Examples
containers_stack.cpp.

Definition at line 56 of file stack.inl.

◆ reverse()

template<typename T >
void CppCommon::Stack< T >::reverse
inlinenoexcept

Reverse the stack.

Definition at line 77 of file stack.inl.

◆ size()

template<typename T >
size_t CppCommon::Stack< T >::size ( ) const
inlinenoexcept

Get the stack size.

Definition at line 144 of file stack.h.

◆ swap()

template<typename T >
void CppCommon::Stack< T >::swap ( Stack< T > &  stack)
inlinenoexcept

Swap two instances.

Definition at line 101 of file stack.inl.

◆ top() [1/2]

template<typename T >
const T* CppCommon::Stack< T >::top ( ) const
inlinenoexcept

Definition at line 148 of file stack.h.

◆ top() [2/2]

template<typename T >
T* CppCommon::Stack< T >::top ( )
inlinenoexcept

Get the top stack item.

Definition at line 147 of file stack.h.

Friends And Related Function Documentation

◆ swap

template<typename T >
template<typename U >
void swap ( Stack< U > &  stack1,
Stack< U > &  stack2 
)
friend

The documentation for this class was generated from the following files: