|
| 1 | +"""Problem Description: |
| 2 | +Implementation of stack using Linked List. |
| 3 | +
|
| 4 | +A stack can be easily implemented through linked list. |
| 5 | +In this implementation, |
| 6 | +the top pointer of the stack is “head” of the linked list where pushing and popping items take place. |
| 7 | +First node have null in link field and second node link have first node address in link field. |
| 8 | +It continues so on and last node address in “top” pointer. |
| 9 | +
|
| 10 | +The main advantage of using linked list over an arrays in stack implementation is- |
| 11 | +it can shrink or grow as much as needed unlike fixed size of array |
| 12 | +
|
| 13 | +The main Stack Operations are: |
| 14 | +
|
| 15 | +->push() : Insert the element into linked list nothing but which is the top node of Stack. |
| 16 | +->pop() : Return top element from the Stack and move the top pointer to the second node of linked list or Stack. |
| 17 | +->peek()/top(): Return the top element. |
| 18 | +->display(): Print all element of Stack. |
| 19 | +
|
| 20 | +[Reference for Description]->(https://www.geeksforgeeks.org/implement-a-stack-using-singly-linked-list/) |
| 21 | +""" |
| 22 | + |
| 23 | + |
| 24 | +class Node: |
| 25 | + def __init__(self, data): |
| 26 | + self.data = data |
| 27 | + self.ref = None |
| 28 | + |
| 29 | + |
| 30 | +class Stack: |
| 31 | + def __init__(self): |
| 32 | + self.__head = None |
| 33 | + self.__size = 0 |
| 34 | + |
| 35 | + def push(self, data): |
| 36 | + # adds an element at the beginning |
| 37 | + self.__size += 1 |
| 38 | + new_node = Node(data) |
| 39 | + new_node.ref = self.__head |
| 40 | + self.__head = new_node |
| 41 | + |
| 42 | + def pop(self): |
| 43 | + # deletes element from beginning |
| 44 | + if self.isEmpty(): |
| 45 | + print("Stack is empty.") |
| 46 | + return |
| 47 | + self.__size -= 1 |
| 48 | + print(self.__head.data) |
| 49 | + self.__head = self.__head.ref |
| 50 | + |
| 51 | + def top(self): |
| 52 | + # prints the topmost element, i.e, head in the case of LL |
| 53 | + if self.isEmpty(): |
| 54 | + print("Stack is empty.") |
| 55 | + return |
| 56 | + print(self.__head.data) |
| 57 | + |
| 58 | + def isEmpty(self): |
| 59 | + return(self.__size == 0) |
| 60 | + |
| 61 | + def display(self): |
| 62 | + # displays the stack from top to bottom |
| 63 | + n = self.__head |
| 64 | + if n is None: |
| 65 | + print("Empty!") |
| 66 | + return |
| 67 | + while n is not None: |
| 68 | + print(n.data) |
| 69 | + n = n.ref |
| 70 | + |
| 71 | + |
| 72 | +if __name__ == "__main__": |
| 73 | + s2 = Stack() |
| 74 | + print("Enter values to push in the stack: ") |
| 75 | + value = [ele for ele in input().split()] |
| 76 | + for i in range(len(value)): |
| 77 | + s2.push(value[i]) |
| 78 | + print("The stack is:") |
| 79 | + s2.display() |
| 80 | + print('\nThe first element which is removed is: ') |
| 81 | + s2.pop() |
| 82 | + print('\nThe topmost element of the stack is:') |
| 83 | + s2.top() |
| 84 | + print("\nThe stack is:") |
| 85 | + s2.display() |
| 86 | + |
| 87 | + |
| 88 | +"""Time-Complexity of the program: |
| 89 | +push(): O(1) |
| 90 | +pop(): O(1) |
| 91 | +top(): O(1) |
| 92 | +display(): O(N) |
| 93 | +""" |
| 94 | + |
| 95 | +"""Test-Cases: |
| 96 | +
|
| 97 | +>>> |
| 98 | +Enter values to push in the stack: |
| 99 | +10 15 23 42 |
| 100 | +The stack is: |
| 101 | +42 |
| 102 | +23 |
| 103 | +15 |
| 104 | +10 |
| 105 | +
|
| 106 | +The first element which is removed is: |
| 107 | +42 |
| 108 | +
|
| 109 | +The topmost element of the stack is: |
| 110 | +23 |
| 111 | +
|
| 112 | +The stack is: |
| 113 | +23 |
| 114 | +15 |
| 115 | +10 |
| 116 | +
|
| 117 | +>>> |
| 118 | +Enter values to push in the stack: |
| 119 | +Anamika |
| 120 | +The stack is: |
| 121 | +Anamika |
| 122 | +
|
| 123 | +The first element which is removed is: |
| 124 | +Anamika |
| 125 | +
|
| 126 | +The topmost element of the stack is: |
| 127 | +Stack is empty. |
| 128 | +
|
| 129 | +The stack is: |
| 130 | +Empty! |
| 131 | +
|
| 132 | +>>> |
| 133 | +Enter values to push in the stack: |
| 134 | +Anamika 1 QWERTY 22 key 87 |
| 135 | +The stack is: |
| 136 | +87 |
| 137 | +key |
| 138 | +22 |
| 139 | +QWERTY |
| 140 | +1 |
| 141 | +Anamika |
| 142 | +
|
| 143 | +The first element which is removed is: |
| 144 | +87 |
| 145 | +
|
| 146 | +The topmost element of the stack is: |
| 147 | +key |
| 148 | +
|
| 149 | +The stack is: |
| 150 | +key |
| 151 | +22 |
| 152 | +QWERTY |
| 153 | +1 |
| 154 | +Anamika |
| 155 | +""" |
0 commit comments