Slides 34
Slides 34
if (Condition A || Condition B)
// Java3401.java
// This program reviews the creation of an "ordered" linked list.
// The <ListNode> class uses the <int> type for operating convenience.
// Remember that many different cases must be considered.
class ListNode
{
public ListNode (int initValue, ListNode initNext) { value = initValue; next = initNext; }
public int getValue () { return value; }
public ListNode getNext () { return next; }
public void setValue (int theNewValue) { value = theNewValue; }
public void setNext (ListNode theNewNext) { next = theNewNext; }
private int value;
private ListNode next;
}
class List
{
ListNode front;
int size;
class ListNode
{
public ListNode (int initValue, ListNode initNext) { value = initValue; next = initNext; }
public int getValue () { return value; }
public ListNode getNext () { return next; }
public void setValue (int theNewValue) { value = theNewValue; }
public void setNext (ListNode theNewNext) { next = theNewNext; }
private int value;
private ListNode next;
}
// Java3403.java
// This program demonstrates how to avoid "dereferencing" <null>
// with a conditional statement prior to working with <nodeObj>.
class ListNode
{
public ListNode (int initValue, ListNode initNext) { value = initValue; next = initNext; }
public int getValue () { return value; }
public ListNode getNext () { return next; }
public void setValue (int theNewValue) { value = theNewValue; }
public void setNext (ListNode theNewNext) { next = theNewNext; }
private int value;
private ListNode next;
}
AP Exam Alert
Dereferencing NULL is a definite
issue on the APCS exam.
class ListNode
{
public ListNode (int initValue, ListNode initNext) { value = initValue; next = initNext; }
public int getValue () { return value; }
public ListNode getNext () { return next; }
public void setValue (int theNewValue) { value = theNewValue; }
public void setNext (ListNode theNewNext) { next = theNewNext; }
private int value;
private ListNode next;
}
class MyStack public int pop()
{ {
private ListNode top; if (top != null)
private ListNode temp;
{
public MyStack() { clearStack(); } int topValue = top.getValue();
top = top.getNext();
public void clearStack() return topValue;
{ }
top = null; else
temp = null; return 0;
} }
public boolean emptyStack()
{ public int peek()
return top == null; {
} if (top != null)
return top.getValue();
public void push(int num) else
{ return 0;
temp = new ListNode(num,top);
}
top = temp;
}
}
// Java3405.java
// This program implements a <MyStack> class using the <ListNode> class, but
// now stores <Object> stack values for more general processing.
class ListNode
{
public ListNode (Object initValue, ListNode initNext) { value = initValue; next = initNext; }
public Object getValue () { return value; }
public ListNode getNext () { return next; }
public void setValue (Object theNewValue) { value = theNewValue; }
public void setNext (ListNode theNewNext) { next = theNewNext; }
private Object value;
private ListNode next;
}
class MyStack public Object pop()
{ {
private ListNode top; if (top != null)
private ListNode temp; {
Object stackItem = top.getValue();
public MyStack() { clearStack(); }
top = top.getNext();
public void clearStack() return stackItem;
{ }
top = null; else
temp = null; return null;
} }
Nodes are removed starting with the first node that was
created.
A B C D E
A B C D E
// Java3407.java
// This program creates a queue with the <ListNode> class.
// At the conclusion the queue becomes a "circular linked List".
public class Java3407
{
public static void main (String args[])
{
System.out.println("\nJAVA3407.JAVA\n");
ListNode temp = new ListNode("John",null);
ListNode front = temp;
ListNode back = temp;
temp = new ListNode("Greg",null);
back.setNext(temp);
back = temp;
temp = new ListNode("Maria",null);
back.setNext(temp);
back = temp;
temp = new ListNode("Heidi",null);
back.setNext(temp);
back = temp;
back.setNext(front); // turns linear linked list into a circular linked list
for (int k = 1; k <= 12; k++)
{
System.out.println(front.getValue());
front = front.getNext();
}
System.out.println();
}
}
// Java3408.java
// This program demonstrates the <MyRing> class, which creates circular linked lists.
public class Java3408
{
public static void main (String args[])
{
System.out.println("\nJAVA3408.JAVA\n");
MyRing numbers = new MyRing();
for (int k = 10; k <= 18; k++)
numbers.enRing(String.valueOf(k));
numbers.resetPeek();
for (int k = 1; k <= 25; k++)
System.out.print(numbers.nextPeek() + " ");
System.out.println("\n\n");
}
}
class ListNode
{
public ListNode (int initValue, ListNode initNext) { value = initValue; next = initNext; }
public int getValue () { return value; }
public ListNode getNext () { return next; }
public void setValue (int theNewValue) { value = theNewValue; }
public void setNext (ListNode theNewNext) { next = theNewNext; }
private int value;
private ListNode next;
}
class MyRing public void enRing(Object ringItem)
{ {
private ListNode start, end, temp; temp = new ListNode(ringItem,null);
if (start == null) {
start = end = temp;
public MyRing() { clearRing(); } start.setNext(end);
}
else {
public void clearRing() end.setNext(temp);
{ end = temp;
start = end = temp = null; end.setNext(start);
}
} }
24 15 34.678 54 117.03
rowNr nextRow nextCol colNr data nextCol colNr data nextCol
24 15 34.678 54 117.03
rowNr nextRow nextCol colNr data nextCol colNr data nextCol
public Sparse()
{
head = null;
first = true;
}
}
Sparse class Methods
initializes the head reference to null
constructor and sets the boolean variable first to
true
responsible for gathering data from the
enterSparse program user and calling the proper
methods to handle the new information
If first is true then enterSparse will
firstNode call this method, which will create the
very first header node and well as the
first column node.
This is the workhorse method in the
insertNode group. It insures that new nodes are
properly inserted in the existing matrix.
Traverses the entire matrix structure
displaySparse and displays all the row values, column
values and data values that are stored.
// Java3410.java The simplicity of the main method hides
// This program implementsthe real work performed
a "sparse by this
matrix" class usingprogram.
// a linked list of linkedSeveral
lists. other methods will do the real
work of this program, but these methods
import java.util.*; are not called from the main method body.
24 15 34.678
public void insertNode(int value, int row, int col)
{
ColNode c1 = null, c2 = null, c3 = null; // used to create new col nodes
RowNode r1 = null, r2 = null, r3 = null; // used to create new row nodes
/****************************************************************
/* The first consideration is to determine if the new node needs
/* to be placed ahead of the existing first header node. This
/* situation occurs if the row value is less than the row number
/* value of the first header node. If this is true, a new header
/* node needs to be created, which will be linked to the
/* insertion node and the previous first header node.
/****************************************************************/
else
{
r2 = head;
while (r2 != null && row > r2.getRowNumber())
// traverse to find the proper row location
{
r3 = r2;
r2 = r2.getNextRow();
}
/****************************************************************
/* When the loop exits, it is possible that row is equal to the
/* current row number. In that case it is not necessary to create
/* a new header node. A check is made to see if this is true by
/* comparing row and the current row number.
/****************************************************************/
{
c2 = r2.getNextCol();
if (col < c2.getColNumber())
// new node fits between header node and the first col node
{
r2.setNextCol(c1);
c1.setNextCol(c2);
}
/****************************************************************
/* If the insertion node does not link at the head of the current
/* linked list, a search must be performed to find the proper
/* column position. The c2 reference traverses with a while loop
/* as long as col is greater than the column number. Note that once
/* again short-circuiting prevents dereference problems.
/* Another reference, c3 trails c2 one node behind. When c2 stops
/* there are now two references at the location for the insertion
/* node. c3 links to the insertion node c1 and the insertion
/* node links to c2.
/****************************************************************/
else
{
while (c2 != null && col > c2.getColNumber())
// find proper col insertion position
{
c3 = c2;
c2 = c2.getNextCol();
}
c3.setNextCol(c1);
c1.setNextCol(c2);
}
}
/****************************************************************
/* It is easy to think that the job is done. However, there
/* exists one more very important case. This is the situation
/* of placing the insertion node neither before the first header
/* node nor in a linked list with an existing header node. In
/* this case a new header node needs to be established to start
/* a new linked list. The insertion node will be the first
/* column node in the new linked list. After all the previous
/* checks and loops, r2 is now at the location in the sparse
/* matrix where the new linked list needs to be created
/****************************************************************/
else
{
r1 = new RowNode(row,c1,r2);
r3.setNextRow(r1);
}
}
}
insertNode Warning
The logic and the code of method insertNode is
quite complex, especially the first time around.