-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoublyLinkedList.java
More file actions
217 lines (151 loc) · 4.64 KB
/
DoublyLinkedList.java
File metadata and controls
217 lines (151 loc) · 4.64 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
class DllNode{
private int data;
private DllNode prev;
private DllNode next;
public DllNode(int data){
this.data = data;
prev = null;
next = null;
}
public DllNode(int data , DllNode prev,DllNode next){
this.data = data;
this.prev = prev;
this.next = next;
}
public int getData(){
return data;
}
public DllNode getNext(){
return next;
}
public DllNode getPrev(){
return prev;
}
public void setPrev(DllNode node){
prev = node;
}
public void setNext(DllNode node){
next = node;
}
}
public class DoublyLinkedList{
private DllNode head;
private DllNode tail;
private int length;
public DoublyLinkedList(){
head = new DllNode(Integer.MIN_VALUE,null,null);
tail = new DllNode(Integer.MIN_VALUE,head,null);
head.setNext(tail);
length = 0;
}
public int getPosition(int data){
DllNode temp = head;
int pos = 0;
while(temp != null){
if(temp.getData() == data){
return pos;
}
pos+=1;
temp = temp.getNext();
}
return Integer.MIN_VALUE;
}
public int getLength(){
return length;
}
public void insertAtHead(int value){
DllNode node = new DllNode(value,null,null);
if(length == 0){
head.setNext(node);
node.setPrev(head);
tail.setPrev(node);
node.setNext(tail);
length += 1;
return;
}
else{
node.setNext(head.getNext());
node.setPrev(head);
head.setNext(node);
length += 1;
}
}
public void insertAt(int data , int position){
if(position < 0){
position = 0;
}
if(position > length){
position = length;
}
DllNode node , temp = head;
if(head == null){
head = new DllNode(data);
tail.setPrev(head);
head.setNext(tail);
}
//when want to insert at head
if(position == 0){
insertAtHead(data);
}
else if(position == length){
insertAtTail(data);
}
else{
for(int i = 1 ; i <= position ; i++){
temp = temp.getNext();
}
//we have reached a position before now.
node = new DllNode(data,null,null);
node.setNext(temp.getNext());
node.setPrev(temp);
temp.getNext().setPrev(node);
temp.setNext(node);
length += 1;
}
}
public void insertAtTail(int data){
DllNode node = new DllNode(data,null,null);
if(length == 0){
insertAtHead(data);
return;
}else{
tail.getPrev().setNext(node);
node.setPrev(tail.getPrev());
tail.setPrev(node);
length += 1;
}
}
public int removeFirst(){return 0;}
public int removeLast(){return 0;}
public synchronized void removeMatched(DllNode node){}
public String toString(){
String result = "[";
DllNode node = head.getNext();
for(int i = 0 ; i < length ; i++){
result += node.getData();
if( i != (length - 1)){
result += ",";
}
node = node.getNext();
}
result += "]";
return result;
}
public static void main(String[] args){
DoublyLinkedList dll = new DoublyLinkedList();
dll.insertAtHead(5);
System.out.println(dll.toString());
System.out.println("list Length " + dll.getLength());
dll.insertAtHead(15);
System.out.println(dll.toString());
System.out.println("list Length " + dll.getLength());
dll.insertAtTail(25);
System.out.println(dll.toString());
dll.insertAt(35,1);
System.out.println(dll.toString());
dll.insertAt(45,dll.getLength());
System.out.println(dll.toString());
dll.insertAt(65,dll.getLength() - 1);
System.out.println(dll.toString());
}
}