-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCList.cpp
More file actions
79 lines (74 loc) · 1.28 KB
/
Copy pathCList.cpp
File metadata and controls
79 lines (74 loc) · 1.28 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
#include <iostream>
using namespace std;
template <class T>
class CList
{
private:
struct CItem
{
T value;
CItem* next;
};
CItem* head; // Корень списка
public:
CList(): head(nullptr) {} // Инициализация списка
CList(T* t, int n) // Инициализация списка по n элементам массива
{
// добавить проверку n
CItem* p = new(CItem);
head = p;
p->value = t[0];
for(int i=1; i<n; i++)
{
p->next = new(CItem);
p = p->next;
p->value = t[i];
}
p->next = nullptr;
}
~CList()
{
CItem* p;
while (head)
{
p = head;
head = head->next;
delete p;
}
}
void insFirst(T t) // Вставка элемента в начало списка
{
CItem* p = new(CItem);
p->value = t;
p->next = head;
head = p;
}
void print()
{
CItem* p = head;
if (!p)
{
cout << "Список пуст!\n";
return;
}
while(p)
{
cout << p->value << '\t';
p = p->next;
}
}
};
int main()
{
system("chcp 1251");
int a[] = {9,7,6,4,3,2,1,20};
CList<int> L1;
CList<int> L2(a,5);
L1.print();
L1.insFirst(5);
L1.insFirst(8);
L1.insFirst(7);
L1.print();
cout << "\n";
L2.print();
}