C++顺序表的创建、初始化、插入、删除、查找及遍历
编译器是VS 2019
转专业上的数据结构课作业,因为时间冲突只能自学C++,前期并没有编程基础,故开博客用来记录自己的作业和学习心得、体会,供后期复习使用。
交作业时的代码>
#include <iostream>
using namespace std;
#define LIST_SIZE 100
#define LIST_INCREASE 10
struct student {
char name[100]; //姓名、年龄、学号
int year;
int code;
};
typedef student elemtype;
typedef struct {
elemtype* elem;
int length;
int listsize;
}list;
void intilist(list& l) {
l.elem = new student[LIST_SIZE];
l.length = 0;
l.listsize = LIST_SIZE;
}
void output(list l) //遍历
{
cout << "当前学生信息表如下>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> " << endl;
for (int i = 0; i < l.length; i++) {
cout << "编号" << " " << i + 1 << "\t" << "姓名" << " " << l.elem[i].name << "\t" << "学号" << " " << l.elem[i].code << "\t" << "年龄" << " " << l.elem[i].year << endl;
}
cout << endl;
}
void input(list& l) {
int num;
cout << "请输入学生数" << endl;
cin >> num;
while (num < 0) {
cout << "学生数量不合法,请重新输入" << endl;
cin >> num;
}
while (num > l.listsize) {
l.listsize += LIST_INCREASE;
}
if (l.listsize > LIST_SIZE) {
cout << "表格已延长" << l.listsize - LIST_SIZE << ",当前表长" << l.listsize << endl;
}
for (int i = 0; i < num; i++) {
cout << "请输入学生" << i + 1 << "信息(姓名、年龄、学号)" << endl;
cin >> l.elem[i].name >> l.elem[i].year >> l.elem[i].code;
for (int j = 0; j < i; j++) {
while (l.elem[i].code == l.elem[j].code) //这里的判断用while而不用if,if只判断一次,while每次都判断。
{
cout << "该学号已存在,请重新输入" << endl;
cin >> l.elem[i].code;
}
}
l.length++;
}
cout << endl;
output(l);
}
void insert(list& l) //允许前、后插入
{
int pos;
cout << "请输入要插入的位置(编号)" << endl;
cin >> pos;
l.length++; //先加表长,防止当数据插入时丢失
while (pos <= 0 || pos > l.length) {
cout << "插入位置不合法,请重新输入" << endl;
cin >> pos;
}
while (pos <= l.length - 1) //不在表尾部插入时,要后移表内数据
{
for (int i = l.length - 1; i > pos - 1; i--) {
l.elem[i] = l.elem[i - 1];
}
break; //只后移一次就可以跳出了
}
cout << "请输入要插入的学生信息(姓名、学号、年龄)" << endl;
cin >> l.elem[pos - 1].name >> l.elem[pos - 1].code >> l.elem[pos - 1].year;
for (int j = 0; j < (pos - 1); j++) {
while (l.elem[pos - 1].code == l.elem[j].code) //这里的判断用while而不用if,if只判断一次,while每次都判断。
{
cout << "该学号已存在,请重新输入" << endl;
cin >> l.elem[pos - 1].code;
}
}
cout << "表长" << l.length << endl;
cout << endl;
output(l);
}
void delete_mumber(list& l) {
int pos;
cout << "请输入要删除的学生编号:" << endl;
cin >> pos;
while (pos <= 0 || pos > l.length) {
cout << "删除位置不合法,请重新输入" << endl;
cin >> pos;
}
while (pos < l.length) {
for (int i = pos - 1; i < l.length - 1; i++) {
l.elem[i] = l.elem[i + 1];
}
break;
}
l.length--;
cout << endl;
output(l);
}
void find_location(list& l) //基于位置查找
{
int loc;
cout << "下面是基于位置查找\n请输入要查找的学生编号:" << endl;
cin >> loc;
while (loc<1 || loc>l.length) {
cout << "输入的学生编号不合法,请重新输入" << endl;
cin >> loc;
};
cout << "要查找的学生信息为" << endl;
cout << "编号" << " " << loc << "\t" << "姓名" << " " << l.elem[loc - 1].name << "\t" << "学号" << " " << l.elem[loc - 1].code << "\t" << "年龄" << " " << l.elem[loc - 1].year << endl;
cout << endl;
}
void find_value(list& l) //基于值的查找
{
int code;
cout << "请输入要查找的学生学号" << endl;
cin >> code;
for (int i = 0; i < l.length; i++) {
while (code < 0) {
cout << "输入的学号不合法,请重新输入" << endl;
cin >> code;
}
while (l.elem[i].code == code) {
cout << "要查找的学生信息为" << endl;
cout << "编号" << " " << i + 1 << "\t" << "姓名" << " " << l.elem[i].name << "\t" << "学号" << " " << l.elem[i].code << "\t" << "年龄" << " " << l.elem[i].year << endl;
break;
}
while (i + 1 == l.length) {
cout << "\n查无此人" << endl;
break;
}
}
}
int main()
{
list stu_info;
intilist(stu_info);
input(stu_info);
insert(stu_info);
delete_mumber(stu_info);
find_location(stu_info);
find_value(stu_info);
}
反思与心得
- 关于while和if
while使用在需反复判断的地方,像上面的 位置不合法,请重新输入 在重新输入时,需要再次判断输入的是否合法,如果这时候用if,那么再输入一个不合法的位置,还是能通过的。
while的使用还要注意break;在调试中,常有出现一直满足while的条件,无法跳出循环的情况。 - 顺序表的创建。
- 顺序表的创建首先要声明一个结构,这个结构用来定义每个学生信息单元的样子,声明语句写法如下
struct student {
char name[100]; //姓名、年龄、学号
int year;
int code;
};
这样后面的student结构的顺序表list就有了name、year和code三个属性。
可以用l.elem[i].name = XXX来写入。
- 然后给student创建一个名字?
typedef student elemtype;
这一块代码不是很懂,先记顺序,后期再修改
然后定义顺序表list。
typedef struct {
elemtype* elem;
int length;
int listsize;
}list;
这句语句:elemtype* elem; 似乎是为list申请一个