Stacks
Introduction to Stacks
Stacks are a specific type of abstract data type (ADT).
Analogy of Stacks with Balls
Example using numbered balls as data:
Balls are in numerical order from smallest to largest.
Program adds balls (data) to a container in increasing order:
Adds ball 1, then ball 2, then ball 3, and ball 4.
Retrieval of data demonstrates how the type of container affects order.
Data Retrieval from Different Containers
First experiment using an open container:
Balls retrieved were:
Two
Four
Three
One
Observed retrieval was not in a specific order due to the lack of structure in the container.
Second experiment using a structured container:
Balls are added in order:
Ball 1, Ball 2, Ball 3, Ball 4.
Retrieval yields:
Four
Three
Two
One
This demonstrates that structure affects retrieval order.
Characteristics of a Stack
The stack is represented as a structured container with one open end.
Data Manipulation in a Stack:
Push: The process of adding data to the stack.
Pop: The process of removing data from the stack.
Last In, First Out (LIFO) Principle:
The last piece of data pushed into the stack is the first piece to be popped out.
Importance of Stacks in Computer Science
Stacks help in reversing the order of data inputs.
They serve as a foundational concept for solving various problems in computer science.
Class discussions will explore multiple examples highlighting the utility of stacks.