Representation of a Stack using a Linked List

Stack using LinkedList in C++


One of the basic linear user defined data structure that is also very useful to solve problems and implement tons of Graph theory algorithms is Stack. It follows the last in first out model and cann be implemented using LinkedList if you want to get its size changing dynamically. This is anarticle on the Representation of a Stack using a Linked List.

Representation of a Stack using LinkedList in C++ | PrepInsta
Stack using an LinkedlIst
Representation of a stack using linked list

What is Stack?


Stack is a data structure where you can add any kind of and store then in a LIFO (last in first out) manner.

If you want to know about The given stack Data structure in a C++ STL,here it goes: Stack in C++ STL


The Algorithm Goes here: 

So here goes the algorithm about How to use a linked list to implement a stack. It is one of the easiest algorithms, if you know what a linked list is. Here we used the inbuilt STL List (List in C++ STL) as a linked list inside a ‘Stack’ named class, otherwise you can make a class or structure with pointers to make a Linked List.

  • Firstly, This is a LinkedList, where only the last element is visible. So you have only one place open to change in the list, that is the topmost node of the list.
  • So if you want to push values, you have to push them at the back. Only if the list is empty, you can push the first value in the first node.
  • If you want to pop values, only the topmost or the last pushed place is accessible. If the list is empty, no values can be popped, otherwise, the momentary topmost value is to be popped.
  • That is how you can achieve the model of Last in First Out.
  • So Here a class is made with a list member. Then values are added only at the back, same goes for the popping.
Stack using a LinkedList in C++

The code to implement this:

using namespace std;

class Stack
  list<int> L;
  void Push(int i)
    cout<<“Pushing the element : “<<(i)<<endl;
  int pop()
    if(L.empty()) {cout<<“The Stack is empty”<<endl;}
    int a=L.back();
    cout<<“Popping the element : “<<a<<endl;
    return a;
  void Show()
    cout<<“The present Stack is:”<<endl;
    for(auto i:L)
    cout<<i<<” “;

int main()
  Stack s;


Pushing the element : 5
Pushing the element : 2
Pushing the element : 11
The present Stack is:
5 2 11
Popping the element : 11
Pushing the element : 12
The present Stack is:
5 2 12

Article is contributed by Rahit Saha