# Stack Class

Topics
Overview | Stack 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 singly linked 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 SLListBase 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.

Stack<TYPE>::Stack()

- Default class constructor that calls the SLListBase base class to constructor a new singly linked list.

Stack<TYPE>::~Stack()

- Class destructor responsible for removing all nodes from the stack.

Stack<TYPE>::Stack(const Stack<TYPE> &X)

- Class copy constructor used to copy construct a new list that will contain a copy of the specified list.

void Stack<TYPE>::operator=(const Stack<TYPE> &X)

- Overloaded assignment operator used to assign this list to the specified list. Traps assignment to itself and does not support chained assignment.

int Stack<TYPE>::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.

int Stack<TYPE>::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 Stack<TYPE>::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.

void Stack<TYPE>::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 *Stack<TYPE>::Top()

- Public member function that returns a pointer to the top node in the stack.

const TYPE *Stack<TYPE>::Top() const

- Public member function that returns a pointer to the top node in the stack.

TYPE *Stack<TYPE>::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.

const TYPE *Stack<TYPE>::Look(unsigned Index) const

- 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.

friend int Stack<TYPE>::List(const Stack<TYPE> &X)

- Friend function used to display all the nodes in the stack to the console. This function is used for test purposes only.

virtual SNode<TYPE> *Stack<TYPE>::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 in case a derived list allocates nodes some other way.

virtual SNodeBase *Stack<TYPE>::DupNode(const SNodeBase *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 SNode type. This function is declared virtual in case a derived list allocates nodes some other way.

virtual void Stack<TYPE>::FreeNode(SNodeBase *Node)

- Protected member function, overriding the base class version, used to delete the specified node. This function assumes that the specified node is a SNode type. This function is declared virtual in case a derived list de-allocates nodes some other way.

End Of Document