DS JAVA_5th Lab Program
DS JAVA_5th Lab Program
return true;
}
}
Linked Stack
21. Write Java programs to implement the following using a singly linked list.
(a) Stack ADT
(b) Queue ADT
Another way to represent a stack is by using a linked list. A stack can be represented by using nodes of
the linked list. Each node contains two fields: data (info) and next (link). The data field of each
node contains an item in the stack and the corresponding next field points to the node containing the
next item in the stack. The next field of the last node is null – that is, the bottom of the stack. The
top refers to the topmost node (the last item inserted) in the stack. The empty stack is represented by
setting top to null. Because the way the nodes are pointing, push and pop operations are easy to
accomplish. Program 21(a) is a complete listing, demonstrating the push and pop operations of a stack
using singly linked list.
stk.push(90); // insert 90
stk.displayStack();
}
}
Output from LinkedStack operations program:
Contents of Stack: [ 40 35 20 ]
Popped item: 40
Contents of Stack: [ 35 20 ]
Contents of Stack: [ 75 70 65 35 20 ]
Popped item: 75
peek(): 70
Contents of Stack: [ 70 65 35 20 ]
Contents of Stack: [ 90 70 65 35 20 ]
Linked Queue
In contiguous storage (using arrays), queues were harder to manipulate than were stacks. It causes
difficulties to handle full queues and empty queues. It is for queues that linked storage really comes
into its own. The linked implementation has two advantages over the array implementation: (1) it is
faster – locations for insertion and deletion are same – at the back and at the front, and (2) it wastes no
space – removed nodes are deleted by the automatic garbage collector process.
Linked queues are just as easy to handle as are linked stacks. This section presents a queue
implementation which makes use of the singly linked list. We keep two pointers, front and rear. The
operations of LinkedQueue class is given Program 21(b) and LinkedQueue class is tested in
Program 21(c).
class LinkedQueueDemo
{ public static void main(String[] args)
{
LinkedQueue q = new LinkedQueue();
q.display();
q.insert('A');
q.insert('B');
q.insert('C');
q.insert('D');
q.display();
System.out.println("delete(): " + q.remove());
q.display();
System.out.println("peek(): " + q.peek());
q.insert('E');
q.insert('F');
4. Stacks and Queues 47
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