Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Q1.Yes,we must always use the Object datatypefor defining an abstract data type.

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

example, consider we store a String inside of an Object type.

a) How can we get it back as a String?

b) What happens if it wasn’t actually a String in the Object reference?

c) How can we double-check what is in the Object reference?

Solution:a) We can get back a String through type Casting.

b)we’ll get NumberFormatException

c)By using == operator which always checks for reference comparison

Q3. If two data structures implement the same interface, are they interchangeable?

Must anything be done to the driver class that uses it?

Solution:Yes they are Interchangeable.We can create a Contsructor in driver class.

Eg: Following are Constructors:

ArrayList(Collection<? extends E> c)

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

underlying linked list data structure).

(a)Define the data structure for the job-item entries.

(b)Write the methods for:

(1) adding a new job-entry to the priority queue.

(2) a job-item leaving the priority queue.

Solution :a)

class WorkShop

Object job;

ListNode next;

public ListNode(Object priority)// priorityurgent/normal/low.

job = priority;

next = null;

b)1)

public void insertJob(Comparable priority) throws PQOverflowException

ListNode temp = thePQueue;

thePQueue = new ListNode(priority);

thePQueue.next = temp;
}

2) public void removeJob() throws PQUnderflowException{

if (thePQueue == null)

throw new PQUnderflowException();

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

used as the underlying datastructure instead of a DLL?

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

You might also like