-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedSortedList.java
125 lines (108 loc) · 2.81 KB
/
LinkedSortedList.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
public class LinkedSortedList implements UnsortedListInterface {
protected int listSize;
protected LinkedListNode<String> next;
protected LinkedListNode<String> firstValue;
protected LinkedListNode<String> targetLocation;
protected LinkedListNode<String> pre;
protected boolean found;
public LinkedSortedList() {
this.listSize = 0;
this.firstValue = null;
this.next = null;
}
public void add(String value) {
if (this.firstValue == null) {
LinkedListNode<String> nuevo = new LinkedListNode<>();
nuevo.setHead(value);
this.firstValue = nuevo;
} else {
this.targetLocation = this.firstValue;
this.pre = null;
while (this.targetLocation != null) {
if (this.targetLocation.getHead().compareTo(value) < 0) {
this.pre = this.targetLocation;
this.targetLocation = this.targetLocation.getNext();
} else {
break;
}
}
LinkedListNode<String> newNode = new LinkedListNode<>();
if (this.pre == null) {
newNode.setHead(value);
newNode.setNext(this.firstValue);
this.firstValue = newNode;
} else {
newNode.setHead(value);
newNode.setNext(this.targetLocation);
this.pre.setNext(newNode);
}
}
this.listSize++;
}
public void find(String target) {
this.targetLocation = this.firstValue;
this.pre = null;
this.found = false;
while (this.targetLocation != null) {
if (this.targetLocation.getHead().compareTo(target) == 0) {
this.found = true;
break;
} else {
this.pre = this.targetLocation;
this.targetLocation = this.targetLocation.getNext();
}
}
}
public int size() {
return this.listSize;
}
public boolean contains(String value) {
find(value);
return this.found;
}
public boolean remove(String value) {
if (contains(value)) {
if (this.firstValue == this.targetLocation) {
this.firstValue = this.firstValue.getNext();
} else {
this.pre.setNext(this.targetLocation.getNext());
}
this.listSize--;
}
return this.found;
}
public String get(String value) {
if (contains(value)) {
return this.targetLocation.getHead();
} else {
return null;
}
}
public void printing() {
this.targetLocation = this.firstValue;
while(this.targetLocation != null) {
System.out.println(this.targetLocation.getHead());
this.targetLocation = this.targetLocation.getNext();
}
}
public void reset() {
this.next = this.firstValue;
}
public String getNext() {
LinkedListNode<String> next = new LinkedListNode<>();
next = this.next;
this.next = this.next.getNext();
return next.getHead();
}
public String toString() {
String stTotal = "";
String st = "";
LinkedListNode<String> duplicate = this.firstValue;
for (int i = 0; i < this.listSize; i++) {
st = duplicate.getHead() + ", ";
duplicate = duplicate.getNext();
stTotal = stTotal + st;
}
return stTotal;
}
}