-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab#07_Farhanulhaq_Task#3.cpp
More file actions
86 lines (78 loc) · 1.94 KB
/
Copy pathLab#07_Farhanulhaq_Task#3.cpp
File metadata and controls
86 lines (78 loc) · 1.94 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
#include <iostream>
using namespace std;
struct Node
{
int data;
Node* next;
};
void insertAtEnd(Node** head, int data) {
Node* newNode = new Node{data, NULL};
if (*head == NULL) {
*head = newNode;
newNode->next = *head;
return;
}
Node* temp = *head;
while (temp->next != *head) {
temp = temp->next;
}
temp->next = newNode;
newNode->next = *head;
}
void traverseCircularList(Node* head) {
if (head == NULL) {
cout << "List is empty." << endl;
return;
}
Node* temp = head;
do {
cout << temp->data << " ";
temp = temp->next;
} while (temp != head);
cout << endl;
}
void deleteNode(Node** head, int key) {
if (*head == NULL) return;
Node *current = *head, *prev = NULL;
// If the head node holds the key
if ((*head)->data == key) {
// Find the last node to update its next pointer
while (current->next != *head) {
current = current->next;
}
if (current == *head) {
delete *head;
*head = NULL;
return;
}
current->next = (*head)->next;
delete *head;
*head = current->next;
return;
}
prev = *head;
current = (*head)->next;
while (current != *head && current->data != key) {
prev = current;
current = current->next;
}
if(current->data==key) {
prev->next = current->next;
delete current;
} else {
cout<<"Key not found in the list." << endl;
}
}
int main() {
Node* head=NULL;
insertAtEnd(&head,19);
insertAtEnd(&head,74);
insertAtEnd(&head,70);
insertAtEnd(&head,5);
cout << "Circular Linked List: ";
traverseCircularList(head);
deleteNode(&head,74);
cout<<"After Deletion: ";
traverseCircularList(head);
return 0;
}