Lecture12 StackQueue
Lecture12 StackQueue
Topics Topics
Stacks (Chapter 7) Queues (Chapter 8, Section1 - 3) Priority Queues (Chapter 8, Section 4) References Return Values (Chapter 8, Section 5)
Stacks and the STL stack Stacks and the STL stack
Definition
A stack is a data structure of ordered entries such that entries can be inserted and removed at only one end (call the top) A stack is a Last-In/First-Out data structure. Entries are taken out of the stack in the reverse order of their insertion
push in : CHAD pop out : C
LIFO
Stacks and the STL stack Stacks and the STL stack
Definition
A stack is a data structure of ordered entries such that entries can be inserted and removed at only one end (call the top) A stack is a Last-In/First-Out data structure. Entries are taken out of the stack in the reverse order of their insertion
push in : CHAD pop out : CH
LIFO
Stacks and the STL stack Stacks and the STL stack
Definition
A stack is a data structure of ordered entries such that entries can be inserted and removed at only one end (call the top) A stack is a Last-In/First-Out data structure. Entries are taken out of the stack in the reverse order of their insertion
push in : CHAD pop out : CHA
LIFO
Stacks and the STL stack Stacks and the STL stack
Definition
A stack is a data structure of ordered entries such that entries can be inserted and removed at only one end (call the top) A stack is a Last-In/First-Out data structure. Entries are taken out of the stack in the reverse order of their insertion
push in : CHAD pop out : CHAD
LIFO
Stacks and the STL stack Stacks and the STL stack
Definition
A stack is a data structure of ordered entries such that entries can be inserted and removed at only one end (call the top) A stack is a Last-In/First-Out data structure. Entries are taken out of the stack in the reverse order of their insertion
push in : CHAD pop out :D CHA
LIFO
Stacks and the STL stack Stacks and the STL stack
Definition
A stack is a data structure of ordered entries such that entries can be inserted and removed at only one end (call the top) A stack is a Last-In/First-Out data structure. Entries are taken out of the stack in the reverse order of their insertion
push in : CHAD pop out :DA CH
LIFO
Stacks and the STL stack Stacks and the STL stack
Definition
A stack is a data structure of ordered entries such that entries can be inserted and removed at only one end (call the top) A stack is a Last-In/First-Out data structure. Entries are taken out of the stack in the reverse order of their insertion
push in : CHAD pop out : DAH C
LIFO
Stacks and the STL stack Stacks and the STL stack
Definition
A stack is a data structure of ordered entries such that entries can be inserted and removed at only one end (call the top) A stack is a Last-In/First-Out data structure. Entries are taken out of the stack in the reverse order of their insertion
push in : CHAD pop out : DAHC
LIFO
Stacks and the STL stack Stacks and the STL stack
a container class holding many items a template class stack of integers, strings, ... #include <stack> stack<int> s1; Three ways: fixed-size or dynamic array, or linked list? STL typically uses dynamic array Functions: push, pop, empty, size , top
How to use
Queues and the STL queue Queues and the STL queue
Definition
A queue is a data structure of ordered entries such that entries can only be inserted at one end (call the rear) and removed at the other end (call the front) and the entry at the front of the queue is called the first entry A queue is a First-In/First-Out data structure. Entries are taken out of the queue in the same order that they were put into the queue
DAHC take out : CHAD
FIFO
put in : CHAD
Queues and the STL queue Queues and the STL queue
Definition
A queue is a data structure of ordered entries such that entries can only be inserted at one end (call the rear) and removed at the other end (call the front) and the entry at the front of the queue is called the first entry A queue is a First-In/First-Out data structure. Entries are taken out of the queue in the same order that they were put into the queue
DAHC take out : CHAD
FIFO
put in : CHAD
Queues and the STL queue Queues and the STL queue
Definition
A queue is a data structure of ordered entries such that entries can only be inserted at one end (call the rear) and removed at the other end (call the front) and the entry at the front of the queue is called the first entry A queue is a First-In/First-Out data structure. Entries are taken out of the queue in the same order that they were put into the queue
DAHC take out : CHAD
FIFO
put in : CHAD
Queues and the STL queue Queues and the STL queue
Definition
A queue is a data structure of ordered entries such that entries can only be inserted at one end (call the rear) and removed at the other end (call the front) and the entry at the front of the queue is called the first entry A queue is a First-In/First-Out data structure. Entries are taken out of the queue in the same order that they were put into the queue
DAHC take out : CHAD
FIFO
put in : CHAD
Queues and the STL queue Queues and the STL queue
Definition
A queue is a data structure of ordered entries such that entries can only be inserted at one end (call the rear) and removed at the other end (call the front) and the entry at the front of the queue is called the first entry A queue is a First-In/First-Out data structure. Entries are taken out of the queue in the same order that they were put into the queue
DAHC take out : CHAD
FIFO
put in : CHAD
Queues and the STL queue Queues and the STL queue
Definition
A queue is a data structure of ordered entries such that entries can only be inserted at one end (call the rear) and removed at the other end (call the front) and the entry at the front of the queue is called the first entry A queue is a First-In/First-Out data structure. Entries are taken out of the queue in the same order that they were put into the queue
DAHC take out : CHAD
FIFO
put in : CHAD
Queues and the STL queue Queues and the STL queue
Definition
A queue is a data structure of ordered entries such that entries can only be inserted at one end (call the rear) and removed at the other end (call the front) and the entry at the front of the queue is called the first entry A queue is a First-In/First-Out data structure. Entries are taken out of the queue in the same order that they were put into the queue
DAHC take out : CHAD
FIFO
put in : CHAD
Queues and the STL queue Queues and the STL queue
Definition
A queue is a data structure of ordered entries such that entries can only be inserted at one end (call the rear) and removed at the other end (call the front) and the entry at the front of the queue is called the first entry A queue is a First-In/First-Out data structure. Entries are taken out of the queue in the same order that they were put into the queue
DAHC take out : CHAD
FIFO
put in : CHAD
Queues and the STL queue Queues and the STL queue
a container class holding many items a template class queue of integers, strings, ... #include <queue> queue<char> q1; fixed-size or dynamic array, or linked list? STL typically uses dynamic array Functions: push, pop, empty, size, front
How to use
A priority queue is a container class that allows entries to be retrieved according to some specified priority levels.
The highest priority entry is removed first Entries with equal priority can be removed according some criterion e.g. FIFO as an queue. #include <queue> priority_queue<int> q2; Functions push, pop, empty, size , top (not front!) Several ways to specify priority (p. 411)
Reference Return Values for the Reference Return Values for the
stack, queue, and priority queue classes stack, queue, and priority queue classes
In STL, the top (for stack) and front (for queue) functions have reference return values, e.g. in stack class definition:
1. int i = b.top(); V V
Chapter 7 introduces the stack data type. Several example applications of stacks are given in that chapter. This presentation shows another use called backtracking to solve the N-Queens problem.
Presentation copyright 1997, Addison Wesley Longman, For use with Data Structures and Other Objects Using C++ by Michael Main and Walter Savitch. Some artwork in the presentation is used with permission from Presentation Task Force (copyright New Vision Technologies Inc) and Corel Gallery Clipart Catalog (copyright Corel Corporation, 3G Graphics Inc, Archive Arts, Cartesia Software, Image Club Graphics Inc, One Mile Up Inc, TechPool Studios, Totem Graphics Inc). Students and instructors who use Data Structures and Other Objects Using C++ are welcome to use this presentation however they see fit, so long as this copyright notice remains intact.
N Queens
ROW 1, COL 1
filled
filled
filled
filled
filled
filled
filled
filled
filled
filled
filled
filled
filled
filled
(how?)
Increase filled by 1. If filled is now N, then the algorithm is done. Otherwise, move to the next row and place a queen in the first column.
The eight queens puzzle has 92 distinct solutions. If solutions that differ only by symmetry operations (rotations and reflections) of the board are counted as one, the puzzle has 12 unique solutions.
352 724
Stacks have many applications. The application which we have shown is called backtracking. The key to backtracking: Each choice is recorded in a stack. When you run out of choices for the current decision, you pop the stack, and continue trying different choices for the previous decision.