(20 Points) 1. Respond to the following true/false questions.
(30 Points) 2. Consider the following conceptual representation for a linked list. head references the first node in the list.
----- ----- ----- ----- | 434 |===>| 231 |===>| 111 |===>...| 234 |===|| ----- ----- ----- ----- head
Here is the definition of our node class:
class Node { int element_; Node next_; Node (Node n, int e) { next_=n; element_=e; } }
Node temp;
Write the instruction(s) that would
make temp reference the 3rd node in the above list.
int count (Node head) { }
Here are the definitions of node and intList classes from lab4:
public class node { int element_; node next_; node (int e, node n) } public class intList { node header_; node current_; public intList( ) public boolean isEmpty( ) public void makeEmpty( ) public void insert( int x ) throws Exception public void insertAtBeginning( int x ) public boolean find( int x ) public void remove( int x ) throws Exception public boolean validCurrent( ) public int retrieve( ) throws Exception public void first( ) public void advance( ) public String dump () }
(10 Points) 3.Write a boolean method isLast for the intList
class. This method throws an exception if current is invalid; otherwise,
the next_ of the current node should be checked and either true or
false returned based on it being null or not.
(10 Points) 4.Write a method update (int newValue)
for the intList
class. This method throws an exception if current is invalid; otherwise,
it places newValue in the element_ of the current node.
(20 Points) 5. Suppose we have an application that maintains a list of
salaries in an intList variable (list) with values like
10000, 40000, etc. Understand that you are in an application and have no
access to current_ or header_ of list. Assume that the
method update exists in intList. Write a code segment that
loops through and updates every salary by giving each a 10% raise.
(10 Points) 6. Assume that the intList L has the following list in it, note that current_ is on the 1st node when this method is called:
current_ ----- ----- ----- ----- | 134 |===>| 231 |===>| 311 |===>| 434 |===|| ----- ----- ----- ----- head_Now consider the following recursive method:
int test2 (int i, intList L) { if (L.invalidCurrent()) return -1; try { if (L.retrieve() == i) return 0; } catch (Exception e){} int temp = test2(i,L); if (temp == -1) return -1; else return temp+1; }