Java Chapter 20
A linked list is a looping data structure
False
The head of a linked list is also a linked list.
False
In Java, the first node in a list has index
0
A linked list class uses a Node class with successor reference next and field element to store values. It uses a reference first to point to the first node of the list. Code to print all elements stored in the list can be written as
Node p = first; while (p != null) { System.out.println(p.element); p = p.next; }
A systematic procedure for starting at the first node in a list, and visiting all nodes in the list by going from each node to its successor is called
a traversal
A list can be considered a recursive data structure because
if you remove the head of the list, what remains is also a list
To remove a node with a positive index k from a linked list
assign the successor reference in the node with index k to the successor reference in the node with index k-1
A Node class for a linked list that can hold elements of type Object can be declared to have fields
Object element; Node next;
In a typical circular doubly linked list, a node has
a field to store the element, and two references to keep track of successor and predecessor nodes
In a typical doubly linked list, a node has
a field to store the element, and two references to keep track of successor and predecessor nodes
A list in which each stored element is associated with a reference to its successor is called
a linked list
The tail of a list
is what you get when take a list and remove its head
A circularly linked list makes it easy to
jump from the last node to the first
To remove a node with index 0 from a linked list,
just move the head reference one node forward
To allocate storage for their elements, linked lists use
linked allocation
In a linked list, the successor of a node X
may not exist
A doubly circularly linked list makes it easy to
move forward through a list, and then quickly jump to the beginning of the list when you get to the end
A doubly linked list makes it easy to
move from any node to its successor, and from any node to its predecessor
To remove the first node in a nonempty linked list
move the head reference one node forward: head = head.next;
The objects that form the units of memory allocation in a linked lists are called
nodes
A linked list class uses a Node class with successor reference next, and uses a reference first to point to the first node of the list. A positive index k is given, and we want to set a reference pred to point to the node with index k-1. The right code to use is
pred = first; for (int i = 1; i < k; i++) pred = pred.next;
In a linked list implementation using a reference first to point to the first node of the list, a method isEmpty() can test to see if the list is empty by executing the statement(s)
return first == null;
To remove a node X with index 1 or greater from a linked list
set a reference pred to the predecessor of the node you want to remove, and set the successor of pred to the successor of X
In many recursive operations on lists,
All of these
Scientists in a certain laboratory are working with a linked list class that uses recursion to compute its size. The scientists know that an empty list has size 0, so they never ask a linked list to compute its size when the list is empty. Under these circumstances,
B and C are both correct
The index of an element to be removed from a list can be the same as the size of the list.
False
The last item in a doubly linked list can sometimes have a successor
False
A linked list class uses a Node class to represent nodes. A private recursive method Node add(int index, E element, Node list) takes a reference list (referring to the first in a chain of Node objects), adds a node containing the given element at the given index, and returns a reference to the first node of the resulting chain. Assume that index is nonnegative and is less than or equal to the size of list. Under these circumstances, the add method should handle its base case (index is 0) by
adding a node containing element to the front of list and returning a reference to the newly created node
A linked list class keeps its elements in the order in which they are added, with the index of an element X being greater than the index of any element added to the list before X. Addition of new elements to such a list can be made more efficient
by keeping a reference to the last element added
To allocate storage for its elements, an array-based list such as ArrayList uses
contiguous allocation
A method int size( ) in a linked list class returns the number of elements stored in the list by executing the single statement return count; For this to work correctly,
count must be an instance variable in the class, initialized to 0, incremented by each add method, and decremented by each remove method
A recursive computation of the size of a list can work as follows
if the list is empty, return zero; otherwise, recursively compute the size of the tail, add one, and return the result
In a linked list, the predecessor of a node X
is undefined if X is the first node, otherwise it is the node whose index is one less than the index of X
A linked list class uses a Node class with a successor reference next to represent nodes. A private recursive method Node add(int index, E element, Node list) takes a reference list (referring to the first in a chain of Node objects), adds a node containing the given element at the given index, and returns a reference to the first node of the resulting chain. Assume that index is nonnegative and is less or equal to the size of list. Under these circumstances, the add method should handle its non-base case (index is not 0) by
setting list.next to add(index-1, element, list.next) and returning list
A linked list class uses a Node class with successor reference next and field element to store values. A recursive method to print all list elements can be written as
static void printList(Node list) { if (list != null) { System.out.println(list.element); printList(list.next); } }
A linked list is represented by a reference to
the first node in the list, unless the list is empty, in which case the reference is set to null
A list method void add(int index, E x) seeking to add an element x that is an object of a class E to a list, should throw an IndexOutOfBoundsException when
the index is negative, or greater than the size of the list
A list method E remove(int index) designed to remove and return the element at the given index should throw IndexOutOfBoundsException when
the index is negative, or is greater than, or equal to, the size of the list
In order to use recursion on linked lists
the recursive method should be made private, and should be called by a public non-recursive method
When using recursion on linked lists
the recursive method should be made private, and should be called by a public non-recursive method