C++: Stack implementation using LinkedList (Data Structure)

We have already learnt about Stack in the post C++: Implementation of Stack using Array (Data Structure).  Now we are implementing Stack using Linked List which we have learnt in C++: Singly Linked List using Template (Data Structure). The C++ program for implementing Stack using Linked List is very simple. For push() and pop() function we have to use insert() and deleteNode() function from Linked List program.


Here is the program:
#include <iostream>
template<class T>class Stack{ struct Node{ T data; Node *next; Node(T val):data(val),next(nullptr) {} }; Node *head; int top;
 public:     Stack(){     top=-1;     head=nullptr;     }    ~Stack();     bool stack_empty();     void push(T);     T pop();
     void display(std::ostream& out = std::cout) const     {       Node *node=head;       while(node!=nullptr){        out<<node->data<<" ";        node=node->next;       }     }};
template<class T>bool Stack<T>::stack_empty(){  if(top==-1)    return true;  else    return false;}
template<class T>void Stack<T>::push(T val){    top++;    Node *node=new Node(val);    node->next = head;    head=node;}
template<class T>T Stack<T>::pop(){  if(stack_empty()==true)    std::cout<<"Underflow"<<std::endl;  else{    top--;    Node *node=head;    head = node->next;    T tmp = std::move(node->data);    delete node;    return tmp;  }}
template<class U>std::ostream & operator <<(std::ostream& os, const Stack<U>& s){s.display(os);return os;}
template<class T>Stack<T>::~Stack(){Node *tmp=nullptr;  while(head){    tmp=head;    head=head->next;    delete tmp;  }  head=nullptr;}
int main(){ Stack<int> stack1; stack1.push(3); stack1.push(4); stack1.push(11); stack1.push(30); std::cout<<stack1<<std::endl; std::cout<<stack1.pop()<<std::endl;    return 0;}


Remember to use delete to delete a node to avoid memory leak.

 Follow us on
                              

                                                                  
                                                                  

Reference:


You may also like:
C++: Swapping Node LInks in Linked List
C++: Understanding Template
What is "namespace" and why do we use it?
C++ STL :Implementation of Counting sort (Sorting)
C++: Queue implementation using Linked List (Data Structure)

Comments

Popular Posts

Top 10 Web Sites Every Programmer Should Visit Daily