View
212
Download
0
Embed Size (px)
Citation preview
Queues
• What are queues?
• Queue Implementation Using Linked Lists.
• Applications of Queues.
What are queues?
• Queues are linear data structures in which we add elements to one end and remove them from the other end.
• Queues are called FIFO.• Queue operations:
Enqueue
Dequeue
What are queues?
413
1413
51413
514
5141
51
enqueue(1);
enqueue(5);
dequeue();
dequeue();
dequeue();
Rear Front
Given the following Queue, how will it change when we apply the given operations?
Queue Interface• Queue interface extends the Container interface.• It inherits all the methods of the Container interface(getCount(),
isEmpty(), isFull(), purge() , accept() and getEnumeration().+ Plus three mehods:
getHead(), dequeue(), and enqueue() It can be implemented with arrays or linked lists:
1 public interface Queue extends Container
2 {
3 Object getHead();
4 void enqueue (Object object);
5 Object dequeue ();
6 }
7 public class QueueAsLinkedList extends AbstractContainer implements Queue
8 {
9 protected LinkedList list;
10 //...
11 }
QueueAsLinkedList Constructor
• We create a linkedList object and put its reference in the list field of the QueueAsLinkedList class:
public QueueAsLinkedList()
{ list = new LinkedList();
QueueAsLinkedList: purge() method
nextDatum nextDatum
13
tail
head
nextDatum nextDatum
13
Count = 2
tail
head
Count = 0
public void purge(){
list.purge();count = 0;
}
Methods of Queue
• Here are the three method of Queue:
1 public void enqueue (Object object)2 {3 list.append (object);4 ++count;5 }6 public Object dequeue()7 {8 if (count ==0)9 throw new ContainerEmptyException();10 Object result = list.getFirst();11 list.extract (result);12 --count;13 return result;14 }15 public Object getHead()16 {17 if (count == 0)18 throw new ContainerEmptyException();19 return list.getFirst(); 20 }
Applecation
• There are many applications of queues, one application is breadth first traversal for trees.
• traversing a tree means, visiting the elements of the tree to do some computation on the values in the nodes.
• there are many ways of traversal,we will stuy next breadth first traversal.
For more info. watch the animation on the CD since this ex. Isn’t our main issue & it’ll be determined later.
A
G
D
H
B C
E IF
J K L