queues the abstract data type queue
play

Queues The Abstract Data Type Queue FIFO queue ADT Another common - PowerPoint PPT Presentation

Queues The Abstract Data Type Queue FIFO queue ADT Another common linear data structure similar to the stack Examples using queues reading character string in order Queue is an ADT with following properties recognize


  1. Queues The Abstract Data Type Queue � FIFO queue ADT � Another common linear data structure similar to the stack � Examples using queues � reading character string in order � Queue is an ADT with following properties � recognize palindromes � elements are kept in their order of arrival � Queue implementations � new items enter at the back, or rear, of the queue � items leave from the front of the queue � LL pointer based � List ADT based � Thus queue has first-in, first-out (FIFO) property � array based � nicely models several real-world processes � tradeoffs � line to buy movie tickets, or queue jobs and print requests EECS 268 Programming II 1 EECS 268 Programming II 2 The Abstract Data Type Queue The Abstract Data Type Queue � ADT queue operations � Operation Contract for the ADT Queue � isEmpty():boolean {query} � Create an empty queue � enqueue(in newItem:QueueItemType) � Destroy a queue throw QueueException � Determine whether a queue is empty � dequeue() throw QueueException � Add a new item to the queue � dequeue(out queueFront:QueueItemType) � Remove the item that was added earliest throw QueueException � Retrieve the item that was added earliest � getFront(out queueFront:QueueItemType) {query} throw QueueException EECS 268 Programming II 3 EECS 268 Programming II 4

  2. The Abstract Data Type Queue Example 1: Ordering Character String � 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) } // end while � Once the characters are in a queue, the Figure 7-2 Some queue operations system can process them as necessary EECS 268 Programming II 5 EECS 268 Programming II 6 Example2: Recognizing Palindromes Example2: Recognizing Palindromes � A nonrecursive � A palindrome is a string of characters that recognition algorithm for reads the same backwards and forwards palindromes � RADAR, MADAM, EYE, etc. � traverse character string � Observations from left to right � stack reverses the order of occurrences � insert each character into both a queue and a stack � queue preserves the order of occurrences � compare the characters at � A palindrome stored in both stack and queue the front of the queue and will display a match when retrieved the top of the stack EECS 268 Programming II 7 EECS 268 Programming II see C7-palin.cpp 8

  3. Linked List Implementations Implementations of the ADT Queue � Linked list based queue implementation � can maintain pointers to front and back of Queue � circular linked list with one external reference also possible � Using ADT List class to implement queue � possible less efficient, but simple � An array-based queue implementation � problem of rightward-drift Figure 7-4 A pointer-based implementation of a queue: (a) a linear linked list with two external pointers; (b) a circular linear linked list with one external pointer EECS 268 Programming II 9 EECS 268 Programming II 10 Operations in LL Implementation List Based Queue Implementation Figure 7-5 Inserting � Queue operations map well to ADT List an item into a operations nonempty queue � enqueue(item) � insert(getLength()+1, item) � dequeue() � remove(1) Figure 7-6 Inserting an � getFront(qfront) � retrieve(1, qfront) item into an empty queue: � We can built the queue ADT as a wrapper over (a) before insertion; (b) after insertion the List ADT Figure 7-7 Deleting an item from a queue of more than one item see C7-QueueP.cpp see C7-QueueL.cpp 11 EECS 268 Programming II 12

  4. � � An Array-Based Implementation Circular Array Implementation � Using arrays is slightly more complex � Problem: � naïve implementation causes rightward drift � front == (back+1) is � queue appears full even when array does not hold true for both queue MAX_QUEUE-1 elements full & empty � Solutions to rightward drift � always copy array elements to left � expensive � Solution: � maintain circular array � how to detect queue full/empty? � use integer counter to hold size of queue � update on each enqueue/dequeue EECS 268 Programming II 13 EECS 268 Programming II 14 An Array-Based Implementation Array Implementation Variations � Initialize the queue, Use a flag isFull to distinguish front = 0, back = MAX_QUEUE � 1, between the full and count = 0 empty conditions � Inserting into a queue Declare back = (back+1) % MAX_QUEUE; MAX_QUEUE + 1 items[back] = newItem; locations for the ++count; array items, but use � Deleting from a queue only MAX_QUEUE of front = (front+1) % MAX_QUEUE; them for queue --count; items see C7-QueueA.cpp EECS 268 Programming II 15 EECS 268 Programming II 16

  5. Comparing Implementations A Summary of Position-Oriented ADTs � Static arrays Vs. dynamically allocated LLs � Position-oriented ADTs � enqueue operation cannot add item if array is full � List � no size restriction with LL (unless memory full) � Stack � Queue � LL Vs List bases array implementations � Stacks and queues � LL-based implementation is more efficient � ADT list approach reuses already implemented � Only the end positions can be accessed class � Lists � much simpler to write � All positions can be accessed � saves programming time EECS 268 Programming II 17 EECS 268 Programming II 18 Summary A Summary of Position-Oriented ADTs � ADT queue has first-in, first-out (FIFO) behavior � Stacks and queues are very similar � Circular array eliminates the problem of � Operations of stacks and queues can be paired off rightward drift in array-based implementation � createStack and createQueue � To distinguish between the queue-full and queue- � Stack isEmpty and queue isEmpty empty conditions in a circular array � push and enqueue � count the number of items in the queue � pop and dequeue � use an isFull flag � Stack getTop and queue getFront � leave one array location empty � LL and List ADT based implementations possible EECS 268 Programming II 19 EECS 268 Programming II 20

Recommend


More recommend