Академический Документы
Профессиональный Документы
Культура Документы
part2
Dr. Bernard Chen Ph.D.
University of Central Arkansas
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 a
film
A line of planes waiting to take off at an airport
A line of vehicles at a toll booth
Introduction to Queues
Difference between Stack and
Queues:
Stack exhibits last-in-first-out (LIFO)
Queue exhibits first-in-first-out (FIFO)
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
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)
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
Queue Operation
Empty Queue
Enqueue(70)
Queue Operation
Enqueue(80)
Enqueue(50)
Queue Operation
Dequeue()
Dequeue()
Queue Operation
Enqueue(90)
Enqueue(60)
Circular Queue
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
Circular Queue
Using a static array
QUEUE_CAPACITY specified
Enqueue increments myBack using mod
operator, checks for full queue
Dequeue increments myFront using mod
operator, checks for empty queue
Circular Example
Both Front and Back wraparound as
needed.
b c d
Front Back
b c d
Front Back
e f
e f g
QUEUE
Only tricky part is vector doubling because
the queue items are not necessarily stored in
an array starting at location 0, and the
contiguity of wraparound must be
maintained.
Therefore, mostly straightforward; maintain
Front
Back
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 empty and full
We may avoid this situation by maintaining one empty position,
so that myFront will never equal to myBack unless the queue is
empty
Queue Operation
Construct:
Create an array, set capacity,
myFront=myBack=0
Empty:
test myFront==myBack
Front :
if not empty:
print array[myFront]
Algorithm for Enqueue(value)
1. Set newBack ==
(myBack+1)%Queue_capacity
2. If newBack == myFront
Signal Queue Full
otherwise:
Set Array[myBack] == value
Set myBack == newBack
Algorithm for Dequeue()
If queue is empty
signal Queue Empty
Otherwise
Set myFront=(myFront+1)%Queue_capacity
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)
Linked Queues
Constructor initializes
myFront, myBack
Empty
myFront == Null
Front
return myFront->data
Dequeue
Delete first node (watch for empty queue)
Enqueue
Insert node at end of list
Enqueue
newptr= new Node(value)
if (empty())
myFront=myBack=newptr;
else
{
myBack->next=newptr;
myBack=newwptr;
}
Dequeue
(if not empty)
ptr=myFront
myFront=myFront->next
delete ptr;
Queue ADT implement by
Vectors
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)
Enqueue and Front
Enqueue (add element to back)
This is the same with push(), therefore:
L.push_back(value);
Front (retrieve value of element from front)
L.begin();
Dequeue
Dequeue (remove element from front)
L.erase( L.begin() );
L.begin() is an iterator, in erase(), you
cannot just gives the location
Functions related to Queue
Constructor: vector<int> L;
Empty(): L.size() == 0?
Enqueue(): L.push_back(value);
Front(): L.begin();
Dequeue(): L.erase(L.begin());
QUEUE
Write a queue program
Vector Functions
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()