数据结构实验题

01

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
#include<iostream>
using namespace std;
int main()
{
int n,count;
cin>>n;
if(n>=2){
printf("%d ",2);
count++;
}
for(int i=0;i<n;i++){
for(int j=2;j*j<i;j++){
if(i%j==0){
continue;
}
else{
printf("%d ",i);
count++;
if(count%10==0){
printf("\n");
}
}
}
}
}

03

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>
#include <stdio.h>
#include<string.h>
using namespace std;
int main()
{

string s;
cin>>s;
string g=s;

for(int i=0;i<s.size();i++){
g[s.size()-i-1]=s[i];
}
if(s==g){
printf("回文");
}
else{
printf("非回文");
}
}

顺序表

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
#include<iostream>
typedef int ElemType;
void CreateList(SqList *&L,ElemType a[],int n)//由a中的a个元素建立顺序表
{
int i;
L=(SqList *)malloc(sizeof(SqList));
for(i=0;i<n;i++)
L->data[i]=a[i];
L->length=n;
}
void InitList(SqList *&L)//初始化线性表
{
L=(SqList *)malloc(sizeof(SqList));
L->length=0;
}
void LocatElem(SqList *L,ElemType e)//查找元素
{
int i=0;
while(i<L->length&&L->data[i]!=e)
i++;
if(i>=L->length)
return 0;
else
return i+1;

}
bool ListInsert(SqList *&L,int i,ElemType e)//插入元素
{
int j;
if(i<1||i>L->length+1)
return false;
i--;
for(j=L->length;j>i;j--)
L->data[j]=L->data[j-1];
L->data[i]=e;
L->length++;
return trun;
}
using namespace std;
int main()
{
CreateList la,lb;
int j,i;
i=initlist(&la);
if(i==1)

}

顺序表

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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368

#include<stdio.h>

#include<stdlib.h>

#define MAXSIZE 100

typedef int DataType;

#define OK 1

#define ERROR -1

#define FALSE 0

#define True 2

typedef struct

{

DataType data[MAXSIZE];

int length;

} SqList;

int InitList(SqList *L )//初始化

{

L->length=0; //空表,长度为0

return OK;

}

int ListEmpty(SqList *L)//判断是否为空表

{

if (L->length==0)

return True;

if (L->length!=0)

return FALSE;

}

int ListLength(SqList L)//求表长

{

return (L.length);

}

void ListTraverse(SqList L)//输出表

{

int i;

for(i=0;i<L.length;i++)

printf("%d ",L.data[i]);

printf("\n");

}

int ListInsert(SqList *L,int i,DataType e)//插入元素

{

int k;

if (L->length==MAXSIZE)

return ERROR;

if (i<1 || i>L->length+1)

return ERROR;

if (i<=L->length)

{

for(k=L->length-1;k>=i-1;k--)

L->data[k+1]=L->data[k];

}

L->data[i-1]=e;

L->length++;

return OK;

}

int ListDelete(SqList *L,int i,DataType *e)//删除元素

{

int k;

if (L->length==0) return ERROR;

if (i<1 || i>L->length) return ERROR;

*e=L->data[i-1];

if (i<L->length)

{

for(k=i;k<L->length;k++)

L->data[k-1]=L->data[k];

}

L->length--;

return OK;

}

int ListFindEle(SqList *L,DataType e)// 查找元素

{

int i;

for (i=1; i<=L->length; i++)

{

if (e == L->data[i-1])

return i;

}

return FALSE ;

}

int ListAlter(SqList *L,int i,DataType *e)//修改元素

{

if (i<1||i>L->length)

return ERROR;

L->data[i-1]=*e;

return OK;

}

void ClearList(SqList *L)//清空表

{

L->length=0; //将线性表的长度置为0

}

//释放表空间

void DestroyList(SqList *L) //释放线性表占据的所有存储空间

{

if (L->data)

free(L->data);

}

int main()

