一.实验目的:
1.掌握c++调试线性表的基本操作方法。
2.掌握顺式与链式线性表的基本操作:插入,删除,查找等等。
3.如何改进程序的健壮性如何做到界面友好。
二.实验环境
vscode,g++编译器。
三.上机内容
定义一个包含学生信息的顺式表和链式表,使其具有如下功能:
- 指定学生个数,输入学生信息。
- 显示列表中所有学生的学生信息。
- 根据名字查询学生学号与成绩。。
- 根据列表顺序来查找学生信息。
- 给定一个学生信息,插入到表中指定的位置。
- 删除指定学生的学生记录。
- 统计表中个数。
四.上机调试流程图
链式顺序表中根据姓名查找的算法流程图:
启动程序,然后输入姓名a。在列表中判断有没有name==a,如果有则输出学生信息。没有则输出没有这名学生。
链式顺序表中插入算法的流程图:
程序开始,输入插入位置a和数据x。先判断插入位置a是否合法,再将第a-1个节点指针域指向x,x的指针域指向原来的第a个节点。
五.测试结果
1.启动程序,检测创建列表功能。
按照菜单提示,先输入1。
继续根据提示输入数据 1 a 1
3 c 3
结果创建列表成功。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18========开始======== 菜单: 创建列表请按1. 输出列表请按2. 插入数据请按3. 删除数据请按4. 姓名查找请按5. 顺序查找请按6. 列表长度请按7. 结束程序请按8. 请选择下一操作: 1 输入学生人数2 输入学号,姓名,成绩1 a 1 输入学号,姓名,成绩3 c 3 创建列表成功!!! 请选择下一操作:
2.检测插入数据功能。按3选择插入数据功能,插入数据2 b 2到第二个数据单元,在输入2查看线性表。结果如下,成功将数据2 b 2插入到列表第二个数据单元。
1
2
3
4
5
6
7
8
93 分别输入插入的位置,学号,姓名与分数: 2 2 b 2 请选择下一操作: 2 1 a 1 2 b 2 3 c 3 请选择下一操作:
3.检测两种查询功能。输入5,按照名字查询学号和成绩。输入6,按照序号查学生信息。结果如下,查询功能都成功运行。
1
2
3
4
5
6
7
8
9
105 输入待查找的名字:b 学号:2 成绩:2 请选择下一操作: 6 请输入学生序号: 1 学号:1 姓名:a 分数:1 请选择下一操作:
4 检测删除功能。
先输入7查询列表长度,按2查看链表。
然后输入4,删除第二个数据单元,再查看长度与链表。
结果如下,成功删除了第二个数据单元,列表长度减一,查看列表中第二个数据元素被删除。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
187 列表的长度:3 请选择下一操作: 2 1 a 1 2 b 2 3 c 3 请选择下一操作: 4 请输入删除的排序1 请选择下一操作: 7 列表的长度:2 请选择下一操作: 2 1 a 1 3 c 3 请选择下一操作:
最后输入0,程序结束。
六.实验总结
在此次实验中,我学习到了如何改进程序的界面友好性,加强程序的健壮性。查询资料反复调试后,逐步改进了程序,学会了运用c➕➕语言来成功创建了顺式表和链式表,受益匪浅。
七.代码
顺式表代码如下
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#include<stdio.h> #include<iostream> #include <string.h> #include <string> using namespace std; //数组的最大长度 #define OK 1 #define error 0 #define MAX 10 //OK和error代表函数状态,分别为1(函数成功运行),0(函数出错) typedef int Status; //Status是函数类型,其值是函数结果状态代码,如OK代表成功,error代表出错。 //ElemType代表数组类型,根据实际情况确定,这里假设为int型 typedef struct { char name[20]; char id[8]; int score; }Student; typedef struct{ Student *elem; int length; }SqList; Status InitList(SqList &L) { L.elem=new Student [MAX]; L.length=0; return OK; } Status InputList(SqList &L){ cout<<"输入学生人数"; cin>>L.length; if (L.length>MAX||L.length<0) exit(error); for(int i=0;i<L.length;i++){ cout<<"输入学号,姓名,成绩"; cin>>L.elem[i].id>>L.elem[i].name>>L.elem[i].score; } cout<<"创建列表成功!!!n"; return OK; } Status OutList(SqList &L){ for(int i=0;i<L.length;i++){ cout<<L.elem[i].id<<" "<<L.elem[i].name<<" "<<L.elem[i].score<<endl; } return OK; } Status GetElem(SqList L) { char name[20]; cout<<"输入待查找的名字:"; cin>>name; int j=0; for(int i=0;i<L.length;i++) { if(strcmp(name,L.elem[i].name)==0) {cout<<"学号:"<< L.elem[i].id<<"n成绩:"<<L.elem[i].score<<endl;} else j++; } if (j==L.length) cout<<"没有这个人的名字"; return 0; } Status GetElem_2(SqList L) { cout<<"请输入学生序号:n"; int i; cin>>i; while (i<0||i>L.length) { cout<<"请输入合法的学生序号:n"; cin>>i; } i=i-1; cout<<"学号:"<<L.elem[i].id<<" 姓名:"<< L.elem[i].name<<" 分数:"<<L.elem[i].score<<endl; return OK; } Status ListInsert(SqList &L) { int i; char name[20]; char id[8]; int score; cout<<"分别输入插入的位置,学号,姓名与分数:n"; cin>>i>>id>>name>>score; if (i<0||i>L.length+1) return error; if (L.length==MAX) return error; for (int j=L.length;j>=i-1;j--) { L.elem[j+1]=L.elem[j]; } strcpy(L.elem[i-1].name,name); strcpy (L.elem[i-1].id,id); L.elem[i-1].score=score; ++L.length; return OK; } Status ListDelete(SqList &L) { int i; cout<<"请输入删除的排序"; cin>>i; if (i<0||i>L.length) { return error; } for (int j=L.length-1;j>i;j--) { L.elem[j-1]=L.elem[j]; } --L.length; return OK; } Status ListStastic(SqList &L){ cout<<"列表的长度:"<< L.length<<endl; return 0; } int main() { SqList List; InitList (List); cout<<"========开始========n"; cout<<"菜单:n"; int a; cout<<"创建列表请按1.n"; cout<<"输出列表请按2.n"; cout<<"插入数据请按3.n"; cout<<"删除数据请按4.n"; cout<<"姓名查找请按5.n"; cout<<"顺序查找请按6.n"; cout<<"列表长度请按7.n"; cout<<"结束程序请按8.n"; while(1) { cout<<"请选择下一操作:n"; cin>>a; switch (a) { case 0: cout<<"========结束========"; return 0; case 1: InputList(List); break; case 2: OutList(List); break; case 3: ListInsert(List); break; case 4: ListDelete(List); break; case 5: GetElem(List); break; case 6: GetElem_2(List); break; case 7: ListStastic(List); break; }} return 0; }
链式表代码如下:
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#include <iostream> #include <string> #include <string.h> #define OK 0 #define error -1 using namespace std; typedef int Status; typedef struct { char name[8]; char id[8]; float score; }Student; typedef struct LNode{ Student data; struct LNode *next; }LNode,*LinkList; Status InitList(LinkList &L){ L=new LNode; L->next=NULL; return OK; } Status InputList(LinkList &L){ int num; cout<<"请输入学生个数:n"; cin>>num; L=new LNode; L->data.score=num; L->next=NULL; LNode *r=L; for (int i=0;i<num;i++){ cout<<"分别输入学号,姓名,分数:n"; LNode *p=new LNode; cin>>p->data.id>>p->data.name>>p->data.score; p->next=NULL; r->next=p; r=p; } return OK; } Status OutputList(LinkList &L){ LNode*p=L->next; int a =L->data.score; for (int i=0;i<a;i++) {cout<<"学号:"<<p->data.id<<" 姓名:"<<p->data.name<<" 成绩:"<<p->data.score<<endl;p=p->next;} return OK; } Status GetList(LinkList &L){ char name[20]; cout<<"请输入查询的姓名n"; cin>>name; LNode*p=L->next; int a=L->data.score; int j=0; for (int i=0;i<a;i++){ if (strcmp((p->data).name,name)==0) { cout<<"学号为:"<<p->data.id<<"n分数为:"<<p->data.score<<endl;} else j++; p=p->next; } if (j==a) cout<<"查无此人"; return OK; } Status GetList_2(LinkList &L){ int i; cout<<"请输入序号"; cin>>i; if (i<0||i>L->data.score) { cout<<"请输入合法的学生序号n"; exit (error); } LNode*p=L->next; for (int j=0;j<i-1;j++) p=p->next; cout<<"学号:"<<p->data.id<<" 姓名:"<< p->data.name<<" 分数:"<<p->data.score<<endl; return 0; } Status InsertList(LinkList &L){ int i,j; char name[20]; char id[8]; int score; cout<<"分别输入插入的位置,学号,姓名与分数n"; cin>>i>>id>>name>>score; LNode*p=L;j=0; while((p->next)&&(j<i-1)) {p=p->next;j++;} if (!p||j>i-1) return error; LNode *s; s=new LNode; strcpy(s->data.id,id); s->data.score=score; strcpy(s->data.name,name); s->next=p->next; p->next=s; L->data.score++; return OK; } Status DeleteList(LinkList &L){ cout<<"输入需删除的序号:"; int i,j; cin>>i; LNode *p=L;j=0; while((p->next)&&(j<i-1)) {p=p->next;++j;} if (!p||j>i+1) return error; LNode *q; q=p->next; p->next=q->next; delete q; L->data.score--; return OK; } Status ListStastic(LinkList &L){ cout<<"长度为:"<<L->data.score<<endl; return OK; } int main() { LinkList List; int a; cout<<"请选择一下操作n"; cout<<"创建列表请按1.n"; cout<<"输出列表请按2.n"; cout<<"插入数据请按3.n"; cout<<"删除数据请按4.n"; cout<<"姓名查找请按5.n"; cout<<"顺序查找请按6.n"; cout<<"列表长度请按7.n"; cout<<"结束程序请按8.n"; while(1) { cout<<"请选择一下操作n"; cin>>a; switch (a) { case 0: cout<<"========结束========"; case 1: InputList(List); break; case 2: OutputList(List); break; case 3: InsertList(List); break; case 4: DeleteList(List); break; case 5: GetList(List); break; case 6: GetList_2(List); break; case 7: ListStastic(List); break; }} }
最后
以上就是甜美御姐最近收集整理的关于数据结构第一次上机实验报告一.实验目的:二.实验环境三.上机内容四.上机调试流程图 五.测试结果六.实验总结七.代码的全部内容,更多相关数据结构第一次上机实验报告一.实验目内容请搜索靠谱客的其他文章。
发表评论 取消回复