Implement a stack using single linked list concept. all the single linked list operations perform based on Stack operations LIFO(last in first out) and with the help of that knowledge we are going to implement a stack using single linked list. using single linked lists so how to implement here it is linked list means what we are storing the information in the form of nodes and we need to follow the stack rules and we need to implement using single linked list nodes so what are the rules we need to follow in the implementation of a stack a simple rule that is last in first out and all the operations we should perform so with the help of a top variable only with the help of top variables are how to insert the elements let’s see
A stack can be easily implemented through the linked list. In stack Implementation, a stack contains a top pointer. which is “head” of the stack where pushing and popping items happens at the head of the list. first node have null in link field and second node link have first node address in link field and so on and last node address in “top” pointer.
The main advantage of using linked list over an arrays is that it is possible to implements a stack that can shrink or grow as much as needed. In using array will put a restriction to the maximum capacity of the array which can lead to stack overflow. Here each new node will be dynamically allocate. so overflow is not possible.
- push() : Insert the element into linked list nothing but which is the top node of Stack.
- pop() : Return top element from the Stack and move the top pointer to the second node of linked list or Stack.
- peek(): Return the top element.
- display(): Print all element of Stack.
Below is the implementation of the above approach:
44->33->22->11-> Top element is 44 22->11-> Top element is 22
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Convert singly linked list into circular linked list
- Difference between Singly linked list and Doubly linked list
- Convert Singly Linked List to XOR Linked List
- C program to create copy of a singly Linked List using Recursion
- Implement Stack using Queues
- Implement a stack using single queue
- How to implement stack using priority queue or heap?
- Implement Stack and Queue using Deque
- Given only a pointer/reference to a node to be deleted in a singly linked list, how do you delete it?
- Alternating split of a given Singly Linked List | Set 1
- Reverse alternate K nodes in a Singly Linked List
- QuickSort on Singly Linked List
- Select a Random Node from a Singly Linked List
- Circular Singly Linked List | Insertion
- Insertion Sort for Singly Linked List
- Recursive selection sort for singly linked list | Swapping node links
- Alternate Odd and Even Nodes in a Singly Linked List
- Find middle of singly linked list Recursively
- Binary Search on Singly Linked List
- C Program to reverse each node value in Singly Linked List
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.