This repository has been archived by the owner on Nov 9, 2023. It is now read-only.
forked from ucsd-cse15l-f22/lab3
-
Notifications
You must be signed in to change notification settings - Fork 826
/
LinkedListExample.java
91 lines (89 loc) · 2.21 KB
/
LinkedListExample.java
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import java.util.NoSuchElementException;
class Node {
int value;
Node next;
public Node(int value, Node next) {
this.value = value;
this.next = next;
}
}
class LinkedList {
Node root;
public LinkedList() {
this.root = null;
}
/**
* Adds the value to the _beginning_ of the list
* @param value
*/
public void prepend(int value) {
// Just add at the beginning
this.root = new Node(value, this.root);
}
/**
* Adds the value to the _end_ of the list
* @param value
*/
public void append(int value) {
if(this.root == null) {
this.root = new Node(value, null);
return;
}
// If it's just one element, add if after that one
Node n = this.root;
if(n.next == null) {
n.next = new Node(value, null);
return;
}
// Otherwise, loop until the end and add at the end with a null
while(n.next != null) {
n = n.next;
n.next = new Node(value, null);
}
}
/**
* @return the value of the first element in the list
*/
public int first() {
return this.root.value;
}
/**
* @return the value of the last element in the list
*/
public int last() {
Node n = this.root;
// If no such element, throw an exception
if(n == null) { throw new NoSuchElementException(); }
// If it's just one element, return its value
if(n.next == null) { return n.value; }
// Otherwise, search for the end of the list and return the last value
while(n.next != null) {
n = n.next;
}
return n.value;
}
/**
* @return a string representation of the list
*/
public String toString() {
Node n = this.root;
String s = "";
while(n != null) {
s += n.value + " ";
n = n.next;
}
return s;
}
/**
* @return the number of elements in the list
*/
public int length() {
Node n = this.root;
int i = 0;
while(n != null) {
i += 1;
n = n.next;
}
return i;
}
}