-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpa_lab_5_3_13_(4)-B.cpp
83 lines (68 loc) · 1.3 KB
/
cpa_lab_5_3_13_(4)-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
#include <iostream>
using namespace std;
class Node
{
public:
Node(int val);
int value;
Node* next;
};
Node::Node(int val) : value(val), next(nullptr){}
class List{
public:
List();
void push_front(int value);
bool pop_front(int &value);
void push_back(int value);
int size();
private:
Node* head;
Node* tail;
};
List::List() : head(nullptr), tail(nullptr){}
void List::push_front(int value)
{
Node* new_head = new Node(value);
new_head->next = head;
if(head == nullptr)
tail = new_head;
head=new_head;
}
bool List::pop_front(int &value)
{
if(head == nullptr)
return false;
value = (*head).value;
head = (*head).next;
return true;
}
void List::push_back(int value)
{
if(head != nullptr)
{
Node* new_tail = new Node(value);
tail->next = new_tail;
tail = new_tail;
}
else push_front(value);
}
int List::size()
{
int i = 0;
for(Node* iter = head; iter != nullptr; iter = iter->next, ++i);
return i;
}
int main()
{
List list;
list.push_back(1);
list.push_back(2);
list.push_back(3);
list.push_back(4);
int value = 0;
while (list.pop_front(value))
{
cout << value << endl;
}
return 0;
}