-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedList.java
More file actions
224 lines (176 loc) · 4.17 KB
/
linkedList.java
File metadata and controls
224 lines (176 loc) · 4.17 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import java.lang.*;
public class linkedList {
private nodeClass head;
private int itemsCount = 0;
//The constructor sets the initial node to null
public linkedList()
{
head = new nodeClass(null);
itemsCount = 0;
}
//The object data can be an instance from the
//teacher class or the student class
public void add(objectsClass data){
//The node you want to add
nodeClass tempNode = new nodeClass(data);
//The current node is set to the head
nodeClass currentNode = head;
//Current node starts at the head
//and iterates through all the nodes
//to check if they're the last node
//in the list.
while (currentNode.getNext()!=null)
{
currentNode = currentNode.getNext();
}
currentNode.setNext(tempNode);
itemsCount++;
}
public void add(objectsClass data, int fence)
{
//The
nodeClass tempNode = new nodeClass(data);
nodeClass currentNode = head;
//Iterate until the fence
for (int i = 1; i<fence && currentNode.getNext()!=null; i++)
{
currentNode = currentNode.getNext();
}
//If the currentNode points to null, temp points to null
//If not, temp points to the node after currentNode
tempNode.setNext(currentNode.getNext());
//curreNode points to temp
currentNode.setNext(tempNode);
itemsCount++;
}
//Returns the object at the fence
public Object get(int fence)
{
if(fence<=0){ return null; }
nodeClass currentNode = head.getNext();
for (int i = 1; i<fence; i++)
{
if(currentNode.getNext() == null)
{
return null;
}
currentNode = currentNode.getNext();
}
return currentNode.getName();
}
public boolean remove(int fence)
{
if(fence<1 || fence > size()) { return false; }
nodeClass currentNode = head;
for (int i =1; i<fence; i++)
{
if(currentNode.getNext()==null) return false;
currentNode = currentNode.getNext();
}
currentNode.setNext(currentNode.getNext().getNext());
itemsCount--;
return true;
}
public int size()
{
return itemsCount;
}
public String toString()
{
nodeClass currentNode = head.getNext();
String output ="";
while (currentNode!=null)
{
output += "[" + currentNode.getType()+" "+currentNode.getId()+" "+currentNode.getName()+ "]";
currentNode = currentNode.getNext();
}
return output;
}
public boolean sortList()
{
//Sort list by name
//Current Node = head
//if current->next == null
//return false;
//Look at the beginning of each name
//See if there's a function that sorts already
//If one is greater than the other (ex a = node b = node and a>b)
//and it is b and then a
//b -> next = a.next (which is after b)
//Then a -> next = b
//Need a temp to iterate over the entire line
//Need a current to keep the one that you're comparing
// ex current = b temp = a
//if a>b
//Then current->next = a->next
//And temp ->next = b;
//Sorting algorithm:
//Compare(String1, String2)
//If returns < 0
//String 1 <Sting 2
//If return 0
//String 1 = String 2
//If returns >0
//String 1>String 2
nodeClass temp = head.getNext().getNext(); // b
nodeClass current = head.getNext(); //a
if(current == null) return false;
for (int i = 1; i<size(); i++)
{
if(temp.getName().compareTo(current.getName())<0)
{
}
//(temp.getName(), current.getName());
}
return true;
}
}
class nodeClass {
nodeClass next;
objectsClass data;
//This constructor sets the next node to null
public nodeClass(objectsClass dataValue)
{
next = null;
data = dataValue;
}
//This constructor specifies which node to point to
public nodeClass(nodeClass nextNode, objectsClass dataValue)
{
next = nextNode;
data = dataValue;
}
//Setter and Getter Functions
public String getType()
{
return data.getType();
}
public void setType(String type)
{
data.setType(type);
}
public String getName()
{
return data.getName();
}
public void setName(String name)
{
data.setName(name);
}
public String getId()
{
return data.getId();
}
public void setId(String id)
{
data.setId(id);
}
public nodeClass getNext()
{
return next;
}
public void setNext(nodeClass nextNode)
{
next = nextNode;
}
}