-
Notifications
You must be signed in to change notification settings - Fork 0
/
Doubly.java
96 lines (75 loc) · 1.45 KB
/
Doubly.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
class Dnode{
Dnode p;
Dlist child;
int data,degree;
boolean mark;
public Dnode next,prev;
Dnode(int data){
this.data = data;
}
}
class Dlist{
public Dnode start=null;
public Dnode end = null;
int size = 0;
public void insert(Dnode nptr)
{
if(start == null)
{
nptr.next= nptr;
nptr.prev = nptr;
start = nptr;
end = start;
}
else{
Dnode x = start.next;
x.prev= nptr;
nptr.next=x;
nptr.prev=start;
start.next = nptr;
}
size ++;
}
public Dnode Delete(Dnode n){
if(size==1 || size==0){
start = null;
end=null;
size=0;
return n;
}
Dnode x = n.next;
Dnode y = n.prev;
x.prev=y;
y.next=x;
start = x;
size--;
return n;
}
public void display()
{
//System.out.print("\nCircular Doubly Linked List = ");
Dnode ptr = start;
if (size == 0)
{
// System.out.print("empty\n");
System.out.print(" ~~~ ");
return;
}
if (start.next == start)
{
System.out.print(start.data+ "<->"+ptr.data+ "");
return;
}
System.out.print(start.data+ "<->");
ptr = start.next;
while(ptr.next != start)
{
System.out.print(ptr.data+ "<->");
ptr = ptr.next;
}
System.out.print(ptr.data+ "<->");
//System.out.print(ptr.data+ "\n");
ptr = ptr.next;
System.out.print(ptr.data);
}
}