-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircularLL.cpp
More file actions
107 lines (101 loc) · 2.14 KB
/
circularLL.cpp
File metadata and controls
107 lines (101 loc) · 2.14 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
#include <iostream>
using namespace std;
class node{
public:
int data;
node* next;
node(int val){
data=val;
next=NULL;
}
};
class circular {
node* head;
public:
circular() {
head = NULL;
}
void insert(int ins){
node* t = new node(ins);
if(head==NULL){
head=t;
head->next=head;
return;
}
node* temp = head;
while(temp->next!=head){
temp=temp->next;
}
temp->next = t;
t->next =head;
}
void del(int val){
if(head==NULL) return;
if(head->next==head && head->data==val){
delete head;
head=NULL;
return;
}
node* curr=head;
node* prev = NULL;
if(head->data==val){
if(head->next==head){
delete head;
head=NULL;
return;
}
node* last =head;
while(last->next!=head){
last = last->next;
}
last->next = head->next;
node* temp = head;
head=head->next;
delete temp;
return;
}
while(curr->next!=head && curr->data !=val){
prev=curr;
curr =curr->next;
}
if(curr->data==val){
prev->next = curr->next;
delete curr;
}
else{
cout<<"NOt found";
}
}
void display() {
if (head == NULL) {
cout << "List is empty\n";
return;
}
node* temp = head;
while (true) {
cout << temp->data << " ";
temp = temp->next;
if (temp == head)
break;
}
cout << endl;
}
};
int main() {
circular s;
s.insert(10);
s.insert(20);
s.insert(30);
cout << "After inserts: ";
s.display();
s.del(20);
cout << "After deleting 20: ";
s.display();
s.del(10);
cout << "After deleting 10: ";
s.display();
s.del(30);
cout << "After deleting 30: ";
s.display();
return 0;
}