Presentation is loading. Please wait.

Presentation is loading. Please wait.

Queues Tonga Institute of Higher Education. Definitions Queue - A data structure that stores a number of items. The first item entered into a queue is.

Similar presentations


Presentation on theme: "Queues Tonga Institute of Higher Education. Definitions Queue - A data structure that stores a number of items. The first item entered into a queue is."— Presentation transcript:

1 Queues Tonga Institute of Higher Education

2 Definitions Queue - A data structure that stores a number of items. The first item entered into a queue is the first item to be removed Peek – The act of viewing the value of the item at the front of the queue. Note: You cannot see the values for items behind the front item FIFO – First in First out – Sometimes used to describe a queue

3 Examples of Queues Waiting to checkout at a grocery store The queue for our laser printer The keyboard remembers the letters you type so even if the computer is busy, they still stay in order when the computer is able to print them.

4 Queue Implemented as an Array 1 st in line2 nd in line3 rd in line We can implement a queue using an array Each cell of the array represents a thing in the queue The front of queue is on the left The back of the queue is on the right 012 345

5 Queue Considerations Queue Size – Can be set when the queue is created Errors:  We try to add an item when the queue is full  We try to remove an item when the queue is empty

6 Circular Queue If you use an array, your queue will reach the end of the array and you’ll won’t be able to add more items even though there are empty cells in your array By allowing the data to wrap around the array, we can void this problem

7 Demonstration Queue Applet

8 Demonstration Queue Code

9 Efficiency of Queues Items can be added and removed in constant O(1) time This time is not dependent on the number of items in the queue so it is very fast No comparisons or moves are necessary

10 Example of Priority Queues You get a bunch of letters and order them:  Letters with high priority (bills) get moved to the front of the queue  Letters with medium priority (letter from grandmother) get moved to the middle of the queue  Letters with low priority (advertisements) get moved to the bottom of the queue Your computer multitasks. (Does many things at the same time)  Important programs are put in the front of the queue to be done first.  Non-important programs are put to the back of the queue to be done later.

11 Priority Queues Works like an ordinary queue However, the order of how things are removed depend on a priority key Ascending Priority Queue – The item with the smallest key has the highest priority Descending Priority Queue – The item with the largest key has the highest priority

12 Demonstration Priority Queue Applet

13 Demonstration PriorityQueue Code

14 Efficiency of Priority Queues Adding items  This is slow because the proper in-order position must be found and created  Shifting and comparisons are needed  Runs in O(N) time Deleting items  This is fast because the item to be deleted is always at the top of the array  No shifting or comparisons are needed  Runs in O(1) time


Download ppt "Queues Tonga Institute of Higher Education. Definitions Queue - A data structure that stores a number of items. The first item entered into a queue is."

Similar presentations


Ads by Google