-
Notifications
You must be signed in to change notification settings - Fork 0
/
AdvCSLLOld.java
133 lines (114 loc) · 2.3 KB
/
AdvCSLLOld.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*************************
* AdvCSLL.java Author: Robert Walker
* Purpose: Implementation of a linked list.
*************************/
import java.util.ArrayList;
public class AdvCSLLOld<T> {
LLNode head;
// Constructor; instantiates head.
public AdvCSLLOld() {
head = null;
//head = new LLNode(null, null);
}
public T removeFirst() {
// if(head.next == null) {
// return "Empty";
// }
//
if(head == null) {
return null;
}
else {
T result = head.t;
if(head.next != null) {
head = head.next;
}
else {
head = null;
}
return result;
}
}
// Adds a node to the list.
public void add(T t) {
// If the list is empty, add the node at the first location.
if (head == null) {
LLNode newNode = new LLNode(null, t);
head = newNode;
}
// Else add at the end of the list.
else {
LLNode temp = head;
while (temp.next != null) {
temp = temp.next;
}
LLNode newNode = new LLNode(null, t);
temp.next = newNode;
}
}
// Searches for and removes a given node.
public String remove(T inT) {
LLNode temp = head;
// If head doesn't reference anything, the list is empty.
if (temp == null) {
return "Empty list";
} else {
while (temp.next != null) {
temp = temp.next;
if (temp.next.t == inT) {
String result = inT + "";
temp.next = temp.next.next;
return result;
}
else {
return "Not found";
}
}
return "Not found";
}
//return "Not in list";
}
// Prints out the list.
public String toString() {
String result = "";
LLNode temp = head;
while (temp.next != null) {
temp = temp.next;
result = result + temp.t + " ";
}
return result;
}
// Returns if the list contains a specific node.
public Boolean contains(T inT) {
LLNode temp = head;
while (temp.next != null) {
temp = temp.next;
if (temp.t == inT) {
return true;
}
}
return false;
}
// Empties the list by clearing head's reference.
public void clear() {
head= null;
}
// Returns if the list is empty.
public Boolean isEmpty() {
if (head == null) {
return true;
} else {
return false;
}
}
// LLNode Nested Class
public class LLNode {
public LLNode next;
public T t;
// Constructor
public LLNode(LLNode inNext, T inT) {
t = inT;
next = inNext;
}
}
}