{

int tmp=0,locat=0,res=0,n,i;

SqList *list;

list=(SqList*)malloc(sizeof(*list));

printf(" ***********************************\n");

printf("* 1---------初始化 *\n");

printf("* 2---------判断是否为空表 *\n");

printf("* 3---------求表长 *\n");

printf("* 4---------输出表*\n");

printf("* 5---------插入元素 *\n");

printf("* 6---------删除元素 *\n");

printf("* 7---------查找元素 *\n");

printf("* 8---------修改元素 *\n");

printf("* 9---------清空表 *\n");

printf("* 10--------释放表空间 *\n");

printf("* 11--------输入线性表初始值 *\n");

printf("* 12--------退出 *\n");

printf(" ************************************\n");

while(1)

{

printf("请选择菜单号(1-12):");

scanf("%d",&n);

if(n==0||n>11)

break;

switch(n)

{

case 1:

InitList(list);

printf("已完成初始化!\n");

break;

case 2:

if(ListEmpty(list)==True)

printf("是空表!\n");

else

printf("不是空表!\n");

break;

case 3:

printf("线性表长度为:%d\n",ListLength(*list));

break;

case 4:

printf("线性表元素的值为:");

ListTraverse(*list);

break;

case 5:

printf("要插入的位置和数值为:");

scanf("%d %d",&locat,&res);

ListInsert(list,locat,res);

printf("插入完成!\n");

break;

case 6:

printf("要删除的位置为:");

scanf("%d",&locat);

ListDelete(list,locat,&res);

printf("删除完成!\n");

break;

case 7:

printf("要查找的位置为:");

scanf("%d",&locat);

printf("该位置的值为:%d\n",ListFindEle(list,locat));

break;

case 8:

printf("要修改的位置和数值为:");

scanf("%d %d",&locat,&res);

ListAlter(list,locat,&res);

printf("修改完成!\n");

break;

case 9:

ClearList(list);

printf("表中内容已清空!\n");

break;

case 10: DestroyList(list);

printf("释放成功!\n");

break;

case 11:

InitList(list);

printf("请输入顺序表长度:");

scanf("%d",&list->length);

printf("请输入顺序表的值:");

for(i=0;i< list->length;i++)

scanf("%d",&list->data[i]);

printf("线性元素的值为:\n");

ListTraverse(*list);

break;

case 12:

exit(1);

default:break;

}

}

return 0;

}

04

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
#include <stdio.h>  
#include <stdlib.h>
#include <string.h>

struct lnode
{
int ID;
struct lnode *next;
};

void BubbleSort(struct lnode * head);
int main()
{
int n, i, d;

while(scanf("%d", &n) != EOF)
{
//初始化带头节点的链表
struct lnode *head, *s, *r, *p;

r = head;
for(i = 0; i < n; i ++)
{
scanf("%d", &d);

s -> ID = d;
r -> next = s;
r = s;
}
r -> next = NULL;

//冒泡排序
BubbleSort(head);

//打印输出
for(p = head -> next; p != NULL; p = p -> next)
{
if(p -> next == NULL)
{
printf("%d\n", p -> ID);
}else
{
printf("%d ", p -> ID);
}
}
}

return 0;
}


void BubbleSort(struct lnode *head)
{
struct lnode *f, *p, *x, *y;
f = NULL;

if(head -> next == NULL || head -> next -> next == NULL)
{
return;
}
while(f != head->next->next)
{
for(p = head; p -> next -> next != f; p = p -> next)
{
if(p -> next -> ID > p -> next -> next ->ID)
{
x = p -> next;
y = p -> next -> next;
p -> next = y;
x -> next = y -> next;
y -> next = x;
}
}
f = p -> next;
}
}

04

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
#include<iostream>
#include<cstdlib>
#include<cstdio>
using namespace std;

struct Node{
char Element;
int length;
Node *last,*next;
};
typedef char ElementType;
typedef Node *List;
List L;

List CreateList(List &L)
{
List I=(List)malloc(sizeof(Node));
int length=0;L->next=L->last=nullptr;
return I;

}
void insect(List &L,ElementType a)
{
L->length++;
Node*n=(Node *)malloc(sizeof(Node));
n->Element=a;
if(L->next==nullptr){
L->last=n;L->next=n;
n->last=n;n->next=n;
}
else{
L->last->next;//L原来的最后一个节点的next指向新最后一个节点
n->last=L->last;//新最后一个节点的last指向旧最后一个节点
n->next=L->next;//新最后一个节点指向第一个节点(循环链表)
L->next->last=n;//第一个节点指向最后一个节点
L->last=n;//指定最后一个节点
}

}

void print(List &L)
{
int n=L->length;
Node *Node=L->next;
for(int i=0;i<n;i++){
cout<<Node->Element;
Node=Node->next;
}

}

void output3th(List &L)
{
Node *Node=L->next;
for(int i=0;i<3;i++){
Node=Node->next;
}
cout<<Node->Element<<endl;
}

void location(List &L)
{
char a;
Node *Node=L->next;
for(int i=1;i<=L->length;i++){
if(Node->Element==a){
cout<<"a location"<<i;
}
else
Node=Node->next;
}
}

void insect4f(List &L)
{
Node *m=(Node *)malloc(sizeof(Node));
Node *Node=L->next;
for(int i=1;i<=3;i++){
Node->next=m;
L->last->last->last=m;
m->last=Node->next;
m->next=L->last->last;
}

}

