Stack Class


Topics:

Overview
QueueStack Class


Overview

A stack is a last-in, first-out (LIFO) data structure where access to the elements take place at one end of the sequence (called the top) using push and pop operations. A push operation adds a new node to the top of the stack and a pop operation removes a node from the top of the stack.


Generic Stack Class

The Stack class presented here is a generic list based stack. It is a resizable data structure that allocates and de-allocates heap space is with every push and pop operation. The Stack class inherits the vbDLListBase base class and manages the list nodes in a last-in, first-out format. Unlike array based stacks the list-based stack can never be full. NOTE: Only a template version of the Stack class is implemented. If your application cannot use templates, code this class directly for the data type being used. Templates were used to allow the stack to handle numerous data types without having to provide a different version for each data type used.

vbStack::vbStack()
vbStack::~vbStack()
vbStack::AllocNode()
vbStack::DupNode()
vbStack::FreeNode()
vbStack::Look()
vbStack::Pop()
vbStack::Push()
vbStack::Rewind()
vbStack::Top()
vbStack::operator=()

vbStack::vbStack() - Default class constructor that calls the vbDLListBase base class to construct a new linked list.

vbStack::vbStack(const vbStack<TYPE> &X) - Class copy constructor used to copy construct a new list that will contain a copy of the specified list.

vbStack::~vbStack() - Class destructor responsible for removing all nodes from the stack.

virtual vbDNode<TYPE> *vbStack::AllocNode(const TYPE &X) - Protected member function, overriding the base class version, used to allocate a new node storing a copy of "X " with it. This function is declared virtual incase a derived list allocates nodes some other way.

virtual vbDNodeBase *vbStack::DupNode(const vbDNodeBase *Node) - Protected member function, overriding the base class version, used to copy construct a node holding the proper type of data. This function assumes that the specified node is a DNode type. This function is declared virtual incase a derived list allocates nodes some other way.

virtual void vbStack::FreeNode(vbDNodeBase *Node) - Protected member function, overriding the base class version, used to delete the specified node. This function assumes that the specified node is a DNode type. This function is declared virtual incase a derived list de-allocates nodes some other way.

TYPE *vbStack::Look(unsigned Index) - Public member function used to peek into the stack. The "Index" value specifies a specific node to look at in the stack. If the "Index" value equals zero a pointer to the data in the top node is returned. If the "Index" value is out of range a null pointer is returned.

int vbStack::Pop(TYPE &X) - Public member function used to get the data from the front node of the stack and copy it into "X" after it is detached and before it is deleted from the list. If the stack is empty, "X" is left untouched. Returns true if successful or false if the stack is empty.

int vbStack::Pop() - Public member function used to pop the top of the stack by detaching and deleting the node from the list. Returns true if successful or false if the stack is empty.

int vbStack::Push(const TYPE &X) - Public member function used to create a new node having a copy of "X" for its data and puts it on the top of the stack. Returns true if successful or false if allocation fails.

void vbStack::Rewind(unsigned Index=0) - Public member function used to pop a specified number of nodes from the stack starting from the top. If "Index"equals zero the entire stack will be cleared.

TYPE *vbStack::Top() - Public member function that returns a pointer to the top node in the stack.

void vbStack::operator=(const vbStack<TYPE> &X) - Overloaded assignment operator used to assign this list to the specified list. Traps assignment to itself and does not support chained assignment.


End Of Document