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
| #include <conio.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct person { //описание структуры
char f[30]; //фамилия
char d[30]; //должность
float zp,pr; //зарплата, премия
char prof; //профсоюз
person *next; //указатель на следующего в списке
};
person vvesti() { //ввод элемента с клавиатуры
person p;
printf ("\nФИО?"); fflush (stdin); scanf ("%s",p.f);
printf ("Должность?"); fflush (stdin); scanf ("%s",p.d);
printf ("Зарплата?"); fflush (stdin); scanf ("%f",&p.zp);
printf ("Премия"); fflush (stdin); scanf ("%f",&p.pr);
printf ("Член профоюза (+/-)?"); fflush (stdin); scanf ("%c",&p.prof);
return p;
}
int show (person *head) { //показ всего списка с определением количества элементов
int count=0;
if (head) while (1) {
printf ("\nФИО: %s Должность: %s Зарплата: %.2f. Премия: %.2f. Профсоюз: %c.", head->f,head->d,head->zp,head->pr,head->prof);
count++;
printf("|||Номер записи %d",count);
if (head->next == NULL) break;
head = head->next;
}
printf ("\nВ списке всего %d записей (звений)\n",count);
return count;
}
void Search (person *head, char *st) { //поиск в списке строки st
if (head==NULL) return;
person *next = head;
do {
char *find = strstr (next->f, st);
if (find!=NULL) printf ("\n%s",next->f);
if (next->next==NULL) break;
next = next->next;
} while (1);
}
void copy1 (person *to, person *from) { //копирование одной записи
strcpy (to->f, from->f);
strcpy (to->d, from->d);
to->zp = from->zp; to->pr = from->pr; to->prof = from->prof;
}
person *add1 (person *head, person *st) { //добавление элемента st в начало списка
//*st уже заполнен данными
person *current = new person;
copy1 (current, st); //копирует 1 запись
if (head==NULL) current->next = NULL;
else {
current->next = head;
head = current;
}
return current;
}
person *add2 (person *head, person *st) { //добавление элемента st в конец списка
person *last = NULL;
if (head!=NULL) {
last=head;
while (last->next!=NULL) last=last->next;
}
person *current = new person;
copy1 (current,st);
current->next = NULL;
if (last) last->next = current;
return current;
}
person *delete1 (person *head0, int n) { //удаление элемента по номеру 1..N
// удалит элемент номер n и вернет указ.на нач.
person *head = head0;
if (head==NULL) return NULL;
if (n==1) { //удаляем первый
person *ptr = head->next;
delete head;
return ptr;
}
person *prev = NULL, *start = head; int i=1;
while (i<n) { //Ищем n-ый элемент
prev = head; head = head->next;
if (head==NULL) return start;
i++;
}
person *ptr = head->next;
delete head;
prev->next = ptr;
return start;
}
person *sort (person *ph) { //сортировка списка методом вставок
person *q, *p, *pr, *out=NULL;
while (ph != NULL) {
q = ph; ph = ph->next; //исключить эл-т
//ищем, куда его включить:
for (p=out,pr=NULL ; p!=NULL &&
strcmp(q->f,p->f)>0; pr=p,p=p->next) ;
//или ваш критерий, когда переставлять!
if (pr==NULL) { q->next=out;out=q; } //в нач.
else { q->next=p; pr->next=q; } //после пред.
}
return out;
}
int main() { //меню и главная программа
person *head = NULL;
system("chcp 1251");
while (1) {
printf ( "\n 1. Показать все (Show all)"
"\n 2. Добавить в начало (Add in head)"
"\n 3. Добавить в конец (Add in tail)"
"\n 4. Удалить по номеру (Delete by number)"
"\n 5. Сортировать по имени (Sort by name)"
"\n 6. Найти по содержимому"
"\n 0. Закончить (Exit)\n ");
fflush (stdin);
char c;
scanf ("%c",&c);
person p;
person *cur;
int n, all;
switch (c) {
case '1':
show (head);
break;
case '2':
p = vvesti();
head=add1(head,&p);
break;
case '3':
p = vvesti();
cur = add2 (head,&p);
if (head==NULL) head=cur;
break;
case '4':
all = show(head);
while (1) {
printf ("\nВведите номер удаляемой записи (1-%d): ",all);
fflush (stdin); scanf("%d",&n);
if (n>=1 && n<=all) break;
}
head = delete1 (head,n);
break;
case '5':
head = sort (head);
break;
case '6' :
Search(head,&p);
break;
case '0':
exit (0);
break;
}
}
} |