forked from MAYANK25402/Hactober-2023-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OperationInLInkedList.cpp
107 lines (90 loc) · 2.23 KB
/
OperationInLInkedList.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
#include <iostream>
struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
// Function to insert a node at the end of the linked list
void insert(Node*& head, int val) {
Node* newNode = new Node(val);
if (!head) {
head = newNode;
return;
}
Node* temp = head;
while (temp->next) {
temp = temp->next;
}
temp->next = newNode;
}
// Function to delete the first occurrence of a node with a given value
void deleteNode(Node*& head, int val) {
if (!head) return;
if (head->data == val) {
Node* temp = head;
head = head->next;
delete temp;
return;
}
Node* temp = head;
while (temp->next && temp->next->data != val) {
temp = temp->next;
}
if (temp->next) {
Node* nodeToDelete = temp->next;
temp->next = temp->next->next;
delete nodeToDelete;
}
}
// Function to search for a node with a given value
bool search(Node* head, int val) {
Node* temp = head;
while (temp) {
if (temp->data == val) {
return true;
}
temp = temp->next;
}
return false;
}
// Function to print the linked list
void print(Node* head) {
Node* temp = head;
while (temp) {
std::cout << temp->data << " ";
temp = temp->next;
}
std::cout << std::endl;
}
// Function to delete the entire linked list to free memory
void deleteLinkedList(Node*& head) {
Node* current = head;
Node* next;
while (current) {
next = current->next;
delete current;
current = next;
}
head = nullptr;
}
int main() {
Node* head = nullptr;
// Insert nodes
insert(head, 1);
insert(head, 2);
insert(head, 3);
insert(head, 4);
std::cout << "Linked List: ";
print(head);
// Search for a node
int searchValue = 3;
std::cout << "Search for " << searchValue << ": " << (search(head, searchValue) ? "Found" : "Not found") << std::endl;
// Delete a node
int deleteValue = 2;
deleteNode(head, deleteValue);
std::cout << "Linked List after deleting " << deleteValue << ": ";
print(head);
// Delete the entire linked list
deleteLinkedList(head);
return 0;
}