Implementing Lists, Stacks, Queues, and Priority Queues Flashcards
Describe the list (list as a general data structure).
A list is a popular data structure for storing data in sequential order. You can perform the following operations on most lists:
- Retrieve an element from the list
- Insert a new element into the list
- Delete an element from the list
- Find out how many elements are in the list
- Determine whether an element is in the list
- Check whether the list is empty
What is a singly linked list?
A singly linked list contains a pointer to the list’s first node, and each node contains a pointer to the next node sequentially.
What is a circular, singly linked list?
A circular, singly linked list is like a singly linked list, except that the pointer of the last node points back to the first node. Note that tail is not needed for circular linked lists. head points to the current node in the list.
What is a doubly linked list?
A doubly linked list contains nodes with two pointers. One points to the next node and the other to the previous node. These two pointers are conveniently called a foreward pointer and a backward pointer. Thus, a doubly linked list can be traversed forward and backward. The java.util.LinkedList class is implemented using a doubly linked list.
What is a stack?
A stack can be viewed as a special type of list whose elements are accessed, inserted, and deleted only from the end (top).
What data structure can you use to implement a stack?
Since the insertion and deletion operations on a stack are made only at the end of the stack, it is more efficient to implement a stack with an array list than with a linked list.
What data structure can you use to implement a queue?
Since deletions are made at the beginning of the list, it is more efficient to implement a queue using a linked list than an array list.
There are two ways to design the stack and queue classes:
- – Using inheritance: you can define a stack class by extending ArrayList, and a queue class by extending LinkedList
- – Using composition: You can define an array list as a data field in the stack class, and a linked list as a data field in the queue class.
Which method is best, and why?
Both designs are fine, but using composition is better because it enables you to define a completely new stack class and queue class without inheriting the unnecessary and inappropriate methods from the array list and linked list.
What data structure can you use to implement a priority queue?
In a priority queue, elements are assigned with priorities. When accessing elements, the element with the highest priority is removed first.
A priority queue can be implemented using a heap, in which the root is the object with the highest priority in the queue.
Which of the following operations are supported by a list?
A. Retrieve an element from this list.
B. Insert a new element to this list.
C. Delete an element from this list.
D. Find how many elements are in this list.
E. Find whether an element is in this list.
All are true
Which of the following statements are true?
A. MyArrayList and MyLinkedList are two concrete implementations of MyList.
B. MyArrayList is implemented using an array. The array is dynamically created. If the capacity of the array is exceeded, create a new larger array and copy all the elements from the current array to the new array.
C. MyLinkedList is implemented using a linked structure.
D. A linked structure consists of nodes. Each node is dynamically created to hold an element. All the nodes are linked together to form a list.
E. MyAbstractList partially implements MyList.
All are true
In the implementation of MyArrayList, which of the following are true?
A. size indicates the number of elements in the list.
B. capacity is the length of the array used to store the elements in the list.
C. capacity is always greater than size.
D. size is reduced by 1 if an element is deleted from the list.
E. capacity is reduced by 1 if an element is deleted from the list.
The correct answer is ABD
Explanation: (C) is not correct because capacity may equal to size.
In the implementation of MyArrayList, which of the following are true?
A. size never reduces.
B. capacity never reduces.
C. Inside MyArrayList, a regular array is used to store elements.
D. size is not declared in MyArrayList, but declared in MyAbstractList as protected.
E. If the current capacity equals to size, capacity is doubled when a new element is added to MyArrayList
All but A are true
In the implementation of MyLinkedList, which of the following are true?
A. MyLinkedList contains all the methods defined in MyList. Additionally, MyLinkedList defines several new methods that are appropriate for processing a linked list.
B. MyArrayList does not introduce new methods. All the methods in MyArrayList are defined in MyList.
C. You can use a linked list to improve efficiency for adding and remove an element anywhere in a list.
D. You should use an array list if your application does not require adding and remove an element anywhere in a list.
All are true
If a linked list is empty, which of following statements are true? A. head is null. B. tail is null. C. head == tail. D. head
All but D are true