-
Notifications
You must be signed in to change notification settings - Fork 0
/
circular.java
115 lines (103 loc) · 3.11 KB
/
circular.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
public class circular {
class solution{
static class Node{
int data;
Node next;
}
static Node addToEmpty(Node last , int data){
if(last!=null){
return last;
}
Node newNode = new Node();
newNode.data = data;
last = newNode;
newNode.next = last;
return last;
}
static Node addToFront(Node last, int data){
if(last==null){
return addToEmpty(last, data);
}
Node newNode = new Node();
newNode.data = data;
newNode.next = last.next;
last.next = newNode;
return last;
}
static Node addEnd(Node last,int data){
if(last==null){
return addToEmpty(last, data);
}
Node newNode = new Node();
newNode.data = data;
newNode.next = last.next;
last.next = newNode;
last = newNode;
return last;
}
static Node addafter(Node last,int item,int data){
if(last == null){
return null;
}
Node newNode, p;
p = last.next;
do{
if(p.data == item){
newNode = new Node();
newNode.data = data;
newNode.next = p.next;
p.next = newNode;
if(p==last){
last =newNode;
}
return last;
}
p =p.next;
}while(p!=last.next);
System.out.println(item+ "the gnis not present");
return last;
}
static Node deleteNode(Node last,int key){
if(last == null){
return null;
}
if(last.data == key&&last.next == last){
last = null;
return last;
}
Node temp = last,d=new Node();
if(last.data == key){
while(temp.next!=last){
temp= temp.next;
}
}
while(temp.next!=last&&temp.next.data !=key){
last = temp.next;
}
if(temp.next.data == key){
d = temp.next;
temp.next = d.next;
}return last;
}
static void printcll(Node last) {
Node p;
if (last == null) {
System.out.println("List is empty.");
return;
}
p = last.next;
do {System.out.print(p.data + " ");
p = p.next;
}while (p != last.next);
}
public static void main(String[] args) {
Node last = null;
last = addToEmpty(last, 6);
last = addEnd(last, 8);
last = addToFront(last, 2);
last = addafter(last, 10, 2);
printcll(last);
deleteNode(last, 8);
printcll(last);}
}
}