CSCI2320 Chapter 16 Part2
Transcript of CSCI2320 Chapter 16 Part2
-
8/11/2019 CSCI2320 Chapter 16 Part2
1/30
Chapter 16 Stack and Queuespart2
Dr. Bernard Chen Ph.D.
University of Central Arkansas
-
8/11/2019 CSCI2320 Chapter 16 Part2
2/30
Introduction to Queues A queue is a waiting line
Its in daily life: A line of persons waiting to check out at a
supermarket
A line of persons waiting to purchase a ticket for afilm
A line of planes waiting to take off at an airport
A line of vehicles at a toll booth
-
8/11/2019 CSCI2320 Chapter 16 Part2
3/30
Introduction to Queues Difference between Stack and
Queues:
Stack exhibits last-in-first-out (LIFO)
Queue exhibits first-in-first-out (FIFO)
-
8/11/2019 CSCI2320 Chapter 16 Part2
4/30
ADT in Queues Unlike stacks in which elements are popped
and pushed only at the ends of the list,
Collection of data elements:
items are removed from a queue at one end,
called the FRONT of the queue; and elements are added at the other end,
called the BACK
-
8/11/2019 CSCI2320 Chapter 16 Part2
5/30
Queue ADT 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)
-
8/11/2019 CSCI2320 Chapter 16 Part2
6/30
Designing and Building a Queue
Class Array-Based Consider an array in which to store a
queue
Note additional variables neededmyFront, myBack
Picture a queueobject like this
-
8/11/2019 CSCI2320 Chapter 16 Part2
7/30
Queue Operation Empty Queue
Enqueue(70)
-
8/11/2019 CSCI2320 Chapter 16 Part2
8/30
Queue Operation Enqueue(80)
Enqueue(50)
-
8/11/2019 CSCI2320 Chapter 16 Part2
9/30
Queue Operation Dequeue()
Dequeue()
-
8/11/2019 CSCI2320 Chapter 16 Part2
10/30
Queue Operation Enqueue(90)
Enqueue(60)
-
8/11/2019 CSCI2320 Chapter 16 Part2
11/30
Circular Queue Problems
We quickly "walk off the end" of the array
Possible solutions
Shift array elements
Use a circular queue
Note that both emptyand full queue
givesmyBack == myFront
-
8/11/2019 CSCI2320 Chapter 16 Part2
12/30
Circular Queue Using a static array
QUEUE_CAPACITYspecified
Enqueue incrementsmyBackusing mod
operator, checks for full queue
Dequeue incrementsmyFrontusing mod
operator, checks for empty queue
-
8/11/2019 CSCI2320 Chapter 16 Part2
13/30
Circular Example
Both Front and Back wraparound asneeded.
b c d
FrontBack
b c d
FrontBack
e f
e fg
-
8/11/2019 CSCI2320 Chapter 16 Part2
14/30
QUEUE Only tricky part is vector doubling because
the queue items are not necessarily stored inan array starting at location 0, and the
contiguity of wraparound must bemaintained.
Therefore, mostly straightforward; maintain Front Back
-
8/11/2019 CSCI2320 Chapter 16 Part2
15/30
Queue Full Situation If an item were stored in the last position, and an Enqueure()
occurred
myBack would be incremented by 1, giving it the same value as
myFront However, myFront == myBack indicates the queue is empty Thus, we cannot distinguish between emptyand full
We may avoid this situation by maintaining one empty position,
so that myFront will never equal to myBack unless the queue isempty
-
8/11/2019 CSCI2320 Chapter 16 Part2
16/30
Queue Operation
Construct:Create an array, set capacity,
myFront=myBack=0
Empty:test myFront==myBack
Front :if not empty:print array[myFront]
-
8/11/2019 CSCI2320 Chapter 16 Part2
17/30
Algorithm for Enqueue(value)
1. Set newBack ==(myBack+1)%Queue_capacity
2. If newBack == myFrontSignal Queue Full
otherwise:Set Array[myBack] == value
Set myBack == newBack
-
8/11/2019 CSCI2320 Chapter 16 Part2
18/30
Algorithm for Dequeue()
If queue is empty
signal Queue Empty
Otherwise
Set myFront=(myFront+1)%Queue_capacity
-
8/11/2019 CSCI2320 Chapter 16 Part2
19/30
Linked Queues
We could also use linked list to store queue
elements Can grow and shrink to fit the situation
No need for upper bound (myCapacity)
-
8/11/2019 CSCI2320 Chapter 16 Part2
20/30
Linked Queues
Constructor initializesmyFront,myBack
Empty myFront == Null
Front return myFront->data
Dequeue Delete first node (watch for empty queue)
Enqueue Insert node at end of list
-
8/11/2019 CSCI2320 Chapter 16 Part2
21/30
Enqueue
newptr= new Node(value)
if (empty())
myFront=myBack=newptr;
else
{
myBack->next=newptr;
myBack=newwptr;
}
-
8/11/2019 CSCI2320 Chapter 16 Part2
22/30
Dequeue
(if not empty)
ptr=myFront
myFront=myFront->next
delete ptr;
-
8/11/2019 CSCI2320 Chapter 16 Part2
23/30
Queue ADT implement byVectors
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)
-
8/11/2019 CSCI2320 Chapter 16 Part2
24/30
-
8/11/2019 CSCI2320 Chapter 16 Part2
25/30
Dequeue
Dequeue (remove element from front)
L.erase( L.begin() );
L.begin() is an iterator, in erase(), youcannot just gives the location
-
8/11/2019 CSCI2320 Chapter 16 Part2
26/30
Functions related to Queue
Constructor: vector L;
Empty(): L.size() == 0?
Enqueue(): L.push_back(value); Front(): L.begin();
Dequeue(): L.erase(L.begin());
-
8/11/2019 CSCI2320 Chapter 16 Part2
27/30
QUEUE
Write a queue program
-
8/11/2019 CSCI2320 Chapter 16 Part2
28/30
Vector Functions
-
8/11/2019 CSCI2320 Chapter 16 Part2
29/30
-
8/11/2019 CSCI2320 Chapter 16 Part2
30/30
Queue + Stack
SCROLL: a queue-stack hybrid model
Thus, you may have functions in both
Push(value)=Enqueue(value)Pop(); Top() --- top of stack
Dequeue(); Front()