STACK
1 Data Structure -> A data structure is a specialized format for organizing,
processing, retrieving and storing data.
1 STACK is a type of data structure.
2 It follows LIFO pattern.
3 LIFO is Last In First Out.
4 Insert and delete operations are only allowed at one end of the stack, called the
top of the stack.
1 Real life examples of stack include ->
2 1. A stack of plates
3 2. A stack of books
4 3. A stack of coins
5 4. Bangles
1 Practical applications in Computer Science ->
2 1. Undo-redo operations
3 2. Browser fwd & back button
4 3. Function calls
IMPLEMENTATION OF STACK IN PYTHON
In [3]:
1 #Define a global list to be used as stack
2 S = []
3
4 #PUSH
5 def Push(S,item):
6 S.append(item) # add item at top of stack
7 print("Pushed Successfully!")
8
9
10 def isEmpty(S):
11 if len(S)==0:
12 return True
13 else:
14 return False
15
16
17 def Pop(S):
18 if isEmpty(S):
19 return None
20 else:
21 item = S.pop() # pop element at -1 index
22 return item
23
24
25 def main():
26 menu = """
27 1. Push
28 2. Pop
29 3. Display
30 4. Peek
31 5. Exit
32 """
33 while True:
34 print(menu)
35 ch = input("Enter your choice : ")
36 if ch=='1':
37 item = input("Enter Player name : ")
38 Push(S,item)
39 elif ch=='2':
40 item = Pop(S)
41 if item == None:
42 print("Stack is Empty!!")
43 else:
44 print(item, "was popped.")
45 elif ch=='3':
46 print(S)
47 elif ch=='4':
48 item = Peek(S)
49 if item == None:
50 print("Stack is Empty!!")
51 else:
52 print(item, " is at Top.")
53 elif ch=='5':
54 break
55 else:
56 print("Invalid Input")
In [4]:
1 main()
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 3
[]
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 2
Stack is Empty!!
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 1
Enter Player name : Sanju Samson
Pushed Successfully!
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 3
['Sanju Samson']
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 1
Enter Player name : S Bharat
Pushed Successfully!
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 1
Enter Player name : Jadeja
Pushed Successfully!
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 1
Enter Player name : Gill
Pushed Successfully!
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 3
['Sanju Samson', 'S Bharat', 'Jadeja', 'Gill']
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 2
Gill was popped.
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 3
['Sanju Samson', 'S Bharat', 'Jadeja']
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 2
Jadeja was popped.
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 2
S Bharat was popped.
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 2
Sanju Samson was popped.
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 2
Stack is Empty!!
1. Push
2. Pop
3. Display
4. Exit
Enter your choice : 4
Adding Peek and Display Functions
In [8]:
1 #Define a global list to be used as stack
2 S = []
3
4 #PUSH
5 def Push(S,item):
6 S.append(item) # add item at top of stack
7 print("Pushed Successfully!")
8
9
10 def isEmpty(S):
11 if len(S)==0:
12 return True
13 else:
14 return False
15
16
17 def Pop(S):
18 if isEmpty(S):
19 return None
20 else:
21 item = S.pop() # pop element at -1 index
22 return item
23
24 def Peek(S):
25 if isEmpty(S):
26 return None
27 else:
28 item = S[-1] # Display element at -1 index
29 return item
30
31
32 def Display(S):
33 if isEmpty(S):
34 print("Stack is Empty")
35 else:
36 print(S[-1], "<-- TOP")
37 for i in range(-2, -len(S)-1, -1):
38 print(S[i])
39
40 def main():
41 menu = """
42 1. Push
43 2. Pop
44 3. Display
45 4. Peek
46 5. Exit
47 """
48 while True:
49 print(menu)
50 ch = input("Enter your choice : ")
51 if ch=='1':
52 item = input("Enter Player name : ")
53 Push(S,item)
54 elif ch=='2':
55 item = Pop(S)
56 if item == None:
57 print("Stack is Empty!!")
58 else:
59 print(item, "was popped.")
60 elif ch=='3':
61 Display(S)
62 elif ch=='4':
63 item = Peek(S)
64 if item == None:
65 print("Stack is Empty!!")
66 else:
67 print(item, " is at Top.")
68 elif ch=='5':
69 break
70 else:
71 print("Invalid Input")
In [10]:
1 main()
1. Push
2. Pop
3. Display
4. Peek
5. Exit
Enter your choice : 1
Enter Player name : Sanju samson
Pushed Successfully!
1. Push
2. Pop
3. Display
4. Peek
5. Exit
Enter your choice : 1
Enter Player name : S Bharat
Pushed Successfully!
1. Push
2. Pop
3. Display
4. Peek
5. Exit
Enter your choice : 1
Enter Player name : Axar
Pushed Successfully!
1. Push
2. Pop
3. Display
4. Peek
5. Exit
Enter your choice : 1
Enter Player name : Jadeja
Pushed Successfully!
1. Push
2. Pop
3. Display
4. Peek
5. Exit
Enter your choice : 1
Enter Player name : Gill
Pushed Successfully!
1. Push
2. Pop
3. Display
4. Peek
5. Exit
Enter your choice : 1
Enter Player name : Dhoni
Pushed Successfully!
1. Push
2. Pop
3. Display
4. Peek
5. Exit
Enter your choice : 3
EXAM Question
Dhoni <-- TOP
Gill
Jadeja
1 1. Define a function to push all those elements from a list, which are divisible
Axar by 3 and 5 to a stack. Also define a function to pop and display the elements.
S 2Bharat
Sanju
3 L samson
= [15,20,30,45,50,60]
4
5 1. Push
6 2. Pop
7 3. Display
4. Peek
5. Exit
Enter
In [12]:
your choice : 2
Dhoni was popped.
1 S = []
2 1.
defPush
PushFromList(S, L):
3 2. Popfor item in L:
4 3. Displayif item%3==0 and item%5==0:
5 4. Peek S.append(item)
6 5. Exit
7 def PopAndDisplay():
8
Enter while
your True:
choice : 3
9
Gill <-- TOP try:
10
Jadeja item = S.pop()
11
Axar print(item)
S12Bharat except:
13
Sanju samson print("No more elements")
14 break
1. Push
2. Pop
In [13]:
3. Display
4. Peek
1 marks = [15,20,30,45,50,60]
5. Exit
Enter your choice : 4
In [14]:
Gill is at Top.
1 PushFromList(S, marks)
1. Push
2. Pop
3. Display
In [15]:
4. Peek
1 5.
S Exit
Enter your choice : 3
Out[15]:
Gill <-- TOP
Jadeja
[15, 30, 45, 60]
Axar
S Bharat
Sanju samson
1. Push
2. Pop
3. Display
4. Peek
In [16]:
5. Exit
1 PopAndDisplay()
Enter your choice : 5
60
45
30
15
No more elements
2. define a function Push to add a list containing book number and book name to a stack
In [17]:
1 def Push(S):
2 bno = int(input("Enter book Number : "))
3 bname = input("Enter book name : ")
4 item = [bno,bname]
5 S.append(item)
6 print("Pushed")
In [18]:
1 S = []
In [20]:
1 Push(S)
Enter book Number : 1
Enter book name : ABC
Pushed
In [21]:
1 Push(S)
Enter book Number : 2
Enter book name : DEF
Pushed
In [22]:
1 Push(S)
Enter book Number : 3
Enter book name : GHI
Pushed
In [23]:
1 print(S)
[[1, 'ABC'], [2, 'DEF'], [3, 'GHI']]
In [24]:
1 def printSelect(S):
2 for item in S:
3 if item[1][0].lower()=='d':
4 print(item)
In [25]:
1 printSelect(S)
[2, 'DEF']
In [ ]: