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
- Difference between Singly linked list and Doubly linked list
- Convert singly linked list into circular linked list
- Convert Singly Linked List to XOR Linked List
- Sum of the nodes of a Singly Linked List
- QuickSort on Singly Linked List
- Circular Singly Linked List | Insertion
- Alternate Odd and Even Nodes in a Singly Linked List
- Alternating split of a given Singly Linked List | Set 1
- Binary Search on Singly Linked List
- Product of the nodes of a Singly Linked List
- Insertion Sort for Singly Linked List
- Find middle of singly linked list Recursively
- Difference between a Static Queue and a Singly Linked List
- Count of Prime Nodes of a 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 firstname.lastname@example.org. 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.
Improved By : 29AjayKumar