Implement a stack using singly linked listTo implement a stack using singly linked list concept , all the singly linked list operations are 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 single linked list. Using singly linked lists , we implement stack by storing the information in the form of nodes and we need to follow the stack rules and implement using singly linked list nodes . So we need to follow a simple rule in the implementation of a stack which is last in first out and all the operations can be performed with the help of a top variable .Let us learn how to perform Pop , Push , Peek ,Display operations in the following article . Show A stack can be easily implemented using 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.
Below is the implementation of the above approach: C++
Java
Python3
C#
Javascript
Output: 44->33->22->11-> Top element is 44 22->11-> Top element is 22 Time Complexity: The time complexity for all push(), pop(), and peek() operations is O(1) as we are not performing any kind of traversal over the list. We perform all the operations through the current pointer only.
Article Tags :
Linked List Stack Technical Scripter
Technical Scripter 2018 Practice Tags :
Linked List Stack Python Program to Implement a Stack using Linked ListPythonServer Side ProgrammingProgramming When it is required to implement a stack data structure using a linked list, a method to add (push values) elements to the linked list, and a method to delete (pop values) the elements of the linked list are defined. Below is a demonstration for the same − C
DownloadRun Code How to implement stack and queue using linked listData Structures are used to store large amounts of data in an efficient and categorical manner. For example in lists, arrays, tuples, dictionaries, and sets. We are now gonna look into Linear Data Structures:
Linked List Node ClassI will continue to use the Node Class mentioned in the previous tutorials. Each Node instance will contain a value and a pointer to the next Node instance in the linked list. To simplify things, I am storing integers in each Node, but a linked list can store any type of data. class Node(object): def __init__(self, value: int, next_node: "Node" = None): self.value = value self.next = next_node def __repr__(self): return "Node <{}>".format(self.value)Stack ClassA stack is a Last-In, First-Out (LIFO) abstract data type. The last value added to the stack will be the first value removed from the stack. My stack is implemented by a Stack Class and offers four common operations.
All these operations are performed in constant time, O(1), by pushing and popping values from the head of the linked list. Here is one solution to implementing a Stack abstract data type using Python. class Stack(object): def __init__(self): self.head = None def push(self, value: int) -> None: self.head = Node(value, self.head) def pop(self) -> int: if self.head is None: raise EmptyStackException("Pop from empty stack.") value = self.head.value self.head = self.head.next return value def peek(self) -> int: if self.head is None: raise EmptyStackException("Peek from empty stack.") return self.head.value def is_empty(self) -> bool: return self.head is None |