-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortedLinkedList.java
More file actions
86 lines (80 loc) · 2.96 KB
/
SortedLinkedList.java
File metadata and controls
86 lines (80 loc) · 2.96 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
//Jude Pierre
//SortedListClass
public class SortedLinkedList{
private Node head;
//Default constructor for head node
public SortedLinkedList(){
head = new Node();
}
//Checks whether the list is empty by looking at the next reference for head.
public boolean isEmpty(){
return head.nextRef == null;
}
//Member method used for inserting the student nodes in sorted order.
public void insertSorted(StudentRecord nextStudent){
Node current = head.nextRef;
Node previous = head;
while(current != null && nextStudent.compareTo(current.data)>0){
previous = current;
current = current.nextRef;
}
current = new Node(nextStudent, current);
previous.nextRef = current;
}
//Member method used for printing the data in each node.
public String toString(String outputFormat){
String s = "";
Node current = head.nextRef;
while(current != null){
s += current.data.toString(outputFormat);
current = current.nextRef;
}
s+="\n";
return s;
}
//Member method used to search for a student object in the list with the full name entered.
public Student searchSorted(StudentRecord studentToFind) throws Exception{
Node current = head.nextRef;
while((current != null) && (studentToFind.compareTo(current.data) != 0)){
current = current.nextRef;
}
if(current == null){
throw new Exception ("Student not found");
}
return current.data;
}
//Member method used to search for a specific student given the ID entered
public StudentRecord searchSortedByID(String ID) throws Exception{
Node current = head.nextRef;
while((current != null) && (current.data.compareToByID(ID))!= true){
current = current.nextRef;
}
if(current == null){
throw new Exception("Student not found");
}
return current.data;
}
//Method used to remove a student by switching reference variables.
public void removeStudent(Student studentToFind) throws Exception{
Node current = head.nextRef;
Node prev = head;
while((current != null) && (studentToFind.compareTo(current.data) > 0)){
prev = current;
current = current.nextRef;
}
if(current == null){
throw new Exception ("Student not found");
}
prev.nextRef = current.nextRef;
}
//Method used to return Student data. Had to update all of the Student variables to StudentReference because of this.
public String returnStudentData(){
String data= "";
Node current = head.nextRef;
while(current != null){
data+= current.data.returnData();
current = current.nextRef;
}
return data;
}
}