-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem0.cpp
More file actions
101 lines (85 loc) · 2.09 KB
/
Problem0.cpp
File metadata and controls
101 lines (85 loc) · 2.09 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
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int value) {
data = value;
next = nullptr;
}
};
class LinkedList {
private:
Node* head;
public:
LinkedList() {
head = nullptr;
}
// Insertion
void insert(int value) {
Node* newNode = new Node(value);
newNode->next = head;
head = newNode;
}
// Traversal and display o
void display() {
Node* current = head;
while (current != nullptr) {
cout << current->data << " ";
current = current->next;
}
cout << endl;
}
// Searching
bool search(int value) {
Node* current = head;
while (current != nullptr) {
if (current->data == value) {
return true;
}
current = current->next;
}
return false;
}
// Deletion
void remove(int value) {
if (head == nullptr) {
return;
}
if (head->data == value) {
Node* temp = head;
head = head->next;
delete temp;
return;
}
Node* current = head;
while (current->next != nullptr) {
if (current->next->data == value) {
Node* temp = current->next;
current->next = temp->next;
delete temp;
return;
}
current = current->next;
}
}
};
int main() {
LinkedList list;
list.insert(5);
list.insert(10);
list.insert(15);
cout << "Linked List: ";
list.display();
int searchValue = 15;
if (list.search(searchValue)) {
cout << searchValue << " found in the linked list." << endl;
} else {
cout << searchValue << " not found in the linked list." << endl;
}
int deleteValue = 10;
list.remove(deleteValue);
cout << "Linked List after deleting " << deleteValue << ": ";
list.display();
}