1
+ import os
2
+
3
+
4
+ class _Node :
5
+ '''
6
+ Creates a Node with two fields:
7
+ 1. element (accesed using ._element)
8
+ 2. link (accesed using ._link)
9
+ '''
10
+ __slots__ = '_element' , '_link'
11
+
12
+ def __init__ (self , element , link ):
13
+ '''
14
+ Initialses _element and _link with element and link respectively.
15
+ '''
16
+ self ._element = element
17
+ self ._link = link
18
+
19
+
20
+ class StackLL :
21
+ '''
22
+ Consists of member funtions to perform different
23
+ operations on the linked list.
24
+ '''
25
+
26
+ def __init__ (self ):
27
+ '''
28
+ Initialses head, tail and size with None, None and 0 respectively.
29
+ '''
30
+ self ._head = None
31
+ self ._tail = None
32
+ self ._size = 0
33
+
34
+ def __len__ (self ):
35
+ '''
36
+ Returns length of stack.
37
+ '''
38
+ return self ._size
39
+
40
+ def isempty (self ):
41
+ '''
42
+ Returns True if stack is empty, otherwise False.
43
+ '''
44
+ return self ._size == 0
45
+
46
+ def push (self , e ):
47
+ '''
48
+ Pushes the passed element at the beginning of the linked list.
49
+ That means, on the top of our stack.
50
+ '''
51
+ newest = _Node (e , None )
52
+
53
+ if self .isempty ():
54
+ self ._head = newest
55
+ self ._tail = newest
56
+ else :
57
+ newest ._link = self ._head
58
+ self ._head = newest
59
+ self ._size += 1
60
+
61
+ def pop (self ):
62
+ '''
63
+ Removes element from the beginning of the linked list.
64
+ That means, pops element that is on the top.
65
+ Returns the removed element.
66
+ '''
67
+ if self .isempty ():
68
+ print ("Stack is Empty. Cannot perform Pop operation." )
69
+ return
70
+
71
+ e = self ._head ._element
72
+ self ._head = self ._head ._link
73
+ self ._size = self ._size - 1
74
+
75
+ if self .isempty ():
76
+ self ._tail = None
77
+
78
+ return e
79
+
80
+ def top (self ):
81
+ '''
82
+ Peeks at the element on the top of the stack.
83
+ '''
84
+ if self .isempty ():
85
+ print ("Stack is Empty. Cannot perform Pop operation." )
86
+ return
87
+
88
+ e = self ._head ._element
89
+ return e
90
+
91
+ def display (self ):
92
+ '''
93
+ Utility function to display the Stack.
94
+ '''
95
+ if self .isempty () == 0 :
96
+ p = self ._head
97
+ while p :
98
+ print (p ._element )
99
+ p = p ._link
100
+ else :
101
+ print ("Empty" )
102
+
103
+ ###############################################################################
104
+
105
+ def options ():
106
+ '''
107
+ Prints Menu for operations
108
+ '''
109
+ options_list = ['Push' , 'Pop' , 'Top' ,
110
+ 'Display Stack' , 'Exit' ]
111
+
112
+ print ("MENU" )
113
+ for i , option in enumerate (options_list ):
114
+ print (f'{ i + 1 } . { option } ' )
115
+
116
+ choice = int (input ("Enter choice: " ))
117
+ return choice
118
+
119
+
120
+ def switch_case (choice ):
121
+ '''
122
+ Switch Case for operations
123
+ '''
124
+ os .system ('cls' )
125
+ if choice == 1 :
126
+ elem = int (input ("Enter Item: " ))
127
+ S .push (elem )
128
+
129
+ elif choice == 2 :
130
+ print ('Popped item is: ' , S .pop ())
131
+
132
+ elif choice == 3 :
133
+ print ("Item on top is: " , S .top ())
134
+
135
+ elif choice == 4 :
136
+ print ("Stack:" )
137
+ S .display ()
138
+ print ("\n " )
139
+
140
+ elif choice == 5 :
141
+ import sys
142
+ sys .exit ()
143
+
144
+ ###############################################################################
145
+
146
+
147
+ S = StackLL ()
148
+ while True :
149
+ choice = options ()
150
+ switch_case (choice )
0 commit comments