public class Doublylinklist {private class data{private Object obj;
Private Data left = null;
Private Data right = NULL;
Data (Object obj) {this.obj = obj;
} private Data a = null;
Private Data last = null; public void Insertfirst (Object obj) {Data data = new data (obj);//if (A/= NULL) {//I = data;//last = da
Ta
}else{//Data.right = A///first.left = data;///////Refactor code if (A/= null) {
last = data;
}else{data.right = A;
First.left = data;
} a = data; public void Insertlast (Object obj) {Data data = new data (obj);//if (A/= NULL) {//I = data;//Last
= data; }else{//last.right = data;//Data.left = last;//last = data;//}//Refactor code if (A/= null) {A
= data;
}else{last.right = data;
Data.left = Last;
last = data; public boolean InsertAfter (Object target,object obj) {Data data = new DatA (obj);
Data cur = i;
while (cur!= null) {if (Cur.obj.equals (target)) {data.right = Cur.right;
Data.left = cur;
if (cur = last) is last = data;
else Cur.right.left = data;
Cur.right = data;
return true;
} cur = cur.right;
return false;
The public Object Deletefirst () throws exception{if (A/= null) throw new Exception ("empty!");
Data temp = i;
if (first.right = = null) {a = null;
last = null;
}else{first.right.left = null;
i = first.right;
return to temp;
The public Object Deletelast () throws exception{if (A/= null) throw new Exception ("empty!");
Data temp = last;
if (first.right = = null) {a = null;
last = null;
}else{last.left.right = null;
last = Last.left;
return to temp;
The public object Delete (Object obj) throws exception{if (A/= null) throw new Exception ("empty!");
Data cur = i;
while (cur!= null) {if (cur.obj.equals (obj)) { if (cur = last) last = Cur.left;
else Cur.right.left = Cur.left;
if (cur = = cur.right;
else cur.left.right = cur.right;
return obj;
} cur = cur.right;
return null;
public void display () {System.out.print ("I-> Last:");
Data data = A;
while (data!= null) {System.out.print (data.obj.toString () + "->");
data = Data.right;
} System.out.print ("\ n");
public static void Main (string[] args) throws exception{doublylinklist dll = new doublylinklist ();
Dll.insertfirst (1);
Dll.insertlast (3);
Dll.insertafter (1, 2);
Dll.insertafter (3, 4);
Dll.insertafter (4, 5);
Dll.display ();
Dll.deletefirst ();
Dll.display ();
Dll.deletelast ();
Dll.display ();
Dll.delete (3);
Dll.display ();
Dll.delete (2);
Dll.display ();
Dll.delete (4);
Dll.display ();
}
}
-> last:1-> 2-> 3-> 4-> 5-> a
-> last:2-> 3-> 4-> 5->
firs T-> last:2-> 3-> 4-> a-> last:2
-> 4-> a-> last:4->