-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircularLinkedList.java
More file actions
86 lines (85 loc) · 1.53 KB
/
circularLinkedList.java
File metadata and controls
86 lines (85 loc) · 1.53 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 Node{
int data;
Node next;
Node(int x){
data=x;
next=null;
}
}
class List{
Node root;
List(){
root=null;
}
void add(int x){
if(root!=null){
Node tmp=root;
while(tmp.next!=root){
tmp=tmp.next;
}
tmp.next=new Node(x);
tmp.next.next=root;
//System.out.println("added: "+tmp.next.data);
}
else{
root=new Node(x);
root.next=root;
//System.out.println("added: "+root.data);
}
}
void traverse(){
if(root==null) return ;
Node tmp=root;
do{
System.out.println(tmp.data);
tmp=tmp.next;
}while(tmp!=root);
}
boolean search(int key){
Node tmp=root;
if(root==null) return false;
do{
if(tmp.data==key) return true;
tmp=tmp.next;
}while(tmp!=root);
return false;
}
void del(int key){
if(root==null) return;
Node tmp=root;
boolean f=true;
do{
if(key==tmp.data) {
f=false;
break;
}
tmp=tmp.next;
}while(tmp!=root);
if(f) return;
if(tmp==root){
root=null;
return;
}
Node foo=tmp;
while(foo.next!=tmp) foo=foo.next;
foo.next=tmp.next;
root=foo;
}
}
public class circularLinkedList {
public static void main(String str[]){
List l=new List();
for(int i=0;i<11;i++) l.add(i*10);
l.traverse();
System.out.println(l.search(100));
System.out.println(l.search(400));
l.del(10);
l.add(55);
System.out.println(l.search(55));
System.out.println(l.search(400));
l.traverse();
l.del(55);
System.out.print("last\n");
l.traverse();
}
}