This repository was archived by the owner on Nov 17, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathSLL.java
More file actions
183 lines (172 loc) · 4.31 KB
/
Copy pathSLL.java
File metadata and controls
183 lines (172 loc) · 4.31 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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import java.util.*;
/**
* Used to create and maintain the SLL.
* Created by Pascal Seguin Febuary 11, 2018.
*/
public class SLL<T extends Comparable<T>>
{
Node<T> head, tail;
/**
* Adds the takin in data in the order dictated by the passsed in Comparator to the SLL. Also converts them to Nodes.
*/
public void addFreq(T n, Comparator<T> compare)
{
Node<T> node = new Node<T>(n);
if (head == null) {
head = node;
}
else {
if ( compare.compare(node.getData(), head.getData()) <= 0) // less than first item, insert here.
addHead(node);
else
{
Node<T> curr = head;
while (curr.getNext() != null && compare.compare(node.getData(), curr.getNext().getData()) > 0)
{
curr = curr.getNext();
}
if (curr.getNext() == null) // did not find a place
{
curr.setNext(node);
}
else
{
node.setNext(curr.getNext());
curr.setNext(node);
}
}
}
}
/**
* Empties the to all for new information to be added.
*/
public void emptyList()
{
head = null;
}
/**
* Orders the created nodes based on the tocompare metheod of ordering. Gives the order for the nodes to be added in.
*/
public void addInOrder(T n)
{
Node<T> node = new Node<T>(n);
if (head == null) {
head = node;
}
else {
if ( (node.getData().compareTo( head.getData())) <= 0) // less than first item, insert here.
addHead(node);
else
{
Node<T> curr = head;
while (curr.getNext() != null && (node.getData().compareTo(curr.getNext().getData())) > 0)
{
curr = curr.getNext();
}
if (curr.getNext() == null) // did not find a place
{
addTail(node);
}
else
{
node.setNext(curr.getNext());
curr.setNext(node);
}
}
}
}
/**
* Adds the selected node to the front of the SLL.
*/
public void addHead( Node<T> n)
{
if (head == null)
{
head = n;
}
else
{
n.setNext(head);
head = n;
}
}
/**
* Returns the size of the SLL.
*/
public int size()
{
Node<T> curr = head;
int i = 0;
while(curr != null)
{
i++;
curr = curr.getNext();
}
return i;
}
/**
* Checks the SLL for the word that matches the passed in data.
*/
public boolean contains(Word w)
{
return indexOf(w) != -1;
}
/**
* Checks the SLL for the matching word data and returns the index of the node that contains it.
* Or returns flag if node is not in the SLL.
*/
public int indexOf(Word w)
{
Node<T> curr = head;
int i = 0;
while (curr != null)
{
if (curr.getData().equals(w))
{
return i;
}
i++;
curr = curr.getNext();
}
return -1;
}
/**
* Gets/returns the data of the node at the passed in location.
*/
public T get(int i)
{
Node<T> curr = head;
int count = 0;
int length = size();
T data = null;
if (i <= length && i >= 0)
{
while (count < i)
{
curr = curr.getNext();
count++;
}
data = curr.getData();
}
return data;
}
/**
* Adds the passed in node to the end of its respective SLL.
*/
public void addTail(Node<T> data)
{
if (head == null)
{
head = data;
}
else
{
Node<T> curr = head;
while(curr.getNext() != null)
{
curr = curr.getNext();
}
curr.setNext(data);
}
}
}