#include
class Node {
public:
int data;
Node* next;
Node(int value) :data(value), next(nullptr) {}
};
class LinkedList
{
private:
Node* head;
public:
LinkedList() :head(nullptr) {}
void appent(int value) {
Node* new_node = new Node(value);
if (!head) {
head = new_node;
return;
}
Node* current = head;
while (current->next)
{
current = current->next;
}
current->next = new_node;
}
void display() {
Node* current = head;
while (current)
{
std::cout << current->data << "->";
current = current->next;
}
std::cout << "nullptr" << std::endl;
}
void remove(int value) {
if (!head) {
return;
}
if (head->data == value) {
Node* temp = head;
head = head->next;
delete temp;
}
Node* current = head;
while (current->next)
{
if (current->data == value) {
Node* temp = current->next;
current->next = current->next->next;
delete temp;
return;
}
current = current->next;
}
}
bool search(int value) {
Node* current = head;
while (current) {
if (current->data == value) {
return true;
}
current = current->next;
}
return false;
}
void clear() {
while (head) {
Node* temp = head;
head = head->next;
delete temp;
}
}
~LinkedList() {
clear();
}
};
int main()
{
LinkedList Mylist;
Mylist.appent(1);
Mylist.appent(2);
Mylist.appent(3);
Mylist.appent(4);
Mylist.display();
Mylist.remove(1);
Mylist.display();
}

- 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