-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpa_lab_5_3_13_(2)-B.cpp
91 lines (78 loc) · 1.15 KB
/
cpa_lab_5_3_13_(2)-B.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
#include <iostream>
using namespace std;
class Node
{
public:
Node(int val);
int value;
~Node();
Node* next;
};
Node::Node(int val) : value(val), next(nullptr)
{
cout << "+Node" << endl;
}
Node::~Node()
{
cout << "-Node" << endl;
}
class List
{
public:
List();
~List();
void push_front(int value);
bool pop_front(int value);
private:
Node* head;
};
List::List() : head(nullptr) { }
List::~List() {
Node *t = head, *next;
while (t != nullptr)
{
next = t->next;
delete t;
t = next;
}
}
void List::push_front(int value)
{
Node* new_head = new Node(value);
new_head->next = head;
head = new_head;
}
bool List::pop_front(int value)
{
Node *t = head, *prev = nullptr;
while (t != nullptr) {
if (t->value == value) {
if (prev == nullptr) {
prev = head;
head = head->next;
delete prev;
}
else
{
prev->next = t->next;
delete t;
}
return true;
}
else
{
prev = t;
t = t->next;
}
}
return false;
}
int main()
{
List list;
list.push_front(1);
list.push_front(2);
list.push_front(3);
list.push_front(4);
return 0;
}