|
APCS Java Subset | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectap.ListNode
The class for linked list nodes that will be used
and tested on the
Advanced Placement Computer Science exam.
For example, the two questions below, similar to
Questions 15 and 33 on the 1992 released AB exam, show
some of the ListNode
methods in use. These
are shown here as two questions emerging from common code. On
the released exam these questions were not related.
(Question 15)
Consider the static methods print
and create
in the class List
below. What is printed
by the call List.print(List.create(5))
?
5 4 3 2 1
1 2 3 4 5
5 1
5
1
(Question 33) Consider the static method process
in the class List
below.
Suppose p
represents
the list
(1,2,3,4)
; what is
returned by the call List.process(p,null)
?
null
(1)
(1,1,1,1)
(4,3,2,1)
(1,2,3,4)
public class List { // return a linked list based on parameters list and soFar public static ListNode process(ListNode list, ListNode soFar) { if (list == null) { return soFar; } else { ListNode temp = list.getNext(); list.setNext(soFar); return process(temp,list); } } // return a new linked list based on parameter n public static ListNode create(int n) { ListNode list = null; for(int k=1; k <= n; k++) { list = new ListNode(new String(""+k), list); } return list; } // print a linked list public static void print(ListNode list) { while (list != null) { System.out.print(list.getValue()+" "); list = list.getNext(); } System.out.println(); } }
Constructor Summary | |
ListNode(java.lang.Object initValue,
ListNode initNext)
Construct a list node with specified value and next node. |
Method Summary | |
ListNode |
getNext()
Returns the next node after this node. |
java.lang.Object |
getValue()
Returns the value stored in this node. |
void |
setNext(ListNode theNewNext)
Sets the next node after this node |
void |
setValue(java.lang.Object theNewValue)
Sets the value stored in this node. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public ListNode(java.lang.Object initValue, ListNode initNext)
initValue
- is the value in the constructed nodeinitNext
- is the value of the next node (returned by
getNext()
)Method Detail |
public java.lang.Object getValue()
public ListNode getNext()
public void setValue(java.lang.Object theNewValue)
theNewValue
- is the (new) value of this nodepublic void setNext(ListNode theNewNext)
theNewNext
- is the (new) next node after this node
|
unofficial documentation for the APCS Java Subset | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |