Open In App

How to implement a Stack using list in C++ STL

Last Updated : 14 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss how to implement a Stack using list in C++ STL.

Stack is a linear data structure which follows. LIFO(Last In First Out) or FILO(First In Last Out). It mainly supports 4 major operations:
1. Push: Push an element into the stack.
2. Pop: Removes the element by following the LIFO order.
3. Top: Returns the element present at the top of the stack.
4. Empty: Returns whether the stack is empty or not.
 

Below is the implementation of the above approach:

C++




// C++ implementation of stack
// using list STL
#include <bits/stdc++.h>
using namespace std;
  
template <typename T>
// templating it so that any data type can be used
  
class Stack {
public:
    list<T> l;
    int cs = 0;
    // current size of the stack
  
    // pushing an element into the stack
    void push(T d)
    {
        cs++;
        // increasing the current size of the stack
        l.push_front(d);
    }
  
    // popping an element from the stack
    void pop()
    {
        if (cs <= 0) {
            // cannot pop us stack does not contain an
            // elements
            cout << "Stack empty" << endl;
        }
        else {
            // decreasing the current size of the stack
            cs--;
            l.pop_front();
        }
    }
  
    // if current size is 0 then stack is empty
    bool empty() { return cs == 0; }
  
    // getting the element present at the top of the stack
    T top() { return l.front(); }
    int size()
    {
        // getting the size of the stack
        return cs;
    }
  
    // printing the elements of the stack
    void print()
    {
        for (auto x: l) {
            cout << x << endl;
        }
    }
};
int main()
{
    Stack<int> s;
    s.push(10); // pushing into the stack
    s.push(20);
    s.push(30);
    s.push(40);
    cout << "Current size of the stack is " << s.size()
         << endl;
    cout << "The top element of the stack is " << s.top()
         << endl;
    s.pop(); // popping from the stack
    cout << "The top element after 1 pop operation is "
         << s.top()
         << endl; // printing the top of the stack
    s.pop(); // popping
    cout << "The top element after 2 pop operations is "
         << s.top() << endl;
    cout << "Size of the stack after 2 pop operations is "
         << s.size() << endl;
    return 0;
}


Output

Current size of the stack is 4
The top element of the stack is 40
The top element after 1 pop operation is 30
The top element after 2 pop operations is 20
Size of the stack after 2 pop operations is 2

Time Complexity: O(1) for both push and pop operations in the stack.
Auxiliary Space: O(N)



Previous Article
Next Article

Similar Reads

stack empty() and stack size() in C++ STL
Stacks are a type of container adaptors with LIFO(Last In First Out) type of working, where a new element is added at one end and (top) an element is removed from that end only. stack::empty()empty() function is used to check if the stack container is empty or not. Syntax : stackname.empty()Parameters :No parameters are passed.Returns :True, if sta
3 min read
Implement a stack using singly linked list
To implement a stack using the singly linked list concept, all the singly linked list operations should be performed based on Stack operations LIFO(last in first out) and with the help of that knowledge, we are going to implement a stack using a singly linked list. So we need to follow a simple rule in the implementation of a stack which is last in
15 min read
How to implement Min Heap using STL?
In C++ STL, there is priority_queue that can directly be used to implement Max Heap. In order to fully understand the code, make sure you are familiar with following concepts in C++ Container Adaptors in STLFunctors See below example: C/C++ Code // C++ program to show that priority_queue is by // default a Max Heap #include &lt;bits/stdc++.h&gt; us
4 min read
How to implement Stack and Queue using ArrayDeque in Java
ArrayDeque in Java The ArrayDeque in Java provides a way to apply resizable-array in addition to the implementation of the Deque interface. It is also known as Array Double Ended Queue or Array Deck. This is a special kind of array that grows and allows users to add or remove an element from both sides of the queue. ArrayDeque class implements Queu
6 min read
Implement Dynamic Multi Stack (K stacks) using only one Data Structure
In this article, we will see how to create a data structure that can handle multiple stacks with growable size. The data structure needs to handle three operations: push(x, stackNum) = pushes value x to the stack numbered stackNumpop(stackNum) = pop the top element from the stack numbered stackNumtop(stackNum) = shows the topmost element of the sta
16 min read
Implement a stack using single queue
We are given queue data structure, the task is to implement stack using only given queue data structure.We have discussed a solution that uses two queues. In this article, a new solution is discussed that uses only one queue. This solution assumes that we can find size of queue at any point. The idea is to keep newly inserted element always at fron
6 min read
Implement a Stack Using Vectors in C++
A stack is a data structure that follows the LIFO (Last In First Out) property means the element that is inserted at last will come out first whereas vectors are dynamic arrays. In this article, we will learn how to implement a stack using vectors in C++. Implementing a Stack Using Vectors in C++Vectors offer dynamic resizing capabilities, allowing
2 min read
Implement Stack using Array
Stack is a linear data structure which follows LIFO principle. In this article, we will learn how to implement Stack using Arrays. In Array-based approach, all stack-related operations are executed using arrays. Let’s see how we can implement each operation on the stack utilizing the Array Data Structure. Implement Stack using Array:To implement a
9 min read
Implement Stack and Queue using Deque
Deque also known as double ended queue, as name suggests is a special kind of queue in which insertions and deletions can be done at the last as well as at the beginning. A link-list representation of deque is such that each node points to the next node as well as the previous node. So that insertion and deletions take constant time at both the beg
15 min read
Implement Stack using Queues
Given a Queue data structure that supports standard operations like enqueue() and dequeue(). The task is to implement a Stack data structure using only instances of Queue and Queue operations allowed on the instances.   Recommended PracticeStack using two queuesTry It!A Stack can be implemented using two queues. Let Stack to be implemented be 's' a
34 min read
Article Tags :
Practice Tags :