-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathList.cpp
More file actions
122 lines (103 loc) · 4.25 KB
/
Copy pathList.cpp
File metadata and controls
122 lines (103 loc) · 4.25 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include "List.h"
ListNode* ListNode::FoundPtr(ListNode *l, int index) {
for (int i = 0; i < index; i++) {
l = l->next;
}
return l;
}
int ListNode::FoundInd(ListNode *l, ListNode *found) {
int res_index = 0;
while (l != found) {
l = l->next;
res_index++;
}
return res_index;
}
ListNode* ListNode::push(ListNode *head, ListNode *l, std::string s, int randIndex) {
//Создаем новый элемент
ListNode *new_l = new ListNode[1];
new_l->data = s;
new_l->prev = l;
new_l->next = NULL;
//Первый параметр (FoundPtr) функции должен быть головой списка, чтобы была возможность найти любой элемент
new_l->rand = ListNode::FoundPtr(head, randIndex);
return new_l;
}
List::List(ListNode *head, ListNode* tail, int count) {
this->head = head;
this->tail = tail;
this->count = count;
}
List::~List() {
//Проходимся с хвоста и удалем весь список
for (int i = 0; i < this->count; i++) {
ListNode *l = this->tail->prev;
delete[] this->tail;
this->tail = l;
}
this->count = 0;
}
void List::Print() {
ListNode *l = this->head;
for (int i = 0; i < this->count; i++) {
std::cout << "Print: " << l->data << " " << l->rand->data << std::endl;
l = l->next;
}
std::cout << "________________________" << std::endl;
}
//Записываем в файл только необходимые параметры: длинну списка(count), будет идти первой в файле
// индекс (ind) рандомного элемента списка
// рамзер (leng) строки data элемента списка
// строку(list->data) элемента списка
void List::Serialize(FILE * file) {// сохранение в файл (файл открыт с помощью fopen(path, "wb"))
//Указатель для прохода по списку
ListNode *l = this->head;
//Записываем переменную длинны списка в файл
fwrite(&(this->count), sizeof(int), 1, file);
int leng;//Используем эту переменную для того, чтобы хранить размер data
//Проходим по списку и записываем его в файл
for (int i = 0; i < this->count; i++) {
//Находим по указателю rand номер элемента в списке и записываем в файл
int ind = ListNode::FoundInd(this->head, l->rand);
fwrite(&ind, sizeof(int), 1, file);
//Высчитываем размер data и записываем его в файл
leng = (l->data).size();
fwrite(&leng, sizeof(int), 1, file);
//Записываем data в виде С строки, то есть строки с нулевым символом, поэтому leng+1
fwrite((l->data).c_str(), sizeof(char), leng + 1, file);
l = l->next;
}
fclose(file);
}
void List::Deserialize(FILE * file) { // загрузка из файла (файл открыт с помощью fopen(path, "rb"))
ListNode *l = new ListNode[1];//Указатель для пробежки по списку
ListNode *head = l;//Голова списка
l->prev = nullptr;
l->next = nullptr;
int count;//Записываем длинну списка из файлп
fread(&count, sizeof(int), 1, file);
for (int i = 0; i < count; i++) {
int leng;//Тут храним длинну data, чтобы можно было её безошибочно считать
int rand_index;//Тут храним индекс рандомного элемента
fread(&rand_index, sizeof(int), 1, file);
fread(&leng, sizeof(int), 1, file);
//Храним промежуточное значение data
char *data = new char[leng + 1];
fread(data, sizeof(char), leng + 1, file);
if (i > 0) {
l->next = ListNode::push(head, l, std::string(data), rand_index);
l = l->next;
}
else {
l->data = std::string(data);
l->rand = l;
}
//Удаляем промежуточное значение
delete[] data;
}
fclose(file);
//Инициализируем параметры
this->head = head;
this->tail = tail;
this->count = count;
}