Introduction
A stack is memory in which values are stored and retrieved in "last in first out" manner by using operations called push and pop.
Program
Suppose you want to insert values in a stack and retrieve values from the stack. The operations would proceed in the following manner:
Explanation
-
Initially, the stack is empty. When you start push A, A is placed in the stack.
-
Similarly, push B and push C put these elements in the stack; the last element pushed is C.
-
The pop operation takes the topmost element from the stack. Thus the element C, which was put in last, is retrieved first. This method is called last-in first-out (LIFO).
-
The push D operation puts element D in the stack above B.
-
Thus push puts the element on the top of the stack and pop takes the element from the top of the stack. The element A which is pushed is the last element taken from the stack.
No comments:
Post a Comment