Title: Queues Author: Penelope Hofsdal Last modified by: George Bebis Created Date: 2/6/2001 12:43:16 AM Document presentation format: On-screen Show (4:3) – PowerPoint PPT presentation
private int front, rear ItemType items int maxQue 5 Implementation Issues
Optimize memory usage.
Conditions for a full or empty queue.
Initialize front and rear.
linear array circular array 6 Optimize memory usage 7 Full/Empty queue conditions 8 Make front point to the element preceding the front element in the queue! NOW! 9 Initialize front and rear 10 Array-based Implementation (cont.)
templateltclass ItemTypegt
QueueTypeltItemTypegtQueueType(int max)
maxQue max 1
front maxQue - 1
rear maxQue - 1
items new ItemTypemaxQue
O(1) 11 Array-based Implementation (cont.)
templateltclass ItemTypegt
QueueTypeltItemTypegtQueueType()
delete items
O(1) 12 Array-based Implementation (cont.)
templateltclass ItemTypegt
void QueueTypeltItemTypegtMakeEmpty()
front maxQue - 1
rear maxQue - 1
O(1) 13 Array-based Implementation (cont.)
templateltclass ItemTypegt
bool QueueTypeltItemTypegtIsEmpty() const
return (rear front)
templateltclass ItemTypegt
bool QueueTypeltItemTypegtIsFull() const
return ( (rear 1) maxQue front)
O(1) O(1) 14 Enqueue (ItemType newItem)
Function Adds newItem to the rear of the queue.
Preconditions Queue has been initialized and is not full.
Postconditions newItem is at rear of queue.
15 Queue overflow
The condition resulting from trying to add an element onto a full queue.
Exercise 37 Implement a client function that returns the number of items in a queue. The queue is unchanged.
int Length(QueueType queue)
Function Determines the number of items in the queue.
Precondition queue has been initialized.
Postconditions queue is unchanged
You may not assume any knowledge of how the queue is implemented.
47
int Length(QueType queue)
QueType tempQ
ItemType item
int length 0
while (!queue.IsEmpty())
queue.Dequeue(item)
tempQ.Enqueue(item)
length
while (!tempQ.IsEmpty())
tempQ.Dequeue(item)
queue.Enqueue(item)
return length
What are the time requirements using big-O? O(N) 48
int Length(QueType queue)
QueType tempQ
ItemType item
int length 0
while (!queue.IsEmpty())
queue.Dequeue(item)
tempQ.Enqueue(item)
length
while (!tempQ.IsEmpty())
tempQ.Dequeue(item)
queue.Enqueue(item)
return length
How would you implement it as member function? What would be the time requirements in this case using big-O? Case 1 array-based rear - front maxQue (front rear) O(1) 49
int Length(QueType queue)
QueType tempQ
ItemType item
int length 0
while (!queue.IsEmpty())
queue.Dequeue(item)
tempQ.Enqueue(item)
length
while (!tempQ.IsEmpty())
tempQ.Dequeue(item)
queue.Enqueue(item)
return length
How would you implement it as member function? What would be the time requirements in this case using big-O? Case 2 linked-list-based O(N)
PowerShow.com is a leading presentation sharing website. It has millions of presentations already uploaded and available with 1,000s more being uploaded by its users every day. Whatever your area of interest, here you’ll be able to find and view presentations you’ll love and possibly download. And, best of all, it is completely free and easy to use.
You might even have a presentation you’d like to share with others. If so, just upload it to PowerShow.com. We’ll convert it to an HTML5 slideshow that includes all the media types you’ve already added: audio, video, music, pictures, animations and transition effects. Then you can share it with your target audience as well as PowerShow.com’s millions of monthly visitors. And, again, it’s all free.
About the Developers
PowerShow.com is brought to you by CrystalGraphics, the award-winning developer and market-leading publisher of rich-media enhancement products for presentations. Our product offerings include millions of PowerPoint templates, diagrams, animated 3D characters and more.