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
| #include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct List {
char word[100];
int stranica[10];
struct List *next; //ukazatel' na sleduyucshuu zapis'
struct List *prior; //ukazatel' na predyducshyuu zapis'
};
struct List *start; //ukazatel' na pervuyu zapis' spiska
struct List *last; //ukazatel' na poslednyuu zapis'
struct List *find(char *);
void enter(void), search(void), show(void);
void dld_store(struct List *i, struct List **start, struct List **last);
void inputs(char *,char *, int), display(struct List *);
void inputc(char *,int *, int), display(struct List *);
int menu_select(void);
int main(void)
{
start=last=NULL; //inicializaciya ukazatelei na nachalo i konec
for(;;) {
switch(menu_select()) {
case 1: enter(); //vvod slova
break;
case 2: show(); //otobrazhenie spiska
break;
case 3: search(); //poisk slova
break;
case 4: exit(0);
}
}
return 0;
}
// vybor deistviya pol'zovatelya
int menu_select(void)
{
char s[80];
int c;
printf("1. Vvod slova\n");
printf("2. Otobrazhenie spiska\n");
printf("3. Poisk slova\n");
printf("4. Exit\n");
do {
printf(" Vash vybor: ");
gets(s);
c=atoi(s);
} while(c<0 || c>4);
return c;
}
// vvod slov i stranic
void enter(void)
{
struct List *info;
for(;;) {
info=(struct List *)malloc(sizeof(struct List));
if(!info) {
printf("\nNet svobodnoi pamyati");
return;
}
inputs("VVedite slovo: ", info->word, 100);
if(!info->word[0]) break; //zavecshit' vvod
inputc("VVedite stranicy: ",info->stranica,10);
// dls_store(info, &start, &last);
} //cikl vvoda
}
//sozdanie dvusvyaznogo spiska
void dls_store(
struct List *i, //novyi element
struct List **start, //pervyi element spiska
struct List **last //poslednii element spiska
)
{
struct List *old, *p;
if(*last==NULL) { //pervyi element spiska
i->next=NULL;
i->prior=NULL;
*last=i;
*start=i;
return;
}
p=*start; //nachat' s nachala spiska
old=NULL;
while(p) {
if(strcmp(p->word, i->word)<0){
old=p;
p=p->next;
}
else {
if(p->prior) {
p->prior->next=i;
i->next=p;
i->prior=p->prior;
p->prior=i;
return;
}
i->next=p; //novyi pervyi element
i->prior=NULL;
p->prior=i;
*start=i;
return;
}
}
old->next=i; //vstavka v konec
i->next=NULL;
i->prior=old;
*last=i;
}
//poisk slova
struct List *find( char *word)
{
struct List *info;
info=start;
while(info) {
if(!strcmp(word, info->word)) return info;
info=info->next; //pereiti k sleduyucshemu adresu
}
printf("Imya ne naideno.\n");
return NULL; //net podhodyaschego elementa
}
//otobrazit' na ekrane ves' spisok
void show(void)
{
struct List *info;
info=start;
while(info) {
display(info);
info=info->next; //pereiti k sleduyucshemu slovu
}
printf("\n\n");
}
//vyvod na ekran vseh polei zapisi
void display(struct List *info)
{
printf("%s\n", info->word);
printf("%d\n", info->stranica);
}
void search(void)
{
char word[100];
struct List *info;
printf("Vvedite slovo: ");
gets(word);
info=find(word);
if(!info) printf("Ne naideno\n");
else display(info);
} |