Queue ADT Abstract Data Type N 3 2

Queue ADT (Abstract Data Type) N … 3 2 1 Professor John Carelli Kutztown University Computer Science Department

What is a Queue? • A queue is an ordered collection, or list, of items that can only be modified by removing items at the beginning or adding items to the end • Items are processed on a first come/first serve basis • There is no access to the middle of the list (no “cutting in line”) • Real life examples: • Waiting on a grocery store check-out line (actual physical line) • Waiting in a reception room or for a table in a restaurant (“virtual” line) Professor John Carelli Kutztown University Computer Science Department

Queues in the abstract • Sometimes referred to as a “FIFO” • Stands for “first-in/first-out” • A stack would be a “LIFO” • Some definitions: • Enqueue: add an item to the end • Dequeue: remove an item from the beginning Professor John Carelli Kutztown University Computer Science Department

Abstract Data Type (ADT) for a Queue • Queue. ADT requires that a program implementing a queue contain certain minimum core capabilities: • Elements must be maintained in an ordered list • Facilities must be provided to both remove first item in list (dequeue) and add new items to the end of the list (enqueue) • Individual elements in the queue can be any valid data type (or collection of data types) • Other possible capabilities: • Query the queue for contents, length, … • Clear or empty the queue • Limit or expand the size of the queue Professor John Carelli Kutztown University Computer Science Department

Queue Representations • Using an Array • Using a Linked List 0 1 1 2 2 3 … 4 … N-1 Professor John Carelli null Kutztown University Computer Science Department

Array Implementation of Queue Use an array to store data elements • Advantages: • Easy to access • Natural ordering 0 Professor John Carelli Kutztown University 2 N-1 last • Array is fixed size • Number of entries is limited unless special measures are taken first • Size Limitation 1 Computer Science Department

Queue Array Implementation with wraparound 1 2 with wrap-around 0 1 2 first last • When the last element is filled, start over at the beginning 0 last • Solution: implement a wraparound capability normal configuration first Starting index issue • Problem: As items are dequeued, empty cells show up at the beginning of the array Example: Queue. Array Professor John Carelli Kutztown University Computer Science Department

Linked List Implementation of Queue Use a linked list to store data elements • Advantages: • Virtually unlimited number of elements • No “wrap-around” worries 1 2 • Potential issues … • More complex coding • Need to manage pointers and data containers • On-the-fly memory allocation/deallocation • More memory required (data plus pointers) • Most likely slower than array implementation N null Example: Queue. LL Professor John Carelli Kutztown University Computer Science Department

Final Considerations • What about more complex data (not just integers)? • The integers can be pointers to other data containers (classes, structs …) • Generalize by making a template • Which implementation is better? Answer is: “it depends…” • If a fixed size queue is acceptable, probably an array • For example: large quantities of predictable, streaming data that requires buffering • (audio or video streaming from the internet) • If managing random, or unpredictable, data, a linked list might be better • Particularly if speed or memory is less of an issue • Lists of names, addresses … Professor John Carelli Kutztown University Computer Science Department
- Slides: 9