-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyLinkedList.java
156 lines (154 loc) · 3.6 KB
/
MyLinkedList.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
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
public class MyLinkedList{
private int size;
private Node start,end;
public MyLinkedList(){
size = 0;
start = null;
end = null;
}
public int size(){
return size;
}
public boolean add(Integer value){
Node a = new Node(value);
if (size == 0){
start = a;
end = a;
} else {
a.setPrev(end);
this.end.setNext(a);
this.end = a;
}
size += 1;
return true;
}
public String toString(){
Node c = start;
String result = "[";
while (!(c == null)){
result = result + c.getData();
if (!(c.next() == null)){
result = result + ", ";
}
c = c.next();
}
result = result + "]";
return result;
}
private Node getNthNode(int index){
Node c = start;
for (int i = 0; i < this.size; i++){
if (i == index){
return c;
}
c = c.next();
}
return c; //should never happen
}
public Integer get(int index){
if ((size < index + 1) || (index < 0)){
throw new IndexOutOfBoundsException("index must be less than size and nonnegative");
}
return getNthNode(index).getData();
}
public Integer set(int index, Integer value){
if ((index >= size) || (index < 0)){
throw new IndexOutOfBoundsException("index must be less than size and nonnegative");
}
Node c = getNthNode(index);
int n = c.getData();
c.setData(value);
return n;
}
public boolean contains(Integer value){
Node c = start;
while (c != null){
if (c.getData() == value){
return true;
}
c = c.next();
}
return false;
}
public int indexOf(Integer value){
Node c = start;
for (int i = 0; i < this.size - 1; i++){
if (c.getData() == value){
return i;
}
c = c.next();
}
return -1;
}
public void add(int index, Integer value){
if ((index >= size) || (index < 0)){
throw new IndexOutOfBoundsException("index must be less than size and nonnegative");
}
Node n = new Node(value);
if (index == size){
add(value);
} else if (index == 0){
Node a = start;
n.setNext(a);
a.setPrev(n);
start = n;
size++;
} else if (index > 0){
Node a = getNthNode(index);
a.prev().setNext(n);
n.setPrev(a.prev());
a.setPrev(n);
n.setNext(a);
size++;
}
}
public Integer remove(int index){
if ((index >= size) || (index < 0)){
throw new IndexOutOfBoundsException("index must be less than size and nonnegative.");
}
if (index == 0){
Node removed = getNthNode(index);
start = removed.next();
removed.next().setPrev(null);
size--;
return removed.getData();
} else if (index == size - 1){
Node removed = getNthNode(index);
end = removed.prev();
removed.setPrev(null);
size--;
return removed.getData();
} else if (index == 1){
Node removed = getNthNode(index);
start = removed.next();
removed.setNext(null);
size--;
return removed.getData();
} else {
Node removed = getNthNode(index);
Node previous = removed.prev();
Node nextNode = removed.next();
previous.setNext(nextNode);
nextNode.setPrev(previous);
size--;
return removed.getData();
}
}
public boolean remove(Integer value){
if (!(this.contains(value))){
return false;
}
int n = this.indexOf(value);
this.remove(n);
return true;
}
public void extend(MyLinkedList other){
this.end.setNext(other.start);
other.start.setPrev(this.end);
this.size += other.size;
this.end = other.end;
other.start = null;
other.end = null;
other.size = 0;
}
}