DS JAVA_6th Lab Program
DS JAVA_6th Lab Program
Deque ADT
22. Write Java programs to implement the deque (double ended queue) ADT using
(a) Array
(b) Doubly linked list.
A deque is a double-ended queue. You can insert items at either end and delete them from either end.
Methods are addFirst(), addLast(), removeFirst() and removeLast().
If you restrict yourself to addFirst() and removeFirst() (or their equivalents on the right),
then the deque acts like a stack. If you restrict yourself to addFirst() and removeLast() (or the
opposite pair), then it acts like a queue.
A deque provides a more versatile data structure than either a stack or a queue, and is sometimes
used in container class libraries to serve both purposes. However, it is not used as often as stacks and
queues.
The deque is maintained by either an array or linked list with pointers first and last which
point to the two ends of the deque. Such a structure is represented by the following figure.
first last
Deletion Insertion
Insertion Deletion
ArrayDeque
Program 22(a) is an array implementation of ArrayDeque class and it is tested in Program 22(b).
Deque: B C D
----------------------------
first:1, last:4, count: 4
0 1 2 3 4 5
Deque: B C D E
----------------------------
deleteLast(): E
deleteLast(): D
----------------------------
first:1, last:2, count: 2
0 1 2 3 4 5
Deque: B C
----------------------------
first:4, last:2, count: 5
0 1 2 3 4 5
Deque: P B C R Q
----------------------------
first:5, last:2, count: 4
0 1 2 3 4 5
Deque: P B C Q
----------------------------
first:4, last:2, count: 5
0 1 2 3 4 5
Deque: P B C X Q
----------------------------
first:4, last:3, count: 6
0 1 2 3 4 5
Deque: P B C Y X Q
----------------------------
Deque is full
----------------------------
first:4, last:3, count: 6
0 1 2 3 4 5
Deque: P B C Y X Q
----------------------------
Linked Deque
A deque is implemented by using a doubly linked list with references to first and last. Following
figure illustrates the linked deque operations. Each node of the doubly linked list contains three fields:
data, prev and next. The fields prev and next refer to the node itself. Program 22(c) implements
LinkedDeque class which is tested in Program 22(d).
}
public Object getLast()
{
if( !isEmpty() ) return( last.data );
else return null;
}
public boolean isEmpty()
{ return (count == 0); }
public int size()
{ return(count); }
public void display()
{ DequeNode p = first;
System.out.print("Deque: [ ");
while( p != null )
{ System.out.print( p.data + " " );
p = p.next;
}
System.out.println("]");
}
}
removeLast(): E
Deque: [ B A D ]
size(): 3
Priority Queue
23. Write a Java program to implement a priority queue ADT.
In many situations, ordinary queues are inadequate, as when FIFO arrangement has to be overruled
using some priority criteria.
The problem with a priority queue is in finding an efficient implementation that allows relatively
fast insertion and deletion. Because items may arrive randomly to the queue, there is no guarantee that
the items at front will be the most likely to be removed and that the items inserted at the rear will be
the last items for deletion. Priority queues are implemented by using (1) arrays, (2) linked lists, (3)
binary heaps
class Node
{ data prn next
String data;
int prn;
Node next; Node
}
The head indicates (or refers to) first node of the list. The delete routine removes the head node
and makes the new head to refer to head next node.
Adding an item to the priority queue is complicated than deleting a node from the queue, because
we need to find the correct location to insert the node.
The insert method traverses the list until finding a node (call it N) whose priority number is
greater than priority number of new node to be added. Insert new node in front of N. If no such node is
found, insert new node after the end of the list (that is, as a last node of the list). While traversing the
list, the object reference of preceding node of the new node is to be saved.
LinkedPriorityQueue class implemented as a linked list is given in Program 23(b), and it is tested
in Program 23(c). Node class is defined in Program 23(a).