Stacks and Queues

15.5 - 15.6

Stacks and Queues

Abstract Data Type (ADT)

Integers as data types

Boolean as a data type

Java objects as data types

Abstract Data Type

Abstract Data type

Abstract Data Type

Array ADT

Array ADT
An array Abstract Data Type is a ordered sequence of elements. It provides random access to a specific element in O(1) time by specifying an integer index.

List ADT

List ADT
A list Abstract Data Type is a ordered sequence of elements that can be traversed sequentually and allows for insertion and removal of elements at any position in O(1) time.

Stack

Stack ADT

Stack ADT
A stack Abstract Data Type is a collection of elements that allows access only at one end, called the top. It is structured such that the last element added will be the first element removed.

Stack Operations

Stacks in the Java Library

Working with Stacks

Stack<Integer> s = new Stack<>(); Constructs an empty stack.
s.push(1);
s.push(2);
s.push(3);
Adds to the top of the stack; s is now [1, 2, 3]. (Following the toString method of the Stack class, we show the top of the stack at the end.)
int top = s.pop(); Removes the top of the stack; top is set to 3 and s is now [1, 2].
head = s.peek(); Gets the top of the stack without removing it; head is set to 2.
boolean b = s.empty() Sets b to false. Same as s.size() == 0.

Queue

A Queue

Queue ADT

Queue ADT
A queue Abstract Data Type is a collection of ordered elements structured such that the first element added will be the first element removed.

Queue Operations

Queues in the Java Library

Working with Queues

Working with Queues

Queue<Integer> q = new LinkedList<>(); The LinkedList class implements the Queue interface.
q.add(1); q.add(2); q.add(3); Adds to the tail of the queue; q is now [1, 2, 3]
int head = q.remove(); Removes the head of the queue; head is set to 1 and q is [2, 3]
head = q.peek(); Gets the head of the queue without removing it; head is set to 2.
boolean b = s.empty() Sets b to false. Same as s.size() == 0.

Priority Queues

priority queue

Priority Queue

Priority Queue Example

Look at this code

PriorityQueue<WorkOrder> q = new PriorityQueue<WorkOrder>();
q.add(new WorkOrder(3, "Shampoo carpets"));
q.add(new WorkOrder(1, "Fix broken sink"));
q.add(new WorkOrder(2, "Order cleaning supplies"));

Priority Queue Example cont.

Call q.remove() for the first time

Working with Priority Queues

PriorityQueue<Integer> q = new PriorityQueue<>(); This priority queue holds Integer objects. In practice, you would use objects that describe tasks.
q.add(3); q.add(1); q.add(2); Adds values to the priority queue.
int first = q.remove();
int second = q.remove();
Each call to remove removes the most urgent item: first is set to 1, second to 2.
int next = q.peek();






Thoughts to ponder

Class Question 1

What does this code print?

Stack<Integer> s = new Stack<>();
Queue<Integer> q = new LinkedList<>();
PriorityQueue<Integer> p = new PriorityQueue<>();
s.push(1); s.push(3); s.push(2);
while (s.size() > 0) q.add(s.pop());
while (q.size() > 0) p.add(q.remove());
while (p.size() > 0) System.out.print(p.remove() + " ");
  1. 1 3 2
  2. 1 2 3
  3. 3 2 1
  4. 2 3 1


Stacks and Queues: Uses in Computer Science

Application: Balancing Parentheses

Application: Balancing Parentheses

When you see an opening parenthesis, push it on the stack.
When you see a closing parenthesis, pop the stack.
If the opening and closing parentheses don’t match
   The parentheses are unbalanced. Exit.
If at the end the stack is empty
   The parentheses are balanced.
Else
   The parentheses are not balanced.

Application: Balancing Parentheses

.

Evaluating Reverse Polish Expressions

.

Reverse Polish Notation

If you read a number
   Push it on the stack.
Else if you read an operand
   Pop two values off the stack.
   Combine the values with the operand.
   Push the result back onto the stack.
Else if there is no more input
   Pop and display the result.

Reverse Polish Notation


Lecture 15 Clicker Question 2

How do you write (3 + 4) x 5 + 6 in Reverse Polish?

  1. 3 4 5 x + 6 +
  2. 3 4 + 5 x 6 +
  3. 3 + 4 5 x + 6
  4. None of the above

Evaluating Algebraic Expressions

Operator Precedence

Parentheses

Complete Algorithm

If you read a number
	 Push it on the number stack.
Else if you read a (
	 Push it on the operator stack.
Else if you read an operator op
	 While the top of the stack has a higher precedence than op
		 Evaluate the top.
	 Push op on the oper­ator stack.
Else if you read a )
	 While the top of the stack is not a (
		 Evaluate the top.
	 Pop the (.
Else if there is no more input
	 While the operator stack is not empty
		 Evaluate the top.

Backtracking

Maze Escape Example


Maze Escape Pseudocode

Push all paths from the point on which you are standing on a stack.
While the stack is not empty
	 Pop a path from the stack.
	 Follow the path until you reach an exit, intersection, or dead end.
	 If you found an exit
		 Congratulations!
	 Else if you found an intersection
		 Push all paths meeting at the intersection, except the current one, onto the stack.