案例要求:
1.定义顺序表,实现顺序表的基本操作。
2.利用1实现学生信息管理(学生信息包括学号、姓名、年龄等),包括:
⑴插入学生信息,建立至少有五个学生信息的线性表。
⑵更新指定学号的学生姓名。
⑶查找指定学号的学生信息。
⑷输出所有学生信息。
⑸删除指定学号的学生信息。
注:其中的输入数据均来自于标准输入。
案例分析:
创建结构体
//学生类型
typedef struct {
char sno[4];
char sname[10];
int sage;
char sdept[3];
}StuType;
定义顺序表
typedef struct {
ElemType* stu;
int length;
int listsize;
}List;
初始化列表
Status InitList(List& L)
{
L.stu = (ElemType*)malloc(LIST_INIT_SIZE * sizeof(ElemType));
if (!L.stu)exit(OVERFLOW);
L.length = 0;
L.listsize = LIST_INIT_SIZE;
return OK;
}
功能实现
//插入学生信息
Status ListInsert(List& L, int i, ElemType e)
{
if (i<1 || i>L.length + 1)
{
return ERROR;
}
L.length += 1;
if (L.length > L.listsize)
{
ElemType* newbase;
newbase = (ElemType*)realloc(L.stu, (L.listsize + LISTINCREMENT) * sizeof(ElemType));
if (!newbase)exit(OVERFLOW);
L.stu = newbase;
L.listsize = LISTINCREMENT;
}
for (int t = L.length; t >= i; t--)
{
L.stu[t] = L.stu[t - 1];
}
L.stu[i - 1] = e;
return 1;
}
//定位指定学生
Status LocateElem(List L, ElemType e, Status(*equal)(ElemType, ElemType))
{
int i = 1;
ElemType* p;
p = L.stu;
while (i <= L.length && equal(*p++, e)==ERROR){ i++; }
if (i <= L.length)
{
return i;
}
else
{
return 0;
}
}
Status equal(ElemType c1, ElemType c2)
{
if (strcmp(c1.sno, c2.sno) == 0)
return OK;
else
return ERROR;
}
//删除学生信息
Status ListDelete(List& L, int i, ElemType& e) {
if (i<1 || i>L.length + 1)
{
return 0;
}
i--;
e = L.stu[i];
for (int j = i; j <= L.length; j++) {
L.stu[j] = L.stu[j + 1];
}
L.length--;
return 1;
}
//获取指定学生信息
Status GetElem(List L, int i, ElemType& e)
{
if (1 <= i && i <= ListLength(L))
{
e = L.stu[i - 1];
return OK;
}
else
{
return ERROR;
}
}
//学生信息赋值
Status PutElem(List& L, int i, ElemType e)
{
if (L.stu != NULL)
{
if (i > L.length || i < 0)
{
return ERROR;
}
else
{
L.stu[i - 1] = e;
return OK;
}
}
else
{
return OVERFLOW;
}
}
//遍历
Status visit(ElemType e)
{
printf("%s\t%s\t%d\t%s\n", e.sno, e.sname, e.sage, e.sdept);
return OK;
}
Status ListTraverse(List L, Status(*visit)(ElemType e)) {
if (0 == L.length) return ERROR;
for (int i = 0; i < L.length; i++) {
visit(L.stu[i]);
}
return OK;
}
完整代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TRUE 1
#define FALSE 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
//学生类型
typedef struct {
char sno[4];
char sname[10];
int sage;
char sdept[3];
}StuType;
typedef StuType ElemType;
#define OK 1
#define LIST_INIT_SIZE 100
#define ERROR -1
#define LISTINCREMENT 20
typedef struct {
ElemType* stu;
int length;
int listsize;
}List;
//初始化列表
Status InitList(List& L)
{
L.stu = (ElemType*)malloc(LIST_INIT_SIZE * sizeof(ElemType));
if (!L.stu)exit(OVERFLOW);
L.length = 0;
L.listsize = LIST_INIT_SIZE;
return OK;
}
//获取列表长度
Status ListLength(List L)
{
return L.length;
}
//插入学生信息
Status ListInsert(List& L, int i, ElemType e)
{
if (i<1 || i>L.length + 1)
{
return ERROR;
}
L.length += 1;
if (L.length > L.listsize)
{
ElemType* newbase;
newbase = (ElemType*)realloc(L.stu, (L.listsize + LISTINCREMENT) * sizeof(ElemType));
if (!newbase)exit(OVERFLOW);
L.stu = newbase;
L.listsize = LISTINCREMENT;
}
for (int t = L.length; t >= i; t--)
{
L.stu[t] = L.stu[t - 1];
}
L.stu[i - 1] = e;
return 1;
}
//定位指定学生
Status LocateElem(List L, ElemType e, Status(*equal)(ElemType, ElemType))
{
int i = 1;
ElemType* p;
p = L.stu;
while (i <= L.length && equal(*p++, e)==ERROR){ i++; }
if (i <= L.length)
{
return i;
}
else
{
return 0;
}
}
Status equal(ElemType c1, ElemType c2)
{
if (strcmp(c1.sno, c2.sno) == 0)
return OK;
else
return ERROR;
}
//删除学生信息
Status ListDelete(List& L, int i, ElemType& e) {
if (i<1 || i>L.length + 1)
{
return 0;
}
i--;
e = L.stu[i];
for (int j = i; j <= L.length; j++) {
L.stu[j] = L.stu[j + 1];
}
L.length--;
return 1;
}
//获取指定学生信息
Status GetElem(List L, int i, ElemType& e)
{
if (1 <= i && i <= ListLength(L))
{
e = L.stu[i - 1];
return OK;
}
else
{
return ERROR;
}
}
//学生信息赋值
Status PutElem(List& L, int i, ElemType e)
{
if (L.stu != NULL)
{
if (i > L.length || i < 0)
{
return ERROR;
}
else
{
L.stu[i - 1] = e;
return OK;
}
}
else
{
return OVERFLOW;
}
}
//遍历
Status visit(ElemType e)
{
printf("%s\t%s\t%d\t%s\n", e.sno, e.sname, e.sage, e.sdept);
return OK;
}
Status ListTraverse(List L, Status(*visit)(ElemType e)) {
if (0 == L.length) return ERROR;
for (int i = 0; i < L.length; i++) {
visit(L.stu[i]);
}
return OK;
}
void main()
{
List L;
ElemType stu, stu_tmp;
int func, pos;
if (!InitList(L))
{
printf("overflow!\n");
exit(-1);
}
for (int j = 0;; j++)
{
printf("Please select the function:\n0--Exit\n1--insert student\n2--delete student\n3--update student name\n4--search student with sno\n5--show all student\n6--count the student\n Please input:");
scanf_s("%d", &func);
switch (func)
{
case 0:
exit(1);
break;
case 1: //插入学生信息
printf("sno: ");
scanf_s("%s", &stu.sno,4);
printf("sname: ");
scanf_s("%s", &stu.sname,10);
printf("sage: ");
scanf_s("%d", &stu.sage);
printf("sdept: ");
scanf_s("%s", &stu.sdept,3);
ListInsert(L, ListLength(L) + 1, stu);
break;
case 2://删除指定学号的学生信息
printf("input the sno:");
scanf_s("%s", &stu.sno,4);
pos = LocateElem(L, stu, equal);
if (pos > 0)
{
ListDelete(L, pos, stu);
pos = NULL;
}
else
printf("The student is not exist!\n");
break;
case 3://更新指定学号的学生姓名
printf("input the sno of the student to be updated:");
scanf_s("%s", &stu.sno,4);
pos = LocateElem(L, stu, equal);
if (pos > 0)
{
printf("input the new name:");
scanf_s("%s", &stu.sname,10);
GetElem(L, pos, stu_tmp);
stu.sage = stu_tmp.sage;
strcpy_s(stu.sdept, stu_tmp.sdept);
PutElem(L, pos, stu);
pos = NULL;
}
else
printf("The student is not exist!\n");
break;
case 4://查找指定学号的学生信息
printf("input the sno:");
scanf_s("%s", &stu.sno,4);
pos = LocateElem(L, stu, equal);
if (pos > 0)
{
GetElem(L, pos, stu_tmp);
printf("%s\t%s\t%d\t%s\n", stu_tmp.sno, stu_tmp.sname, stu_tmp.sage, stu_tmp.sdept);
pos = NULL;
}
else
printf("The student is not exist!\n");
break;
case 5: //输出所有学生信息
ListTraverse(L, visit);
break;
case 6: //输出学生数量
printf("The student count is %d\n", ListLength(L));
break;
}
}
}