-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleLinkedList.java
More file actions
48 lines (39 loc) · 1.48 KB
/
SimpleLinkedList.java
File metadata and controls
48 lines (39 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class SimpleLinkedList {
private String name;
private SimpleLinkedList next;
SimpleLinkedList(String n) { name = n; next = null; }
String getName() { return name; }
SimpleLinkedList getNext() { return next; }
void setName(String n) { name = n; }
void setNext(SimpleLinkedList nxt) { next = nxt; }
public String toString() { return name; }
public static void main(String[] args) {
// Construct six nodes
SimpleLinkedList name0 = new SimpleLinkedList("Diether"); //null;
SimpleLinkedList name1 = new SimpleLinkedList("Nathaniel");
SimpleLinkedList name2 = new SimpleLinkedList("Jethro");
SimpleLinkedList name3 = new SimpleLinkedList("Jheronimo");
SimpleLinkedList name4 = new SimpleLinkedList("Janice");
SimpleLinkedList name5 = new SimpleLinkedList("Brent");
// Link the nodes into a chain
name0.setNext(name1);
name1.setNext(name2);
name2.setNext(name3);
name3.setNext(name4);
name4.setNext(name5);
name5.setNext(null);
// Traverse the Linked List
SimpleLinkedList prnt = name0;
//int i = 0;
int count = 0; // count nodes
// Traverse the Linked List in a loop
while (prnt != null) {
count++;
//System.out.println("Name " + i + ": " + prnt);
//i++;
prnt = prnt.getNext();
}
System.out.println("Count of Nodes: " + count);
//System.out.print("\nEnd of List");
}
}