Professional Documents
Culture Documents
Unit 4 Solution
Unit 4 Solution
The reason we need to specify a “less generic” abstract type is Java does not have facilities for
defining generics.
Q2. How can we get a generic object type back into its more specific self? For
Q3. If two data structures implement the same interface, are they interchangeable?
LinkedList(Collection<? extends E> c) both dataStructure implement Collections(I) and they are
interchangeable by Constructor
Q4. Run through the basic operation of a stack and queue, implemented by linked
lists. How can the FIFO nature of a queue (and LIFO nature of a stack) be
guaranteed?
solution: . In stack Implementation, a stack contains a top pointer. which is “head” of the stack
where pushing and popping items happens at the head of the list. first node have null in link field
and second node link have first node address in link field and so on and last node address in “top”
pointer.
In a Queue data structure, we maintain two pointers, front and rear. The front points the first item
of queue and rear points to last item.enQueue() This operation adds a new node after rear and
moves rear to the next node.deQueue() This operation removes the front node and moves front to
the next node.
Q5.Consider a Priority Queue ADT for a car maintenance workshop. Vehicles arrive
for service, and the repair job is assessed as being urgent(u), normal priority(n), or
low priority(l). The workshop manager also requires a count of the number of jobs
waiting in the queue. (You can assume the ADT will be implemented using an
Solution :a)
class WorkShop
Object job;
ListNode next;
job = priority;
next = null;
b)1)
thePQueue.next = temp;
}
if (thePQueue == null)
else
thePQueue = thePQueue.next;
Q6. Why For a double ended queue (deque): why is the double linked list data
structure well suited to implementing a deque? Could a single link list or array be
Solution: For implementing deque, we need to keep track of two pointers, front and rear. We
enqueue (push) an item at the rear or the front end of deque and dequeue(pop) an item from both
rear and front end and DLL contains pointer for previous and next node which is not possible in
Singly Linked List and Arrays