-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoubly LL.java
More file actions
86 lines (82 loc) · 2.16 KB
/
Doubly LL.java
File metadata and controls
86 lines (82 loc) · 2.16 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
class doublyLL{
public static class Node{
int data;
Node next;
Node prev;
Node(int data){
this.data=data;
}
}
public static void display(Node head){
Node t=head;
while(t!=null){
System.out.print(t.data+" ");
t=t.next; // 4->5->7
}
System.out.println();
}
public static void display2(Node tail){
Node t=tail;
while(t!=null){
System.out.print(t.data+" ");
t=t.prev; // 7->5->4
}
System.out.println();
}
public static void displayr(Node random){
Node t=random;
while(t.prev!=null){
t=t.prev;
}
while(t!=null){
System.out.print(t.data+" ");
t=t.next;
}
System.out.println();
}
public static Node inserth(Node head,int data){ // 4 5 7
Node p=new Node(6);
p.next=head;
head.prev=p; // 6 4 5 7
head=p;
return head;
}
public static void insertt(Node head,int data){
Node t=head;
while(t.next!=null){
t=t.next;
}
Node p=new Node(9); // 6 4 5 7 9
t.next=p;
p.prev=t;
}
public static void insert(Node head,int id,int data){
Node s=head;
for(int i=1;i<=id-1;i++){
s=s.next;
}
Node r=s.next;
Node p=new Node(8);
s.next=p;
p.prev=s;
p.next=r; // 6 4 5 8 7 9
r.prev=p;
}
public static void main(String[] args) {
Node x=new Node(4);
Node y=new Node(5);
Node z=new Node(7);
x.next=y;
y.prev=x;
y.next=z;
z.prev=y;
display(x);
display2(z);
displayr(y);
Node newhead=inserth(x,6);
insertt(x,8);
insert(x,3,8);
display(x);
display(newhead);
}
}