void delete3th(List &l)
{
Node *tmp,*Node=L->next;
for(int i=1;i<=3;i++){
Node=Node->next;
}
tmp=Node;free(tmp);//删掉啦第三个节点
L->next->next->next=L->last->last;//第二个节点的next指向第四个节点
L->last->last->last=L->next->next;//第四个节点的last指向第二个节点

}
void DestroyList(List &L)
{
Node *tmp,*Node=L->next;
for(int i=0;i<L->length;i++){
tmp=Node;
Node=Node->next;
free(tmp);
}
free(L);
}
int printlength(List &L)
{
return L->length;
}
int main()
{
char a,b,c,d,e,f;
List L=CreateList(L);
insect(L,a);insect(L,b);insect(L,c);insect(L,d);insect(L,e);
print(L);
cout<<L->length<<endl;
cout<<printlength(L)<<endl;
output3th(L);
location(L);
insect4f(L);
print(L);
delete3th(L);
print(L);
DestroyList(L);
}

05

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
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
typedef Node2 *HList;
//1.数据储存
//创建存储结构
#define MaxCol 10
typedef int ElemType;
typedef struct Node1
{
ElemType data[MaxCol];
struct Node1 *next;
}DList;

//指定每个表的行列数
typedef struct Node2
{
int Row, Col;
DList *next;
}HList;

