06-Queues - 1 Queue 1 The Abstract Data Type Queue circle5...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 Queue 1 The Abstract Data Type Queue circle5 A queue is a list from which items are deleted from one end ( front ) and into which items are inserted at the other end ( rear, or back ) circle5 It is like line of people waiting to purchase tickets: circle5 Queue is referred to as a first-in-first-out (FIFO) data structure. circle5 The first item inserted into a queue is the first item to leave circle5 Queues have many applications in computer systems: circle5 Any application where a group of items is waiting to use a shared resource will use a queue. e.g. circle5 jobs in a single processor computer circle5 print spooling circle5 information packets in computer networks. circle5 A simulation : a study to see how to reduce the waiting time involved in an application 2 2 A Queue 3 front rear dequeue enqueue ADT Queue Operations circle5 createQueue() circle5 Create an empty queue circle5 destroyQueue() circle5 Destroy a queue circle5 isEmpty():boolean circle5 Determine whether a queue is empty circle5 enqueue(in newItem:QueueItemType) throw (QueueException) circle5 Inserts a new item at the end of the queue (at the rear of the queue) circle5 dequeue() throw QueueException dequeue(out queueFront:QueueItemType) throw (QueueException) circle5 Removes (and returns) the element at the front of the queue circle5 Remove the item that was added earliest circle5 getFront(out queueFront:QueueItemType) throw (QueueException) circle5 Retrieve the item that was added earliest (without removing) 4 3 Some Queue Operations Operation Queue after operation x.createQueue() an empty queue front ↓ x.enqueue(5) 5 x.enqueue(3) 5 3 x.enqueue(2) 5 3 2 x.dequeue() 3 2 x.enqueue(7) 3 2 7 x.dequeue(a) 2 7 (a is 3) x.getFront(b) 2 7 (b is 2) 5 An Application -- Reading a String of Characters circle5 A queue can retain characters in the order in which they are typed aQueue.createQueue( ) while (not end of line) { Read a new character ch aQueue.enqueue(ch) } circle5 Once the characters are in a queue, the system can process them as necessary 6 4 Recognizing Palindromes ringbutton3 A palindrome circle5 A string of characters that reads the same from left to right as its does from right to left ringbutton3 To recognize a palindrome, a queue can be used in conjunction with a stack circle5 A stack reverses the order of occurrences circle5 A queue preserves the order of occurrences ringbutton3 A nonrecursive recognition algorithm for palindromes circle5 As you traverse the character string from left to right, insert each character into both a queue and a stack circle5 Compare the characters at the front of the queue and the top of the stack 7 Recognizing Palindromes (cont.) 8 The results of inserting a string into both a queue and a stack 5 Recognizing Palindromes -- Algorithm isPal(in str:string):boolean // Determines whether str is a palindrome or not aQueue.createQueue(); aStack.createStack(); len = length of str; for (i=1 through len) { nextChar = ith character of str; aQueue.enqueue(nextChar);aQueue....
View Full Document

Page1 / 21

06-Queues - 1 Queue 1 The Abstract Data Type Queue circle5...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online