forked from nitind2411/hacktoberfest-2022-DSA-CP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCircularLinkedList.cpp
119 lines (103 loc) · 2.67 KB
/
CircularLinkedList.cpp
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
// C++ in circular linked list
#include <iostream>
using namespace std;
struct Node {
int data;
struct Node* next;
};
struct Node* AddingToEmp(struct Node* block, int data) {
if (block != NULL) return block;
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
block = newNode;
block->next = block;
return block;
}
struct Node* AddingToFront(struct Node* block, int data) {
if (block == NULL) return AddingToEmp(block, data);
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = block->next;
block->next = newNode;
return block;
}
struct Node* AddingToEnd(struct Node* block, int data) {
if (block == NULL) return AddingToEmp(block, data);
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = block->next;
block->next = newNode;
block = newNode;
return block;
}
// inserting a node after specific node
struct Node* AddingAfter(struct Node* block, int data, int item) {
if (block == NULL) return NULL;
struct Node *newNode, *p;
p = block->next;
do {
// if the item is found, place newNode after it
if (p->data == item) {
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = p->next;
p->next = newNode;
// if p is the last node, make newNode as the last node
if (p == block) block = newNode;
return block;
}
p = p->next;
} while (p != block->next);
cout << "\nThe given node is not present in the list" << endl;
return block;
}
// deleting a node
void DelNode(Node** block, int key) {
if (*block == NULL) return;
if ((*block)->data == key && (*block)->next == *block) {
free(*block);
*block = NULL;
return;
}
Node *temp = *block, *d;
// if block is to be deleted
if ((*block)->data == key) {
while (temp->next != *block) temp = temp->next;
temp->next = (*block)->next;
free(*block);
*block = temp->next;
}
while (temp->next != *block && temp->next->data != key) {
temp = temp->next;
}
// if node to be deleted was found
if (temp->next->data == key) {
d = temp->next;
temp->next = d->next;
free(d);
}
}
void Traversing(struct Node* block) {
struct Node* p;
if (block == NULL) {
cout << "The list is empty" << endl;
return;
}
p = block->next;
do {
cout << p->data << " ";
p = p->next;
} while (p != block->next);
}
int main() {
struct Node* block = NULL;
block = AddingToEmp(block, 7);
block = AddingToEnd(block, 9);
block = AddingToFront(block, 3);
block = AddingAfter(block, 17, 3);
Traversing(block);
DelNode(&block, 9);
cout << endl;
Traversing(block);
return 0;
}