Presentation is loading. Please wait.

Presentation is loading. Please wait.

Queue What is a queue?. Queues A queue is similar to waiting in line for a service, e.g., at the bank, at the bathroom –The first item put on the queue.

Similar presentations


Presentation on theme: "Queue What is a queue?. Queues A queue is similar to waiting in line for a service, e.g., at the bank, at the bathroom –The first item put on the queue."— Presentation transcript:

1 Queue What is a queue?

2 Queues A queue is similar to waiting in line for a service, e.g., at the bank, at the bathroom –The first item put on the queue is at the front of the queue. –All items added to the queue are placed behind of the previous items. –The last item put on the queue is at the back of the queue.

3 Queues Queues are called First-in First-out (FIFO) data structures. –The first person to enter the queue is the first person to be served, i.e., leave the queue. –The last person to enter the queue is the last one to be removed from it.

4 Queues Characteristics of queues: –Data can only be place at the back of the queue. –Data can only be removed from the front of the queue. –Data can only be removed from the rear of the queue if there is only one item on the queue. –Data can not be removed from the middle of the queue without first removing all items in front of it.

5 Queue Behaviors The behavior of putting an item in the queue is called enqueue. –Enqueue 4 onto the queue. The behavior of removing and item from the queue is called dequeue. –Dequeue 4 from the stack. (Remember this only works if 4 is in the front of the queue.)

6 Example What queue exists after executing the following commands? –enqueue(3) –enqueue(6) –enqueue(8) –enqueue(1) –dequeue() –enqueue(14)

7 Queue Implementation The ability to use a queue is not built into Java like arrays; nor is it available in util –You can implement a queue in many different ways, e. g. using a list or an array.

8 Queue Implementation A queue class can be written using arrays that will simulate a queue in your programs. –A restriction of using arrays to implement a queue is that the total size of the queue is limited. Why? Can you define a method that will resize the array to hold a queue of any size?

9 Queue Implementation Write a program to implement the Queue class that holds data of any data type?

10 Queue Implementation How do we modify our queue implementation so that the queue will grow?

11 Palindrome Example A program that determines if a word is a palindrome using a stack and a queue. –A palindrome is a word that is spelled the same both forwards and backwards.


Download ppt "Queue What is a queue?. Queues A queue is similar to waiting in line for a service, e.g., at the bank, at the bathroom –The first item put on the queue."

Similar presentations


Ads by Google