(Week-10 Manual) CS-242-DSA - Stack
(Week-10 Manual) CS-242-DSA - Stack
Week-10 Manual
Stack
v1.0
6/11/2019
Week 10 Manual 2021
Stack
In everyday life, a stack is a familiar thing. You might see a stack of books on your desk, a Stack
of dishes in the cafeteria, a stack of towels in the linen closet, or a stack of boxes in the attic.
When you add an item to a stack, you place it on top of the stack. When you remove an item, you
take the topmost one. This topmost item is the last one that was added to the stack.
So when you remove an item, you remove the item added most recently. That is, the last item
added to the stack is the first one removed.
In spite of our examples of a stack, everyday life usually does not follow this last-in, first-out, or
LIFO, behavior. Although the employee hired most recently is often the first one fired during a
layoff, we live in a first-come, first-served society. In the computer science world, however,
last-in, first-out is exactly the behavior required by many important algorithms. These algorithms
often use the abstract data type stack, which is an ADT that exhibits a last-in, first-out behavior.
For example, a compiler uses a stack to interpret the meaning of an algebraic expression, and a
run-time environment uses a stack when executing a recursive method.
Page 1
Week 10 Manual 2021
Page 2
Week 10 Manual 2021
Page 3