1. Stacks
A Stack is a Last In First Out (LIFO) data structure. It supports two basic operations
called push and pop. The push operation adds an element at the top of the stack,
and the pop operation removes an element from the top of the stack.
2. Java provides a Stack class which models the Stack data structure. The
Stack class is part of Java’s collections framework.
3. What can we do with a stack?
push - place an item on the stack
peek - Look at the item on top of the stack, but do not remove it
pop - Look at the item on top of the stack and remove it
A
X
R
C
push(M)
A
X
R
C
M
item = pop()
item = M
A
X
R
C
4. Real life examples –
Pile of plates on a dinner table.
Browser uses stack to maintain back and forth movement of user on the
pages of a website.
To reverse a word You push a given word to stack - letter by letter - and
then pop letters from the stack.
Backtracking
Suppose we are finding a path for solving maze problem. We choose a
path and after following it we realize that it is wrong. Now we need to go
back to the beginning of the path to start with new path. This can be
done with the help of stack.
5. Expression Evaluation
Stack is used to evaluate prefix, postfix and infix expressions.
Expression Conversion
An expression can be represented in prefix, postfix or infix notation. Stack
can be used to convert one form of expression to another.
For Example:
Input : Prefix : *+AB-CD
Output : Infix : ((A+B)*(C-D))
https://codeburst.io/conversion-of-infix-expression-to-postfix-expression-using-
stack-data-structure-3faf9c212ab8
7. What is a queue?
A data structure of ordered items such that items can be inserted only at one end and
removed at the other end.
A queue is called a FIFO (First in-First out) data structure.
Java provides a Queue interface which is part of Java’s collections framework.
8. Example
A line at the supermarket
What can we do with a queue?
Enqueue - Add an item to the queue
Dequeue - Remove an item from the queue
What are some applications of queues?
online waiting system
Input/Output processing
Queueing of packets for delivery in networks