Chapter15 TB
Chapter15 TB
Chapter 15
Linked Data Structures
◼ Multiple Choice
1) The first node in a linked list is commonly referred to as the ________ node.
(a) head
(b) tail
(c) predecessor
(d) successor
Answer: A
3) A node contains:
(a) data item(s)
(b) reference(s) to another node
(c) both A and B
(d) none of the above
Answer: C
4) In Java, you indicate the end of a linked list be setting the link instance variable of the last node in
the linked list to __________.
(a) 0
(b) -1
(c) 1
(d) null
Answer: D
Chapter 15 Linked Data Structures 2
5) If the head instance variable of a linked list contains a reference to null, this means the list is:
(a) full
(b) empty
(c) lost
(d) none of the above
Answer: B
6) Making the Node class a private inner class of a linked data structure is an example of:
(a) polymorphism
(b) encapsulation
(c) inheritance
(d) all of the above
Answer: B
8) A ____________ copy of an object is a copy that has no references in common with the original
object.
(a) bit copy
(b) deep copy
(c) shallow copy
(d) none of the above
Answer: B
9) A _____________ copy of an object is a copy that has references in common with the original
object.
(a) bit copy
(b) deep copy
(c) shallow copy
(d) none of the above
Answer: C
10) To use the Java Iterator Interface you must import the ____________ package.
(a) java.tools
(b) java.linkedlist
(c) java.iterator
(d) java.util
Answer: D
Chapter 15 Linked Data Structures 3
11) Java contains a mechanism that automatically reclaims memory. This mechanism is called:
(a) Garbage elimination
(b) Garbage collection
(c) Taking out the trash
(d) None of the above
Answer: B
12) A ____________ linked list has nodes that contain two references to Nodes.
(a) circular
(b) sequential
(c) doubly
(d) one-way
Answer: C
13) The _________ node is the first node in the tree data structure.
(a) leaf
(b) root
(c) sibling
(d) branch
Answer: B
15) Recursively visiting the root node, left subtree and then the right subtree describes:
(a) preorder processing
(b) inorder processing
(c) postorder processing
(d) none of the above
Answer: A
16) Recursively visiting the left subtree, right subtree and then the root describes:
(a) preordering processing
(b) inorder processing
(c) postorder processing
(d) none of the above
Answer: C
Chapter 15 Linked Data Structures 4
◼ True/False
1) A linked data structure contains nodes and links.
Answer: True
3) When using a linked list, you do not need to know when the end of the list has been reached.
Answer: False
4) Forgetting to set the reference instance variable of the last node in a linked list to null will cause a
logic error.
Answer: True
6) A deep copy of an object is a copy that has references in common with the original object.
Answer: False
7) A copy constructor and a clone method should normally make a deep copy whenever possible.
Answer: True
8) If you define a clone method, the class should implement the Cloneable interface.
Answer: True
9) An iterator is any object that allows you to step through the list one item at a time.
Answer: True
◼ Short Answer/Essay
1) What is the function of the variable head when used with a linked list? What is the data type of the
head variable?
Answer: The head variable references the first node in a linked list. Without this reference variable,
the beginning of the list is lost. The data type of the head variable is of the Node type.
2) Draw a diagram of a linked list that contains nodes with data items of type String that contains the
name of a city and type double that contains a pollution index. Include an instance variable named
head to indicate the beginning of the list. Insert the following nodes: Franklin, 15.7, Chicago, 23.2,
Denver, 7.2.
Answer:
Franklin
Head
15.7
Chicago
23.2
Denver
7.2
NULL
3) Redraw the diagram created in number 2 above after inserting a node containing Chattanooga, 27.6.
Answer:
Chapter 15 Linked Data Structures 6
Chattanooga
Head
27.6
NULL
Franklin
15.7
Chicago
23.2
Denver
7.2
NULL
4) Redraw the diagram created in number 3 above, after deleting the node containing Chicago.
Answer:
Chattanooga
Head
27.6
NULL
Franklin
15.7
Denver
7.2
NULL
5) Redraw the diagram created in number 4 above after deleting the head node.
Answer:
Chapter 15 Linked Data Structures 7
Franklin
Head
15.7
Denver
7.2
NULL
6) Create a generic Node class to represent the linked list depicted in your diagrams above.
Answer:
public Node()
link = null;
cityName = null;
pollutionCount = 0.0;
}
Chapter 15 Linked Data Structures 8
setData(cName, pCount);
link = linkValue;
cityName = cName;
pollutionCount = pCount;
link = newLink;
return cityName;
}
Chapter 15 Linked Data Structures 9
return pollutionCount;
return link;
7) Given the Node class created in number 6 above, write Java statements to insert a new node
containing (Chattanooga, 23.7) into an empty list.
Answer:
Node head=null;
Node current=null;
head = current;
8) Given the Node class created in number 6 above, write Java statements to delete a node from the
beginning of the list.
Answer:
current = head;
Chapter 15 Linked Data Structures 10
if(current.getLink() == null)
head = null;
else
current = current.getLink();
head = current;
current = null;
9) Write a method called displayList that displays the data items in the Node class created in number 6
above.
Answer:
Node current = h;
while(current != null)
current = current.getLink();
Chapter 15 Linked Data Structures 11
5 10
1 3 9 12
12) What is the result of a preorder traversal of the binary search tree created in question 12 above?
Answer: 7, 5, 1, 3, 10, 9, 12
13) What is the result of an inorder traversal of the binary search tree created in question 12 above?
Answer: 1, 3, 5, 7, 9, 10, 12
14) What is the result of a postorder traversal of the binary search tree created in question 12 above?
Answer: 1, 3, 5, 9, 12, 10, 7