//2.运算方法
//建表
void CreateTable(HList)
{
int i, j;
DList *r, *s;
h = (HList *)malloc(sizeof(HList));
printf("表的行列数:\n");
scanf("%d%d", &h->Row, &h->Col);
for (int i = 0; i < h->Row; i++) {
printf("第%d行:", i + 1);
s = (DList *)malloc(sizeof(DList);
for (int j = 0; j->h->Col; j++) {
scanf("%d", &s->data[j]);
if (h->next == NULL)
h->next = s;
else
r->next = s;
r = s;


}
r->next=NULL;
}
}

//销毁单链表
void DestroyTable(HList *h)
{
DList *pre = h->next, *p = pre->next;
while (p!=NULL)
{
free(pre);
pre = p;
p = p->next;
}
free(pre);
free(h);
}
//输出单链表
void DispTable(HList *h)
{
int j;
DList *p = h->next;
while (p!=NULL)
{
for (j = 0; j < h->Col; j++) {
printf("%4d", p->data[j]);
printf("\n");
p = p->next;
}
}
}
//表连接运算法
void LinkTable(HList *h1,HList *h2,HList *h)
{
int i, j;
DList *p = h->next, *q, *s, *r;
printf("连接字段是:第一个表序号,第二个表序号:");
scanf("%d%d", &i, &j);
h = (HList)malloc(sizeof(HList));
h->Row = 0;
h->Col = h1->Col + h2->Col;
h->next = NULL;
while (p!=NULL)
{
q = h2->next;
while (q != NULL)
{
if (p->data[i - 1] == q->data[j - 1]) {
s = (DList)malloc(sizeof(HList));
for (int k= 0; k < h1->Col; k++)
s->data[k] = p->data[k];
for (int k = 0; k < h2->Col; k++)
s->data[h1->Col + k] = q->data[k];
if (h->next == NULL)
h->next = s;
else
r->next = s;
r = s;
h->Row++;

}
p = p->next;
}
r->next = NULL;
}
}



void main()
{
HList *h1, *h2, *h;
printf("表1:、n");
CreateTable(h1);
printf("表2:\n");
CreateTable(h2);
LinkTable(h1, h2, h);
printf("链接结果表:\n");
DispTable(h);
DestroyTable(h1);
DestroyTable(h2);
DestroyTable(h);
}

06

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
#include<iostream>
using namespace std;


typedef struct
{
int i, j;
int pre;
}Box;
typedef struct
{
Box data[10];
int front, rear;
}QuType;

bool mgpath1(int xi, int yi, int xe, int ye)
{

int i, j, find = 0, di;
QuType qu;
qu.front = qu.rear = -1;
qu.rear++;
qu.data[qu.rear].i = xi; qu.data[qu.rear].j = yi; //(xi, yi)进队
qu.data[qu.rear].pre = -1;
mg[xi][yi] = -1;
while (qu.front != qu.rear && !find)
{
qu.front++;
i = qu.data[qu.front].i; j = qu.data[qu.front].j;
if (i == xe&&j == ye)
{
find = 1;
print(qu, qu.front);
return true;

}

for (di = 0; di < 4; di++) {
switch (di)
{
case 0:i = qu.data[qu.front].i - 1; j = qu.data[qu.front].j; break;
case 1:i = qu.data[qu.front].i; j = qu.data[qu.front].j + 1; break;
case 2:i = qu.data[qu.front].i + 1; j = qu.data[qu.front].j; break;
case 3:i = qu.data[qu.front].i; j = qu.data[qu.front].j - 1; break;
}
if (mg[i][j] == 0) {
qu.rear++;
qu.data[qu.rear].i = i; qu.data[qu.rear].j = j;
qu.data[qu.rear].pre = qu.front;
mg[i][j] = -1;
}
}
}
return false;
}
//从队列qu中输出路径
void print(QuType qu, int front)
{
int k = front, j, ns = 0;
printf("\n");
do {
j = k;
k = qu.data[k].pre;
qu.data[j].pre = -1;

} while (k != 0);
printf("迷宫路径如下:、n");
k = 0;
while (k<10)
{
if (qu.data[k].pre == -1) {
ns++;
printf("\t(%d,%d)", qu.data[k].i, qu.data[k].j);
if (ns % 5 == 0) printf("\n");

}
k++;
}
printf("\n");
}

void main()
{
int M, N;
cin >> M >> N;
if (!mgpath1(1, 1, M, N))
printf("迷宫误解");

}

07

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
#include<iostream>
#include<stdio.h>
using namespace std;
typedef string SqString;
int index(SqString s, SqString t)
{
unsigned int i = 0, j = 0;
while ( i<s.length()&&j<t.length())
{
if (s[i] == t[j]) {
i++; j++;

}
else
{
i = i - j + 1;
j = 0;
}
if(j<=t.length())
break;
}
if (j <= t.length()){
return(i - t.length());}

else{

return(-1);}
}

int main()
{
string s = "aaaaab";
string t = "aaab";
index(s, t);
return 0;

}

08
`
//创建二叉树

#include<btree.h>
void CreatBTNode(BTnode &b,char str)
{
BTNode St[MaxSize],p;
int top=-1,k,j=0;
char ch;
b=NULL;
ch=str[j];
while(ch!=’\0’)
{switch(ch)
{
case ‘(‘:top++,St[top]=p;k=1;break;
case ‘)’:top–;break;
case ‘,’:k=z;break;
default:p=(BTNode *)malloc(sizeof(BTNode));
p->data=ch;p->lchild=p->rchild=NULL;
if(b==NULL)
b=p;
else
{
switch(k)
{
case1:St[top]->lchild=p;break;
case2:St[top]->rchild=p;break;
}
}
}
j++;ch=str[j];
}
}

//查找节点
/递归模型
f(b,x)=NULL
f(b,x)=b
f(b,x)=p
f(b,x)=f(b->rchild,x)
/
//对应的递归算法
BTNode FindNode(BTNode b,ElemType x)
{ BTNode *p;
if(b==NULL)
return NULL;
else if(b->data==x)
return b;
else
{ p=FindNode(b->lchild,x);
if(p!=NULL)
return p;
else
return FindNode(b->rchild,x);
}

}

//找孩子节点
BTNode lchildNode(BTNode p)
{
return p->lchild;
}

BTNode rchildNode(BTNode p)
{
return p->rchild;
}

//求高度
/递归模型
f(b)=0
f(b)=MAX{f(b->lchild),f(b->rchild)}
/
//递归算法
int BTNodeHeight(BTNode *b)
{ int lchild,rchild;
if(b==NULL) return 0;
else
{ lchild= BTNodeHeight(b->lchild);//求左子树的高度
rchild=BTNodeHeight(b->rchild);
return (lchild>rchild?(lchild+1:rchild+1));
}

}

//输出二叉树
void DispBTNode(BTNode *b)
{ if(b!=NULL)
{ printf(“%c”,b->data);
if(b->lchild!=NULL||b->rchild!=NULL)
{ printf(“(\n” );
DispBTNode(b->lchild);
if(b->rchild!=NULL) printf(“,\n” );
DispBTNode(b->rchild);
printf(“)\n” );
}
}
}

//二叉树的遍历
//先序遍历
void PreOrder(BTNode b)
{ if(b!=NULL)
{ printf(“%c\n”,b->data );
PreOrder(b->lchild);
PreOrder(b->rchild);
}
}
//中根遍历
void InOrder(BTNode
b)
{ if(b!=NULL)
{
InOrder(b->lchild);
printf(“%c\n”,b->data );
InOrder(b->rchild);
}
}
//后根遍历

void PostOrder(BTNode b)
{ if(b!=NULL)
{
PostOrder(b->lchild);
PostOrder(b->rchild);
printf(“%c\n”,b->data );
}
}
void main()
{
BTNode
b,p,lp,*rp;
CreatBTNode(b,”A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))”);
printf(“二叉树的基本运算”);
printf(“(1)输出二叉树:”); DispBTNode(b);printf(“\n” );

}