Skip to Content
C++ Data Structures and Algorithms
book

C++ Data Structures and Algorithms

by Wisnu Anggoro
April 2018
Intermediate to advanced content levelIntermediate to advanced
322 pages
6h 57m
English
Packt Publishing
Content preview from C++ Data Structures and Algorithms

Pushing the items of the Stack ADT

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

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Data Structures and Algorithms Using C++

Data Structures and Algorithms Using C++

Ananda Rao Akepogu, Radhika Raju Palagiri
C++ Data Structures and Algorithm Design Principles

C++ Data Structures and Algorithm Design Principles

John Carey, Anil Achary, Shreyans Doshi, Payas Rajan
C++ Plus Data Structures, 6th Edition

C++ Plus Data Structures, 6th Edition

Nell Dale, Chip Weems, Tim Richards

Publisher Resources

ISBN: 9781788835213Supplemental Content