-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoublyLinkedSortedList.java
More file actions
178 lines (156 loc) · 5.53 KB
/
DoublyLinkedSortedList.java
File metadata and controls
178 lines (156 loc) · 5.53 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
public class DoublyLinkedSortedList implements DoublyLinkedSortedListInterface
{
private Node first; //head
private Node last; //tail
//constructor
public DoublyLinkedSortedList()
{
first = null;
last = null;
}
//returns first node in the list
@Override
public Node getFirst()
{
return first;
}
//returns last node in the list
@Override
public Node getLast()
{
return last;
}
//removes a node from the list
@Override
public Node remove(HurricaneRowData toRemove)
{
//placeholder to start from beginning
Node currentNode = first;
//while this currentnode is not null (i.e: not end of list)
while(currentNode != null)
{
//checks if this is the node to remove
if(currentNode.getValue().equals(toRemove))
{
//creates references to neighboring nodes nodes previous and next
Node previousNode = currentNode.getPrevious();
Node nextNode = currentNode.getNext();
//updates previous node next pointer
//if there is a previous node, connect it to next node
if(previousNode != null)
previousNode.setNext(nextNode);
//if there is not anything behind previous node, then remove first node and make it nextnode
else
first = nextNode;
//updates next node's previous pointer
//if next node is not null, connect it to previous node
if(nextNode != null)
nextNode.setPrevious(previousNode);
else
//remove next node
last = previousNode;
//return removed node
return currentNode;
}
//move to next node in the list
currentNode = currentNode.getNext();
}
//returns null if value not found
return null;
}
@Override
public void insert(HurricaneRowData newValue)
{
//new node that will store the ace data
Node node = new Node(newValue);
//There are 4 cases to this: inserting in a null linked list, inserting at the beginning, insertion in the middle, and insertion at the end
//inserting in an empty list
if(first == null)
{
first = node;
last = node;
return;
}
//starts the search from the beginning
Node currentNode = first;
//while the current ace is larger
while(currentNode != null && currentNode.getValue().getAceIndex() > newValue.getAceIndex())
{
currentNode = currentNode.getNext();
}
//insert at beginning
if(currentNode == first)
{
node.setNext(first); //new node points to old
first.setPrevious(node); //old first points to new node
first = node; //updates head of the list
}
//inserts at the end
else if(currentNode == null)
{
last.setNext(node); //old last points to new node
node.setPrevious(last); //new node points to old last
last = node; //update tail
}
//inserts at the middle
else
{
//gets node before insertion point
Node previousNode = currentNode.getPrevious();
//links previous node to new node
previousNode.setNext(node);
node.setPrevious(previousNode);
//links new node to current node
node.setNext(currentNode);
currentNode.setPrevious(node);
}
}
//converts linked list to a string
@Override
public String toString()
{
String result = ""; // empty string
Node current = first; //sets current node as first node
while(current != null)
{
result += current.getValue().toString() + "\n";
current = current.getNext();
}
return result;
}
//insert two public string methods here
//this will test the insert/getfirst and remove methods
public String testFirst()
{
//creates new list
DoublyLinkedSortedList list = new DoublyLinkedSortedList();
//insert new test data
list.insert(new HurricaneRowData(1920, 500, 20, 2, 2));
list.insert(new HurricaneRowData(1921, 400, 20, 2, 2));
list.insert(new HurricaneRowData(1922, 100, 20, 2, 2));
list.insert(new HurricaneRowData(1923, 50, 20, 2, 2));
//checks if ace of 1920 (largest ace) is correct
if(list.getFirst().getValue().getAceIndex() == 500)
return "GETFIRST PASS";
return "FAIL";
}
public String testRemove()
{
DoublyLinkedSortedList list = new DoublyLinkedSortedList();
//same list as last time but with variables
HurricaneRowData a = new HurricaneRowData(1920, 500, 20, 2, 2);
HurricaneRowData b = new HurricaneRowData(1921, 400, 20, 2, 2);
HurricaneRowData c = new HurricaneRowData(1922, 100, 20, 2, 2);
HurricaneRowData d = new HurricaneRowData(1923, 50, 20, 2, 2);
list.insert(a);
list.insert(b);
list.insert(c);
list.insert(d);
//remove 1920 ace
list.remove(a);
//if 1920 is top ace it passes
if(list.getFirst().getValue().getAceIndex() == 400)
return "REMOVE METHOD PASS";
return "FAIL";
}
}