-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircular_linked_list.cpp
More file actions
111 lines (92 loc) · 2.05 KB
/
circular_linked_list.cpp
File metadata and controls
111 lines (92 loc) · 2.05 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
#include <iostream>
using namespace std;
// we do not use head in this case, we deal with tail
class node
{
public:
int data;
node *next;
node(int data)
{
this->data = data;
this->next = next;
}
};
// isnert data after the given element in linked list
void insert(node *&tail, int element, int value)
{
// when circular linked list is empty
if (tail == NULL)
{
node *temp = new node(value);
tail = temp;
temp->next = temp;
}
// non empty list
// assuming that the element is present in the circular linked list
node *current = tail;
while (current->data != element)
{
current = current->next;
}
// if element is found, than current is representing current wala element
node *temp = new node(value);
if (temp->next = temp)
{
node *forward = current->next;
current->next = temp;
temp->next = forward;
}
else
{
temp->next = current->next;
current->next = temp;
}
}
void deletion(node* &tail, int value){
//empty list
if(tail == NULL){
cout<<"list is empty, please check again "<<endl;
return;
}
else{
//non empty list , assuming that the value is present in the list
node* back = tail;
node* current = back->next;
while(current->data != value){
back = current;
current = current->next;
}
if(current == back){
tail = NULL;
return;
}
if(tail == current){
tail = back;
return;
}
back->next = current->next;
current->next = NULL;
delete current;
}
}
void print(node* &tail)
{
node *temp = tail;
do
{
cout << tail->data << " ";
tail = tail->next;
} while (tail != temp);
cout << endl;
}
int main()
{
node *tail = new node(8);
cout << tail->data;
insert(tail, 8, 9);
print(tail);
insert(tail, 8, 15);
print(tail);
return 0;
}