-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCLL.java
More file actions
143 lines (128 loc) · 3.79 KB
/
Copy pathCLL.java
File metadata and controls
143 lines (128 loc) · 3.79 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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
class node{ //create node
private node link;
private int data;
public node(){
this.link=null;
this.data=0;
}
public node(node link, int data) {
this.link = link;
this.data = data;
}
public node getLink() {
return link;
}
public void setLink(node link) {
this.link = link;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
}
public class CLL{ //creating circular linked list
private node front; //two pointers front and rear
private node rear;
private int size; //counter for traversing
public CLL(){ //non parameterized default constructor
this.front=null;
this.rear=null;
this.size=0;
}
public void insert(int data,int position){ //insert at position function
if(position==1) { //insert at first position
node tmp = new node(front, data); //create temporary node
if (front == null) { //check if list is empty
front = tmp;
tmp.setLink(front);
rear = front;
} else {
rear.setLink(tmp);
front = tmp;
}
size++;
}
else if(position>1 && position<size){ //check is entered position is valid or not
node tmp=new node(null,data); //temporary node
node p=front; //node pointing to front
while(position!=1){ //traverse untill required node found
p=p.getLink();
position--;
}
node q=p.getLink(); //next node of p
p.setLink(tmp);
tmp.setLink(q);
size++;
}
else if(position==size+1){ //insert at last
node tmp=new node(null,data);
rear.setLink(tmp);
tmp.setLink(front);
rear=tmp;
size++;
}
else{
System.out.println("Enter Valid position");
}
}
public void delete(int data){ //delete data
int counter=size;
node p=front; //two pointers p and q for delete operation
node q=front; //one is always next to other
q=q.getLink();
if(size==0){
System.out.println("List is Empty");
return;
}
while(counter!=0){
if(q.getData()==data)
break;
p=p.getLink();
q=q.getLink();
counter--;
}
if(counter==0){
System.out.println("Entered Data Not Exists in List");
}
else {
if(size==1){
front=null;
rear=null;
size=0;
}
else if (front.getData() == data) {
front = front.getLink();
rear.setLink(front);
size--;
} else if (rear.getData() == data) {
p.setLink(front);
rear = p;
size--;
} else {
node tmp = q.getLink();
p.setLink(tmp);
size--;
}
}
}
public void display(){ //traversing circular linked list
int counter=size;
if(size==0)
{
front=null;
rear=null;
size=0;
System.out.println("List Is Empty");
}
else{
node p=front;
while(counter!=-1){
System.out.print(p.getData()+" - >");
p=p.getLink();
counter--;
}
}
}
}