April 2018
Intermediate to advanced
322 pages
6h 57m
English
As we discussed earlier, we can only add a new item from the top position of the stack. Since the top variable in the Stack is similar to Head variable in the LinkedList, we can utilize the implementation of InsertHead() in the LinkedList data type to be implemented in the Push() operation in the Stack data type. The implementation of the Push() operation should be as follows:
template <typename T>void Stack<T>::Push(T val){ // Create a new Node Node<T> * node = new Node<T>(val); // The Next pointer of this new node // will point to current m_top node node->Next = m_top; // The new Node now becomes the m_top node m_top = node; // One item is added m_count++;}
The preceding code snippet is similar to the ...