-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1_Stack_Implementation.py
More file actions
51 lines (41 loc) · 1.26 KB
/
1_Stack_Implementation.py
File metadata and controls
51 lines (41 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Implementation of Stack Data Structure
""" A stack is an abstract data type that holds an ordered, linear sequence of items.
In contrast to a queue, a stack is a last in, first out (LIFO) structure.
A real-life example is a stack of plates: you can only take a plate from the top of the stack,
and you can only add a plate to the top of the stack. """
class Stack:
def __init__(self):
self.stack = list()
self.maxsize = 10
self.top = -1
def push(self, data):
if self.top == self.maxsize - 1:
print("Stack is Full")
else:
self.top += 1
self.stack.append(data)
def pop(self):
if self.top == -1:
print("Stack is EMPTY!!")
else:
item = self.stack.pop()
self.top = -1
print("Deleted item is: ", item)
def display(self):
print(self.stack)
Obj = Stack()
while(True):
print("\n1. PUSH")
print("2. POP")
print("3. DISPLAY")
print("4. EXIT")
choice = int(input("Enter your choice: "))
if(choice == 1):
value = int(input("Enter the data: "))
Obj.push(value)
elif choice == 2:
Obj.pop()
elif choice == 3:
Obj.display()
else:
break