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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
| #include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <locale.h>
#include <string.h>
struct Node {
char snp[256];
char pos[256];
char place[256];
struct Node *next;
};
typedef struct Node *PNode;
PNode Head = NULL;
PNode fAddNode(const char* snp, const char* pos, const char* place)
{
PNode NewNode = (PNode)malloc(sizeof(struct Node));
strcpy(NewNode->snp, snp);
strcpy(NewNode->pos, pos);
strcpy(NewNode->place, place);
NewNode->next = Head == NULL ? NewNode : Head->next;
if (Head) Head->next = NewNode;
Head = NewNode;
}
PNode getNum(int n) {
int count = 1;
PNode head = Head;
PNode _Head = Head;
if(n != 1)
do
{
count++;
_Head = _Head->next;
} while(count < n && _Head != head);
return _Head;
}
PNode getLast() {
PNode head = Head;
do
{
head = head->next;
} while(head->next != Head);
return head;
}
int nNode()
{
if(Head == NULL)
{
return 0;
}
int n = 0;
PNode head = Head;
do
{
n++;
Head = Head->next;
} while(Head != head);
return(n);
}
PNode AddNode(const char* snp, const char* pos, const char* place, int n)
{
int num = nNode(Head);
if(Head == NULL)
{
fAddNode(snp,pos,place);
return 0;
}
PNode NewNode = (PNode)malloc(sizeof(struct Node));
strcpy(NewNode->snp, snp);
strcpy(NewNode->pos, pos);
strcpy(NewNode->place, place);
if(n == 1)
{
NewNode->next = Head;
PNode _np = getLast(Head);
_np->next = NewNode;
Head = NewNode;
}
else if(n <= num)
{
PNode np = getNum(n);
PNode _np = getNum(n-1);
NewNode->next = np;
_np->next = NewNode;
}
else
{
PNode ln = getLast();
NewNode->next = ln->next;
ln->next = NewNode;
}
return NewNode;
puts("Successful");
}
PNode searchInList(char* str)
{
PNode head = Head;
PNode p = NULL;
do
{
printf("%s", Head->snp); //Для теста
if(str == Head->snp)
{
puts("Test1"); //Для теста
p = Head;
}
Head = Head->next;
}while(Head->next != head);
return(p);
}
int addNodeAfterName(const char* name, const char* snp, const char* pos, const char* place)
{
if(Head == NULL)
{
puts("List empty");
return 0;
}
PNode p = searchInList(name);
if(p == NULL)
{
printf("%s not found\n", name);
return 0;
}
PNode NewNode = (PNode)malloc(sizeof(struct Node));
strcpy(NewNode->snp, snp);
strcpy(NewNode->pos, pos);
strcpy(NewNode->place, place);
NewNode->next = p->next;
p->next = NewNode;
}
void fReadList()
{
FILE *in;
char s[256];
char n[128];
char p[128];
char pos[256];
char place[256];
int i = 1;
in=fopen("list.txt", "r");
if (in == NULL)
{
puts("Error. file not found");
}
else
{
while(!feof(in))
{
fscanf(in, "%s", s);
fscanf(in, "%s", n);
fscanf(in, "%s\n", p);
fscanf(in, "%s ", pos);
fscanf(in, "%s", place);
strcat(s, " ");
strcat(s, n);
strcat(s, " ");
strcat(s, p);
AddNode(s, pos, place, i);
i++;
}
close(in);
}
puts("Reading from the file was successful");
}
int fWriteList()
{
FILE *out;
if(Head == NULL)
{
puts("List empty");
return 0;
}
out = fopen("list.txt", "w");
PNode head = Head;
PNode _head = getLast();
do
{
fprintf(out, "%s %s %s", Head->snp, Head->pos, Head->place);
if(Head != _head) fprintf(out, "\n");
Head = Head->next;
} while(Head != head);
puts("The writing to the file was successful");
close(out);
}
int printList()
{
if(Head == NULL)
{
puts("List empty");
return 0;
}
int i = 1;
PNode head = Head;
puts(" _____________________________________________________________________________");
puts("| | | | |");
puts("|N| Name | Position | Place |");
puts("|_|________________________________________|______________|___________________|");
do
{
printf("|%-1d|%-40s|%-14s|%-19s|\n", i, Head->snp, Head->pos, Head->place);
puts("|_|________________________________________|______________|___________________|");
i++;
Head = Head->next;
} while(Head != head);
}
int clearList()
{
if(Head == NULL)
{
puts("List empty");
return 0;
}
PNode head = Head;
do
{
strcpy(Head->snp, "0");
strcpy(Head->pos, "0");
strcpy(Head->place, "0");
Head = Head->next;
} while(Head != head);
puts("List successfully cleared");
}
int freeList()
{
if(Head == NULL)
{
puts("List empty");
return 0;
}
PNode mainHead = getLast();
PNode _Head = NULL;
while(Head != mainHead)
{
_Head = Head;
Head = Head->next;
free(_Head);
}
free(mainHead);
}
int main()
{
int num;
char val=' ';
char snp[256];
char pos[256];
char place[256];
for(;;)
{
while(val != '1' && val != '2' && val != '3' && val != '4' && val != '5' && val != '6' && val != '0')
{
puts(" ----------Menu-------------------");
puts("| 1. Add a new item by number |");
puts("| 2. Print list |");
puts("| 3. Clear list (=0) |");
puts("| 4. Read from file |");
puts("| 5. Write to file |");
puts("| 6. Add a new item after somebody|");
puts("| 0. Exit |");
puts(" ---------------------------------");
scanf("%s", &val);
}
if(val == '1')
{
printf("Surname Name Patronymic\n");
fflush(stdin);
gets(snp);
printf("Position\n");
fflush(stdin);
gets(pos);
printf("Place\n");
fflush(stdin);
gets(place);
printf("Number\n");
scanf("%d", &num);
AddNode(snp, pos, place, num);
val = ' ';
}
if(val == '2')
{
printList();
val = ' ';
}
if(val == '3')
{
clearList();
val = ' ';
}
if(val == '4')
{
if(Head != NULL)
{
freeList();
Head = NULL;
}
fReadList();
val = ' ';
}
if(val == '5')
{
fWriteList();
val = ' ';
}
if(val == '6')
{
char str[256];
printf("After whom we will add? Enter the Surname Name Patronymic\n");
fflush(stdin);
gets(str);
PNode p = searchInList(str);
if(p == NULL) puts("Person not found"); //Для теста
else puts("OK"); //Для теста
val = ' ';
}
if(val == '0') break;
}
freeList();
Head = NULL;
} |