Chapter 10: Stacks and Queues Reading: Bailey Chapter 10 6/28/2016

advertisement
Chapter 10: Stacks and Queues
Reading: Bailey Chapter 10
6/28/2016
Introduction to Stacks
• A stack is a last-in-first-out (LIFO) data
structure
• Adding an item
– Referred to as pushing it onto the stack
• Removing an item
– Referred to as
popping it from
the stack
2
A Stack
• Definition:
– An ordered collection of data items
– Can be accessed at only one end (the top)
• Operations:
– construct a stack (usually empty)
– check if it is empty
– Push: add an element to the top
– Peek: retrieve the top element
– Pop: remove the top element
3
Bailey’s Interface for Stack
Selecting Storage Structure
• We could use an array for our Stack
– Let position 0 be top of stack
• Problem … consider pushing and popping
– Requires much shifting
5
Selecting Storage Structure
• A better approach is to let position 0 be the
bottom of the stack
• Thus our design will include
– An array to hold the stack elements
– An integer to indicate
the top of the stack
Nyhoff, ADTs, Data Structures and
Problem Solving with C++, Second
Edition, © 2005 Pearson Education,
Inc. All rights reserved. 0-13-140909-
6
We can also use a Vector (Lab demo)
• Advantages:
– Works like array -- O(1) for push and pop
– can be resized
• Disadvantages:
– A little slower than an array
– Time to push an item could take O(N) when resizing
6/28/2016
We can also use a Linked List
(your homework)
• Advantages:
– Can resize indefinitely, no big delay as in Vector
– O(1) push and pop
• Disadvantages:
– Extra memory to store a link for every data element
– Push and pop though O(1) will be a little slower
than array due to time needed to allocate or
deallocate a node
6/28/2016
What your StackLink will look like:
• Stack s = new Stack();
•
•
•
•
•
s.push("a");
s.push("b");
s.push("c");
s.pop();
s.pop();
6/28/2016
Application of Stacks
Consider events when a function begins
execution
• Activation record (or stack frame) is created
• Stores the current environment for that
function.
• Contents:
Nyhoff, ADTs, Data Structures and
Problem Solving with C++, Second
Edition, © 2005 Pearson Education,
Inc. All rights reserved. 0-13-140909-
10
Run-time Stack
• Functions may call other functions
– interrupt their own execution
• Must store the activation records to be
recovered
– system then reset when first function resumes
execution
• This algorithm must have LIFO behavior
• Structure used is the run-time stack
Nyhoff, ADTs, Data Structures and
Problem Solving with C++, Second
Edition, © 2005 Pearson Education,
Inc. All rights reserved. 0-13-140909-
11
Use of Run-time Stack
When a function is called …
• Copy of activation record pushed onto run-time
stack
• Arguments copied into parameter spaces
• Control transferred to starting address of body of
function
Nyhoff, ADTs, Data Structures and
Problem Solving with C++, Second
Edition, © 2005 Pearson Education,
Inc. All rights reserved. 0-13-140909-
12
Use of Run-time Stack
When function terminates
• Run-time stack popped
– Removes activation record of terminated function
– exposes activation record of previously executing function
• Activation record used to restore environment of
interrupted function
• Interrupted function resumes execution
Nyhoff, ADTs, Data Structures and
Problem Solving with C++, Second
Edition, © 2005 Pearson Education,
Inc. All rights reserved. 0-13-140909-
13
Introduction to Queues
• A queue is a waiting line – seen in daily life
– A line of people waiting for a bank teller
– A line of cars at a toll both
– "This is the captain, we're 5th in line for takeoff"
• What other kinds of queues can you think of
Nyhoff, ADTs, Data Structures and
Problem Solving with C++, Second
Edition, © 2005 Pearson Education,
Inc. All rights reserved. 0-13-140909-
14
The Queue As an ADT
• A queue is a sequence of data elements
• In the sequence
– Items can be removed only at the front
– Items can be added only at the other end, the back
• Basic operations
–
–
–
–
–
Construct a queue
Check if empty
Enqueue (add element to back)
Front (retrieve value of element from front)
Dequeue (remove element
from front)
Nyhoff, ADTs, Data Structures and
Problem Solving with C++, Second
Edition, © 2005 Pearson Education,
Inc. All rights reserved. 0-13-140909-
15
Bailey Queue Interface
Designing and Building a Queue Class
Array-Based
• Consider an array in which to store a queue
• Note additional variables needed
– myFront, myBack
• Picture a queue
object like this
Nyhoff, ADTs, Data Structures and
Problem Solving with C++, Second
Edition, © 2005 Pearson Education,
Inc. All rights reserved. 0-13-140909-
17
Designing and Building a Queue Class
Array-Based
• Problems
– We quickly "walk off the end" of the array
• Possible solutions
– Shift array elements
– Use a circular queue
– Note that both empty
and full queue
gives myBack == myFront
Nyhoff, ADTs, Data Structures and
Problem Solving with C++, Second
Edition, © 2005 Pearson Education,
Inc. All rights reserved. 0-13-140909-
18
Designing and Building a Queue Class
Array-Based vs Vector Based
• Using a ‘circular’ array:
– enqueue and dequeue are O(1)
– However, array size is fixed
• Using a Vector (without circular organization)
– Easy to implement
– enqueue O(1) except during resizing, O(N)
– dequeue O(N) due to need to shift data down one
19
Linked Queues (Your homework)
• Even with dynamic allocation of queue size
– Array size is still fixed
– Cannot be adjusted during run of program
• Could use linked list to store queue elements
– Can grow and shrink to fit the situation
– No need for upper bound (myCapacity)
Nyhoff, ADTs, Data Structures and
Problem Solving with C++, Second
Edition, © 2005 Pearson Education,
Inc. All rights reserved. 0-13-140909-
20
Circular Linked List
• Possible to treat the linked list as circular
– Last node points back to first node
– Alternatively keep pointer to last node rather than
first node
Nyhoff, ADTs, Data Structures and
Problem Solving with C++, Second
Edition, © 2005 Pearson Education,
Inc. All rights reserved. 0-13-140909-
21
Application of Queues: Buffers and
Scheduling
• Important use of queues is I/O scheduling
– Use buffers in memory to improve program
execution
– Buffer arranged
in FIFO
structure
22
Application of Queues: Buffers and
Scheduling
• Consider a keyboard buffer
– Acts as a queue
– But elements may be
removed from the back of
the queue with
backspace key
• A printer spool is a queue of print jobs
Nyhoff, ADTs, Data Structures and
Problem Solving with C++, Second
Edition, © 2005 Pearson Education,
Inc. All rights reserved. 0-13-140909-
23
Application of Queues: Buffers and
Scheduling
• Queues used to
schedule tasks
within an operating
system
• Job moves from disk to ready queue
Nyhoff, ADTs, Data Structures and
Problem Solving with C++, Second
Edition, © 2005 Pearson Education,
Inc. All rights reserved. 0-13-140909-
24
Application of Queues: Buffers and
Scheduling
• Ready queue may actually
be a priority queue … job may get to "cut the
line" based on its priority
Nyhoff, ADTs, Data Structures and
Problem Solving with C++, Second
Edition, © 2005 Pearson Education,
Inc. All rights reserved. 0-13-140909-
25
Queue vs Stack – Maze Running
• Stack is “depth first”
– Goes as far as it can, then “backtracks” when stuck
• Queue is “breadth first”
– Explores multiple pathways simultaneously
6/28/2016
Application I
• Stack Based Language
– Common way of calculating (reverse polish notation)
– Command line interpreter (unix and dos)
– Example:
1 1 add 3 mul 4 sub pop
[ displays the value: 2]
– Each piece above is a “token”
• A Token can be
6/28/2016
Download