Consider the following stack of characters implemented as an array of 4 elements:
Stack = ["A", "J","P","N"]
Display the Stack as the following operations take place:(a) Stack.pop()
(b) Stack.append("K")
(c) Stack.append("S")
(d) Stack.pop()
(e) Stack.append("G")
(f) Stack.pop()
Answers
Answered by
4
['A', 'J', 'P']
['A', 'J', 'P', 'K']
['A', 'J', 'P', 'K', 'S']
['A', 'J', 'P', 'K']
['A', 'J', 'P', 'K', 'G']
['A', 'J', 'P', 'K']
Answered by
1
Answer:
Operations that would take place are as follows:
Explanation:
- (a) Stack.pop(): [‘A’, ‘J’, ‘P’] the last element would be deleted since stack works on the technology of "Last-In-First-Out", and the element is deleted from the top of the stack.
- (b) Stack.append("K"): [‘A’, ‘J’, ‘P’, ‘K’] again using the same logic of LIFO, element 'K' would be added on top of the stack.
- (c) Stack.append("S"): [‘A’, ‘J’, ‘P’, ‘K’, ‘S’]
- (d) Stack.pop(): [‘A’, ‘J’, ‘P’, ‘K’]
- (e) Stack.append("G"): [‘A’, ‘J’, ‘P’, ‘K’, ‘G’]
- (f) Stack.pop(): [‘A’, ‘J’, ‘P’, ‘K’]
Hence, in this way, these operations would be performed on the stack of the characters.
#SPJ2
Similar questions