/
MyLinkedList.java
164 lines (147 loc) · 4.16 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
157
158
159
160
161
162
163
164
/**
* Singly Linked List
* Created by Devang on 03-Jan-17.
*/
public class MyLinkedList<E> {
Node<E> head;
Node<E> tail;
int size;
public MyLinkedList() {
head = null;
tail = null;
size = 0;
}
public void addLast(E data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
tail = head;
} else {
tail.next = newNode;
tail = newNode;
}
size++;
}
public void addFirst(E data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
tail = head;
} else {
newNode.next = head;
head = newNode;
}
size++;
}
public void remove(E key) {
if (head == null) {
return;
}
if (head.data.equals(key)) {
head = head.next;
if(head.next == null){
tail = null;
}
} else {
Node currentNode = head;
while (currentNode.next != null){
if (currentNode.next.data.equals(key)) {
currentNode.next = currentNode.next.next;
if(currentNode.next == null){
tail = currentNode;
}
break;
}
currentNode = currentNode.next;
}
}
size--;
}
public void insertAfter(E key, E data){
if (head == null) {
return;
}
Node newNode = new Node(data);
Node currentNode = head;
while (currentNode != null){
if(currentNode.data.equals(key)){
newNode.next = currentNode.next;
currentNode.next = newNode;
if(newNode.next == null){
tail = newNode;
}
size++;
return;
}
currentNode = currentNode.next;
}
}
public void insertBefore(E key, E data){
if (head == null) {
return;
}
//If inserting before head
if(head.data.equals(key)){
addFirst(data);
size++;
return;
}
Node newNode = new Node(data);
Node currentNode = head;
//Else iterate till end
while (currentNode.next != null){
if(currentNode.next.data.equals(key)){
newNode.next = currentNode.next;
currentNode.next = newNode;
size++;
return;
}
currentNode = currentNode.next;
}
}
public String toString() {
Node currentItem = head;
String output = "[";
int cnt = 0;
while (currentItem.next != null) {
if (cnt != 0)
output += ", ";
output += currentItem.data;
cnt++;
currentItem = currentItem.next;
}
if (cnt != 0)
output += ", ";
output += currentItem.data;
output += "]";
return output;
}
protected class Node<E> {
protected E data;
protected Node<E> next;
protected Node(E data) {
this.data = data;
}
}
public static void main(String[] args) {
MyLinkedList list1 = new MyLinkedList();
list1.addLast("Hello");
list1.addLast("World");
list1.addLast("!");
System.out.println(list1);
System.out.println("tail: " + list1.tail.data);
list1.remove("!");
System.out.println("tail: " + list1.tail.data);
System.out.println(list1);
list1.addFirst("!");
System.out.println(list1);
System.out.println("Size: " + list1.size);
list1.insertAfter("Hello", "Everyone");
System.out.println(list1);
list1.insertBefore("Hello", 125);
list1.remove("World");
System.out.println(list1);
System.out.println("tail: " + list1.tail.data);
System.out.println("Size: " + list1.size);
}
}