单双链表的例程

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

#include "stdlib.h"
#include "malloc.h"

#define Element int

#define turn int
//to adjust which point to choice printup or printdown

//if change "int",The scanf's number in it must be change too;

typedef struct
{
Element D;
Single *next;//to back
}list;
//single list type

typedef struct Double
{
Element D;
Double *front;
Double *next;
}list_double;
//double list type


//单链表例程
void init_single(list *&L);
void creatlist_single(list *&L,Element n);
void printlist_single(list *&L);
void insertlist_single(list *&L,Element n);//插入例程
list *findlist_single(list *&L,Element n);//用于发现一个节点的前驱元.n为要发现的节点
void delelist_single(list *&L,Element n);//删除为n的
void delelist_all(list *&L,Element n);//删除所有为n的

void init_single(list *&L)//has been done
{
L=(list*)malloc(sizeof(list));
L->next=NULL;
}

void creatlist_single(list *&L,int n)//has been done
{
list *s,*r;
init_single(L);
r=L;
while(n!=0)
{
s=(list*)malloc(sizeof(list));
scanf("%d",&s->D);
r->next=s;
r=r->next;//use plus to position to position
n--;
}
r->next=NULL;
}

void printlist_single(list *&L)//has been done
{
list *p;
p=L->next;
while(p!=NULL)
{
printf("%d",p->D);
p=p->next;
}
printf("n");
}

list* findlist_single(list *&L,Element n)//find plus preious
{
list *p;
p=L;
while(p->next!=NULL && p->next->D!=n)
p=p->next;
return p;
}

void delelist_single(list *&L,Element n)//has been done
{
list *p,*temp;
p=findlist_single(L,n);
if(p!=NULL)
{
temp=p->next;
p->next=temp->next;
free(temp);
}
}

void delelist_all(list *&L,Element n)//has been done!!!
{
list *p,*tmp;
p=L;
list *r;
while(1)
{
r=findlist_single(L,n);
if(r->next==NULL)
break;
tmp=r->next;
r->next=tmp->next;
free(tmp);
}
}

void insertlist_single(list *&L,Element n)//has been done!!!
{
list *newcell=NULL;
list *p;
Element m;
p=L;
printf("请输入要插入的数:n");
scanf("%d",&m);
while(p->next!=NULL)
{
if(p->D==n)
{
newcell=(list*)malloc(sizeof(list));
newcell->D=m;
newcell->next=p->next;
p->next=newcell;
}
p=p->next;
}
if(p->D==n && p->next==NULL)
{
newcell=(list*)malloc(sizeof(list));
newcell->D=m;
p->next=newcell;
newcell->next=NULL;
}
}

//双链表例程

void init_double(list_double *&head,list_double *&rear);//different from init_single
void creatlist_double(list_double *&head,list_double *&rear,Element n);//it use "front plus"
void prints(list_double *&head,list_double *&rear,turn tag);
void insertlist_double(list_double *&head,Element n);
void delelist_double(list_double *&head,Element n);

void init_double(list_double *&head,list_double *&rear)//has been done;
{
head=(list_double*)malloc(sizeof(list_double));
head->next=NULL;
head->front=NULL;
rear=(list_double*)malloc(sizeof(list_double));
rear->next=NULL;
rear->front=NULL;
}

void creatlist_double(list_double *&head,list_double *&rear,Element n)//has been done
{
init_double(head,rear);
list_double *s,*r,*temp=NULL;
r=head;
while(n--)
{
s=(list_double*)malloc(sizeof(list_double));
scanf("%d",&s->D);
r->next=s;
temp=r;
r=r->next;
r->front=temp;
}
r->next=NULL;
rear=r;
}

void prints(list_double *&head,list_double *&rear,turn tag)//if tag=1 upprint,else downprint
{
list_double *p;
if(tag==1)
{ p=head->next;
while(p!=NULL)
{
printf("%d",p->D);
p=p->next;
}
}
else if(tag==0)
{
p=rear;
while(p->front!=NULL)
{
printf("%d",p->D);
p=p->front;
}
}
printf("n");
}

void insertlist_double(list_double *&head,Element n)//insert back after n. has been done
{
list_double *p1,*p3=NULL;
p1=head;
Element m;
printf("请输入要插入的数:n");
scanf("%d",&m);
while(p1->next!=NULL)
{
if(p1->D==n)
{
p3=(list_double*)malloc(sizeof(list_double));
if(p3==NULL)
{
printf("there is no space!n");
break;
}
p3->D=m;
p3->front=p1->front;
p3->next=p1->next;
p1->next=p3;
p1->front=p3;
}
p1=p1->next;
}
if(p1->D==n && p1->next==NULL)//LAST POINT
{
p3=(list_double*)malloc(sizeof(list_double));
p3->D=m;
p1->next=p3;
p1->front=p3;
p3->next=NULL;
p3->front=NULL;
}
}

void delelist_double(list_double *&head,Element n)//has been done
{
list_double *p1,*p2;
p1=head;
while(p1->next!=NULL)
{
p2=p1->next;
if(p2->D==n)
{
if(p2->next!=NULL)//not last point
{
p2->front->next=p2->next;
p2->next->front=p2->front;
free(p2);
}
else//last point
{
p2->front->next=NULL;
free(p2);
}
}
else
p1=p1->next;
}
}


int main()
{
Element n,m;
Element a,b;
turn tag;//tag=1 up tag=0 down
list *L;//相当于head指针
list_double *head;
list_double *rear;
scanf("%d",&n);//确定链表长度

/*Single list creat test*/

//creatlist_single(L,n);
//printlist_single(L);

/*Single delete list creat test*/

//scanf("%d",&m);//要删除的数
//delelist_single(L,m);
//delelist_all(L,m);
//printlist_single(L);

/*Single insert*/

//insertlist_single(L,a);//在所有a后插入一个数
//printlist_single(L);



/*Double list creat test*/

creatlist_double(head,rear,n);
prints(head,rear,1);

/*Double delete list creat test*/

//delelist_double(head,m);
//prints(head,rear,1);

/*Double insert list creat test*/

printf("请输入要在什么数后插入:n");
scanf("%d",&a);

/*Double insert*/
insertlist_double(head,a);
prints(head,rear,1);
return 0;
}