Presentation is loading. Please wait.

Presentation is loading. Please wait.

Queue ADT (Abstract Data Type) N … 3 2 1.

Similar presentations


Presentation on theme: "Queue ADT (Abstract Data Type) N … 3 2 1."— Presentation transcript:

1 Queue ADT (Abstract Data Type) N 3 2 1

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

3 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

4 Abstract Data Type (ADT) for a Queue
QueueADT 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

5 Queue Representations
Using an Array Using a Linked List 1 2 3 4 N-1 1 2 N null

6 Array Implementation of Queue
Use an array to store data elements Advantages: Easy to access Natural ordering Size Limitation Array is fixed size Number of entries is limited unless special measures are taken 1 N-1 first last 2

7 Queue Array Implementation with wrap-around
Starting index issue Problem: As items are dequeued, empty cells show up at the beginning of the array Solution: implement a wrap- around capability When the last element is filled, start over at the beginning normal configuration 1 2 first last with wrap-around 1 2 first last

8 Array Implementation of Queue in C++
qarray.c Simple implementation of QueueADT using C++ Stores user provided integers in a queue Features: Fixed size array Implements wrap-around feature Basic functionality: enqueue, dequeue, and display methods main.c - simple wrapper for testing

9 Linked List Implementation of Queue
Use a linked list to store data elements Advantages: Virtually unlimited number of elements No “wrap-around” worries 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 1 2 N null

10 Queue Linked List Implementation in C++
qllist.c Simple implementation of QueueADT using C++ Stores user provided integers in a queue Features: Unlimited queue size Basic functionality: enqueue, dequeue, and display methods main.c - simple wrapper for testing (same as for array implementation)

11 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 …


Download ppt "Queue ADT (Abstract Data Type) N … 3 2 1."

Similar presentations


Ads by Google