Implement the following method as a new static method for the IntNode
class. (Use the usual IntNode class with instance variables called
data and link.)
public static int count42s(IntNode head)
// Precondition: head is the head reference of a linked list.
// The list might be empty or it might be non-empty.
// Postcondition: The return value is the number of occurrences
// of 42 in the data field of a node on the linked list.
// The list itself is unchanged.
Implement the following method as a new static method for the IntNode
class. (Use the usual IntNode definition with instance variables called
data and link.)
public static boolean has42(IntNode head)
// Precondition: head is the head reference of a linked list.
// The list might be empty or it might be non-empty.
// Postcondition: The return value is true if the list has at least
// one occurrence of the number 42 in the data part of a node.
Implement the following method as a new method for the IntNode
class. (Use the usual IntNode definition with instance variables called
data and link.)
public static boolean all42s(IntNode head)
// Precondition: head is the head reference of a linked list.
// The list might be empty or it might be non-empty.
// Postcondition: The return value is true if every node in the
// list contains 42 in the data part. NOTE: If the list is empty,
// then the method returns true.
Implement the following method as a new stati method for the IntNode
class. (Use the usual IntNode definition with instance variables called
data and link.)
public static int sum(IntNode head)
// Precondition: head is the head reference of a linked list.
// The list might be empty or it might be non-empty.
// Postcondition: The return value is the sum of all the data components
// of all the nodes. NOTE: If the list is empty, the method returns 0.
Implement the following method as a new static method for the IntNode
class. (Use the usual IntNode definition with instance variables called
data and link.)
public static int product(IntNode head)
// Precondition: head is the head reference of a linked list.
// The list might be empty or it might be non-empty.
// Postcondition: The return value is the product of all the data components
// of all the nodes. NOTE: If the list is empty, the method returns 1.
Implement the following method as a new static method for the IntNode
class. (Use the usual IntNode definition with instance variables called
data and link.)
public static void listTailInsert(IntNode head, int entry)
// Precondition: head is the head reference of a non-empty
// linked list.
// Postcondition: A new node has been added at the tail end
// of the list. The data in the new node is taken from the
// parameter called entry.
Implement the following method as a new static method for the IntNode
class. (Use the usual Node definition with instance variables called
data and link.)
public static boolean dataIsOn(IntNode head, IntNode p)
// Precondition: head is the head reference of a linked list
// (which might be empty, or might be non-empty). The parameter p
// is a non-null reference to some IntNode on some linked list.
// Postcondition: The return value is true if the data in p
// appears somewhere in a data field of a node in head's
// linked list. Otherwise the return value is false.
// None of the nodes on any lists are changed.
Implement the following method as a new static method for the IntNode
class. (Use the usual Node definition with instance variables called
data and link.)
public static boolean isOn(IntNode head, IntNode p)
// Precondition: head is the head reference of a linked list
// (which might be empty, or might be non-empty). The parameter p
// is a non-null reference to some IntNode on some linked list.
// Postcondition: The return value is true if p actually points to
// one of the IntNodes in the head's linked list. For example,
// p might point to the head node of this list, or the second node,
// or the third node, and so on. Otherwise the return value is
// false. None of the nodes on any lists are changed.
Implement the following method as a new method for the IntNode
class. Do not call any of the other class methods from
within your implementation, except for the constructor.
(Use the usual IntrNode definition with instance variables called
data and link.)
public void listInsertZero(IntNode previous)
// Precondition: previous is a reference to a node on a linked list.
// Postcondition: A new node has been added to the list after
// the node that previous refers to. The new node contains 0.
Suppose that p, q, and r are all references to nodes in a linked list with 15
nodes. The variable p refers to the first node, q refers to the 8th node, and r
refers to the last node. Write a few lines of code that will make a new copy of
the list. You code should set THREE new variables called x, y, and z so that: x
refers to the first node of the copy, y refers to the 8th node of the copy, and
z refers to the last node of the copy. Your code may NOT contain any loops,
but it can use the
other IntNode methods.
Short Answers Section 4.4 The Bag ADT with a Linked List
|
Suppose that a and b are IntNode variables. Write one clear
sentence to tell me when the expression
(a==b
) will be true.
Compare the worst-case big-O time analysis for these two methods:
The add method for the Bag that is implemented using an array,
and the add method for the Bag that is implemented using a linked list.
Compare the worst-case big-O time analysis for these two methods:
The remove method for the Bag that is implemented using a fixed-sized array,
and the remove method for the Bag that is implemented using a linked list.
Short Answers Section 4.5 The Sequence ADT with a Linked List
|
Tell me about one of the Sequence operations that is more efficient
because the class keeps a tail reference as a private instance variable.
Provide a specific example showing why the operation would be
less efficient without the tail reference.
Tell me about one of the Sequence operations that is easier to program
because the class keeps a precursor (rather than just a cursor).
Provide a specific example showing why the operation would be
harder to program without the precursor.
Compare the worst-case big-O time analysis for these two methods:
The addBefore method for the Sequence that is implemented using an array,
and the addBefore method for the Sequence that is implemented using a linked list.
Compare the worst-case big-O time analysis for these two methods:
The remove method for the Sequence that is implemented using an array,
and the remove method for the Sequence that is implemented using a linked list.
Short Answer Section 4.6 Arrays vs.
Linked Lists vs. Doubly Linked Lists
|
Write a class definition that could be used to define a node in a doubly
linked list. Include only the instance variables, not the methods.
Also write one sentence to describe a situation when a
doubly linked list is appropriate.
Describe a situation where storing items in an array is clearly
better than storing items on a linked list.