-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03_CircularLinkedList.cpp
More file actions
62 lines (62 loc) · 1.32 KB
/
03_CircularLinkedList.cpp
File metadata and controls
62 lines (62 loc) · 1.32 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
#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define ll long long
class node {
public:
int data;
node *next = NULL;
node(int data) {
this->data = data;
this->next = NULL;
}
};
void insertNode(node *&head, int data, int value) {
if (head == NULL) {
node *temp = new node(data);
head = temp;
head->next = temp;
return;
}
node *temp = head;
node *add = new node(data);
while (temp->data != value) {
temp = temp->next;
}
add->next = temp->next;
temp->next = add;
}
void deleteValue(node *&head, int value) {
node *temp = head, *dlt;
while (temp->next->data != value) {
temp = temp->next;
}
dlt = temp->next;
if (dlt == head) {
head = dlt->next;
}
temp->next = dlt->next;
dlt->next = NULL;
delete dlt;
}
void printcircular(node *&head) {
node *temp = head;
do {
cout << temp->data << " ";
temp = temp->next;
} while (temp != head);
cout << endl;
}
int main() {
node *head = NULL;
insertNode(head, 1, 1);
insertNode(head, 3, 1);
insertNode(head, 7, 3);
insertNode(head, 5, 3);
printcircular(head);
cout << head->data << endl;
deleteValue(head, 1);
printcircular(head);
cout << head->data << endl;
return 0;
}