LinkedList<Integer> list = new LinkedList<Integer>();
Quote:
Originally Posted by SamwiseRed
[You must be logged in to view images. Log in or Register.]
Code:
//naw naw
//15 Feb 2015
//IntNode.java
import java.util.*;
public class IntNode{
//instance variables
private IntNode link;
private int value;
//---Constructors---
IntNode(){
link = null;
value = 0;
}//end default constructor
IntNode(int d, IntNode n){
link = n;
value = d;
}//end constuctor
//---getters---
public IntNode getLink(){
return link;
}//end getLink
public int getValue(){
return value;
}//end getValue
//---setters---
public void setLink(IntNode n){
link = n;
}//end setLink
public void setValue(int d){
value = d;
}//end setValue
public String toString(){
String str = "";
for(IntNode cursor = this; cursor != null; cursor = cursor.link)
str += cursor.value + " ";
return str;
}//end toString method
public static int listLength(IntNode head){
int answer = 0;
for(IntNode cursor = head; cursor != null; cursor = cursor.link)
answer++;
return answer;
}//end listLength
public static void main(String args[]){
IntNode node6 = new IntNode(6, null);
IntNode node5 = new IntNode(5, node6);
IntNode node4 = new IntNode(4, node5);
IntNode node3 = new IntNode(3, node4);
IntNode node2 = new IntNode(2, node3);
IntNode node1 = new IntNode(1, node2);
System.out.println(node1);
System.out.println(listLength(node1));
}//end main
}//end class IntNode
|