# Homework2 by stariya

VIEWS: 9 PAGES: 9

• pg 1
```									1052 Homework 2 Linked Lists

For the following questions, assume that a linked list is implemented using the Node class
where a Node contains instance data of type int called info; and Node next; where next
references the next Node in the linked list. Also assume that head references the first Node
in the list.

1) Which of the following instructions would create an initially empty linked list?
a) Node head = new Node( );
d) Node head = new Node(0);

2)    Assume that the linked list has at least two Nodes in it. Which of the following
instructions will return the second int value in the list?
e) It is not possible to return the second int value in the list using head.

3) Assume Node temp is currently set equal to head. Which of the following while loops
could be used to iterate through each element of a linked list?
b) while (temp != null)
temp = temp.next;
temp = temp.next;
e) while (temp != null)

4) Assume Node2 is defined as follows:
int data; Node2 a, b;
where a refers to the Node2 before this one in a linked list and b refers to the Node2 after
this one in a linked list. Node2 could then be used to create which of the following
d) singly linked list with a top node

Assume that the countIt and sumIt methods receive a parameter Node temp, which
references the first Node in a linked list where Node is a class that consists of data instances
int info and Node next and further assume that the int variables count is initialized to 0.

5) Which of the following methods could be used to count the number of items in the linked
list?
a) public int countIt(Node temp)
{
while(temp != null)
{
count += temp.info;
temp = temp.next;
}
return count;
}

b) public int countIt(Node temp)
{
while(temp != null)
{
count++;
}
return count;
}

c) public int countIt(Node temp)
{
while(temp != null)
{
count++;
temp = temp.next;
}
return count;
}

d) public int countIt(Node temp)
{
{
count++;
temp = temp.next;
}
return count;
}

e) public int countIt(Node temp)
{
while(temp != null)
{
if(next != null) count++;
temp = temp.next;
}
return count;
}

6. Which of the following methods could be used to sum all of the items in the linked list?
a) public int sumIt(Node temp)
{
while(temp != null)
{
sum += temp.info;
temp = temp.next;
}
return sum;
}

b) public int sumIt(Node temp)
{
while(temp != null)
{
sum += temp.info;
}
return sum;
}

c) public int sumIt(Node temp)
{
while(temp != null)
{
sum++;
temp = temp.next;
}
return sum;
}

d) public int sumIt(Node temp)
{
{
sum += temp.info;
temp = temp.next;
}
return sum;
}

e) public int sumIt(Node temp)
{
while(temp != null)
{
if(next != null) sum += temp.info;
temp = temp.next;
}
return sum;
}

For questions 7 - 8, assume that a linked list consists of Node objects, where Node has two
instance data, int info and Node next. The linked list stores in the info data, 20, 11, 13, 19,
12, 14 in that order. Assume that Node head references the first item in the list.

7) What will be returned by return head.next.next.next.info; ?
a) 20
b) 11
c) 13
d) 19
e) 12

8) What is returned by return head.info; ?
a) 20
b) 11
c) 13
d) 19
e) 6

9) A variation of a linked list is a circular linked list where the last Node in the list has next
= head rather than next = null. One problem with this type of list is that
a) it wastes memory space since head already points at the first Node, so the last
one does not need to
b) there is no ability to add a new Node at the end of the list since the last Node
points at the first Node
c) it is more difficult to traverse the list since the old terminating condition, (next =
= null), is no longer true for the last node
d) a header Node for this type of list is more complex
e) all of the above

For questions , assume Node is a class consisting of an int info and a Node next and header
is a HeaderNode that has Node front, rear and int count where front references the first
item in the list, rear references the last item in the list and count is the number of elements
in the list.

10) Which of the following for-loops could replace the previous while loop that would start
at head and go until temp == null?
a) for(Node temp = header.front, int j = 0; j < header.count; temp = temp.next) { … }
b) for(int j = 0; j < header.count; j++) { … }
c) for(Node temp = header.front; temp != header.rear; temp = temp.next) { … }
d) for(Node temp = header.front, int j = 0; j < header.count; temp = temp.next, j++) {
…}
temp = temp.next, j++) { … }

11) This type of linked list is referred to as a

For questions 12 , assume that a DoubleNode is a Node with an int info; and DoubleNode
previous, next; with DoubleNode head referencing the first item in this list. Assume that the
list stores the int values 0, 6, 3, 2, 8 in that order. Draw this structure.

Free-form Questions:

For questions 1 – 4, use the following class definition of a linked list Node:
class Node
{
int info;
Node next;
}

1) Show the instructions required to create a linked list that is referenced by head and
stores in order, the int values 3, 6 and 2. Assume that Node’s constructor receives no
parameters.

2) Assume that head references a linked list that stores the values 3, 6 and 2 in that order.
Show the instructions needed to move the value 2 in front of the value 6 (so that the list is
now 3, 2, 6).

3) Assume that head references a linked list although we don’t know what is currently
stored in that list. Write a block of code using a try-catch block that will work through
the entire linked list printing each element out, stopping only when we have reached the
end of the list because a NullPointerException is thrown. Once the Exception is thrown,
output the number of elements found in the list.

4. Suppose that a and b are IntNode variables. Write one clear sentence to tell me when the
expression (a==b) will be true.

5. Compare the worst-case big-O time analysis for these two methods: The add method for
the ArrayList class , and the add method for the linked list class.

6. Compare the worst-case big-O time analysis for these two methods: The delete method
for the ArrayList that is implemented using a fixed-sized array, and the remove method for
the collection that is implemented using a linked list.

7. 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.

8. Describe a situation where storing items in an array is clearly better than storing items on

9. A double linked circular linked list might be used in a network application or in an
encryption program where the code for plain text message is stored. Write an insert
method for the DLCircular class where:

LIST

Next         Next          Next       Next       Next       Next
5            10            20         30         140        92
prev         prev          prev       prev       prev       prev

The LIST reference points to the last node. A reference needs to be kept to a current
pointer as the place where the new node will be inserted.

10. Draw arrow and boxes for the following code where both current and previous are
pointing to nodes in the list.
ListNode newNode = new ListNode();
newNode.data = newData;
if (current != null)
{
newNode.next = current.next;
current.next = newNode;
}

11. Draw the list created by the following code:
Node head = new Node(1, null);
Node n = new Node(2, null);

Node p = null;

12. Draw arrows and boxes to add to the front of list.

13. Draw arrows and boxes to add in the middle of the list.

14. Here is the source code of a singly linked list class. Read the code and explain what the
method mystery does?

public class SLList
{
public SLList()
{
}

// nesting private class: Node
private class Node
{
String value;
Node next;

private Node(String v)
{

value = v;
next = null;
}
}

public void printAll()
{
if( probe==null )
{
System.out.println("this list is empty");
return;
}

while ( probe!=null )
{
System.out.println( probe.value);
probe = probe.next;
}
}
public void mystery(String value)
{
Node newNode = new Node(value);

{
}
else
{
while(prob.next!=null )
prob = prob.next;
prob.next = newNode;
}
}
}

Multiple Choice
1. Suppose cursor refers to a node in a linked list (using the IntNode class with
instance variables called data and link). What statement changes cursor so that it
refers to the next node?
o A. cursor++;
o B. cursor = next;
o C. cursor += next;
o D. cursor = cursor.next;
o
2. Suppose cursor refers to a node in a linked list (using the Node class with instance
variables called data and next). What boolean expression will be true when cursor
refers to the tail node of the list?
o A. (cursor == null)
o B. (cursor.next == null)
o C. (cursor.data == null)
o D. (cursor.data == 0.0)
o E. None of the above.
o
3. Which boolean expression indicates whether the numbers in two nodes (p and q) are
the same. Assume that neither p nor q is null.
o A. p == q
o B. p.data == q.data
o C. p.next == q.next
o D. None of the above.
o
4. Suppose that p is a reference variable that contains the null reference. What
happens at runtime if the program tries to activate a method of p?
o A. IllegalArgumentException
o B. IllegalStateException
o C. NullPointerException
o D. The results are unpredictable.
o
5. Suppose that a method has one node as a parameter and it returns two references to
nodes. What's the best header for the method?
o A. IntNode foo(IntNode p)
o B. IntNode, IntNode foo(IntNode p)
o C. IntNode[ ] foo(IntNode p)

6. In the linked list version of the LinkedList class, an instance variable manyNodes is
used to keep track of how long the linked list is. Why not just make a call to the
IntNode method listLength()?
o A. The listLength() method is O(n) and the alternative is O(1).
o B. The listLength() method is private.
o C. The listLength() method results in an infinite loop for circular lists.
o D. The listLength() method works only for lists of integers.

7. Suppose that the ArrayList class is implemented with a linked list. Which of these
operations are likely to have a constant worst-case time?
o B. countOccurrences
o C. remove
o D. None of (A), (B), and (C) have a constant worst-case time
o E. TWO of (A), (B), and (C) have a constant worst-case time
o F. ALL of (A), (B), and (C) have a constant worst-case time
o

9. What kind of list is best to answer questions such as "What is the item at position n?"
A. Lists implemented with an array.