-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdLL.java
More file actions
67 lines (64 loc) · 1.38 KB
/
dLL.java
File metadata and controls
67 lines (64 loc) · 1.38 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
public class dLL {
class Node{
int data;
Node left;
Node right;
Node(int data){
this.data=data;
}
}
private Node head;
public void add(int data){
Node New=new Node(data);
if(head==null){
head=New;
return;
}
Node temp=head;
while(temp.right!=null){
temp=temp.right;
}
temp.right=New;
New.left=temp;
}
public void delete(int data){
Node temp=head;
Node prev=head;
if(temp.data==data){
head=temp.right;
}
while(temp!=null && temp.data!=data){
prev=temp;
temp=temp.right;
}
if(temp==null){
System.out.println("not found");
return;
}
Node ptr=temp.right;
prev.right=ptr;
ptr.left=prev;
}
public void traverse(){
Node temp=head;
while(temp!=null){
System.out.print(temp.data+"-> ");
temp=temp.right;
}
System.out.print("NULL\n");
}
public static void main(String[] args) {
dLL l=new dLL();
l.add(3);
l.add(5);
l.add(7);
l.add(9);
l.add(0);
l.traverse();
l.delete(7);
l.traverse();
}
}
// o/p,
// 3-> 5-> 7-> 9-> 0-> NULL
// 3-> 5-> 9-> 0-> NULL