Chapter 16 Stack and Queues part2 Dr. Bernard Chen Ph.D. University of Central Arkansas.

30
Chapter 16 Stack and Queues part2 Dr. Bernard Chen Ph.D. University of Central Arkansas

Transcript of Chapter 16 Stack and Queues part2 Dr. Bernard Chen Ph.D. University of Central Arkansas.

Page 1: Chapter 16 Stack and Queues part2 Dr. Bernard Chen Ph.D. University of Central Arkansas.

Chapter 16 Stack and Queues part2

Dr. Bernard Chen Ph.D.University of Central Arkansas

Page 2: Chapter 16 Stack and Queues part2 Dr. Bernard Chen Ph.D. University of Central Arkansas.

Introduction to Queues A queue is a waiting line

It’s 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

Page 3: Chapter 16 Stack and Queues part2 Dr. Bernard Chen Ph.D. University of Central Arkansas.

Introduction to Queues

Difference between Stack and Queues:

Stack exhibits last-in-first-out (LIFO)

Queue exhibits first-in-first-out (FIFO)

Page 4: Chapter 16 Stack and Queues part2 Dr. Bernard Chen Ph.D. University of Central Arkansas.

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

Page 5: Chapter 16 Stack and Queues part2 Dr. Bernard Chen Ph.D. University of Central Arkansas.

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)

Page 6: Chapter 16 Stack and Queues part2 Dr. Bernard Chen Ph.D. University of Central Arkansas.

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 queueobject like this

Page 7: Chapter 16 Stack and Queues part2 Dr. Bernard Chen Ph.D. University of Central Arkansas.

Queue Operation Empty Queue

Enqueue(70)

Page 8: Chapter 16 Stack and Queues part2 Dr. Bernard Chen Ph.D. University of Central Arkansas.

Queue Operation

Enqueue(80)

Enqueue(50)

Page 9: Chapter 16 Stack and Queues part2 Dr. Bernard Chen Ph.D. University of Central Arkansas.

Queue Operation

Dequeue()

Dequeue()

Page 10: Chapter 16 Stack and Queues part2 Dr. Bernard Chen Ph.D. University of Central Arkansas.

Queue Operation

Enqueue(90)

Enqueue(60)

Page 11: Chapter 16 Stack and Queues part2 Dr. Bernard Chen Ph.D. University of Central Arkansas.

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 queuegives myBack == myFront

Page 12: Chapter 16 Stack and Queues part2 Dr. Bernard Chen Ph.D. University of Central Arkansas.

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

Page 13: Chapter 16 Stack and Queues part2 Dr. Bernard Chen Ph.D. University of Central Arkansas.

Circular Example

Both Front and Back wraparound as needed.

b c d

FrontBack

b c d

FrontBack

e f

e fg

Page 14: Chapter 16 Stack and Queues part2 Dr. Bernard Chen Ph.D. University of Central Arkansas.

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

Page 15: Chapter 16 Stack and Queues part2 Dr. Bernard Chen Ph.D. University of Central Arkansas.

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

Page 16: Chapter 16 Stack and Queues part2 Dr. Bernard Chen Ph.D. University of Central Arkansas.

Queue Operation Construct:

Create an array, set capacity, myFront=myBack=0

Empty:test myFront==myBack

Front :if not empty:

print array[myFront]

Page 17: Chapter 16 Stack and Queues part2 Dr. Bernard Chen Ph.D. University of Central Arkansas.

Algorithm for Enqueue(value) 1. Set newBack ==

(myBack+1)%Queue_capacity

2. If newBack == myFrontSignal “Queue Full”

otherwise:Set Array[myBack] == valueSet myBack == newBack

Page 18: Chapter 16 Stack and Queues part2 Dr. Bernard Chen Ph.D. University of Central Arkansas.

Algorithm for Dequeue() If queue is empty

signal “Queue Empty”Otherwise

Set myFront=(myFront+1)%Queue_capacity

Page 19: Chapter 16 Stack and Queues part2 Dr. Bernard Chen Ph.D. University of Central Arkansas.

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)

Page 20: Chapter 16 Stack and Queues part2 Dr. Bernard Chen Ph.D. University of Central Arkansas.

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

Page 21: Chapter 16 Stack and Queues part2 Dr. Bernard Chen Ph.D. University of Central Arkansas.

Enqueue

newptr= new Node(value)if (empty())

myFront=myBack=newptr;

else {

myBack->next=newptr;myBack=newwptr;

}

Page 22: Chapter 16 Stack and Queues part2 Dr. Bernard Chen Ph.D. University of Central Arkansas.

Dequeue

(if not empty)ptr=myFrontmyFront=myFront->nextdelete ptr;

Page 23: Chapter 16 Stack and Queues part2 Dr. Bernard Chen Ph.D. University of Central Arkansas.

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)

Page 24: Chapter 16 Stack and Queues part2 Dr. Bernard Chen Ph.D. University of Central Arkansas.

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();

Page 25: Chapter 16 Stack and Queues part2 Dr. Bernard Chen Ph.D. University of Central Arkansas.

Dequeue

Dequeue (remove element from front)

L.erase( L.begin() );

L.begin() is an iterator, in erase(), you cannot just gives the location

Page 26: Chapter 16 Stack and Queues part2 Dr. Bernard Chen Ph.D. University of Central Arkansas.

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());

Page 27: Chapter 16 Stack and Queues part2 Dr. Bernard Chen Ph.D. University of Central Arkansas.

QUEUE

Write a queue program

Page 28: Chapter 16 Stack and Queues part2 Dr. Bernard Chen Ph.D. University of Central Arkansas.

Vector Functions

Page 29: Chapter 16 Stack and Queues part2 Dr. Bernard Chen Ph.D. University of Central Arkansas.
Page 30: Chapter 16 Stack and Queues part2 Dr. Bernard Chen Ph.D. University of Central Arkansas.

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()