数据结构之学生信息管理(C语言)
1.顺序表
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define MAXSIZE 100
typedef struct {
char no[20];
char name[30];
float score;
} Student;
typedef struct {
Student *elem;
int length;
int caption;
} Sqlist;
int Init(Sqlist &L) {
L.elem = (Student*)malloc(sizeof(Student));
if (!L.elem) exit(OVERFLOW);
L.length = 0;
L.caption = MAXSIZE;
return OK;
}
void Check(Sqlist L) {
if(L.length == 0) {
printf("当前顺序表为空!");
} else {
printf("当前顺序表不为空");
}
}
int Get(Sqlist L, int i, Student &m) {
if (i<0 || i>L.length)
return ERROR;
m = L.elem[i - 1];
return OK;
}
void Local(Sqlist L, char a[20]) {
int m =-1;
for (int i = 0; i < L.length; i++) {
if (strcmp(L.elem[i].name, a) == 0) {
m = i;
printf("学号:%s\t成绩:%.1f",L.elem[i].no,L.elem[i].score);
}
}
if(m==-1) {
printf("该学生不存在!");
}
}
int Creat(Sqlist &L) {
int n;
printf("请输入您要初始化的学生信息(学号,姓名,成绩)顺序表的长度:");
scanf("%d", &n);
int i;
for (i = 0; i < n; i++) {
printf("请输入第 %d个学生的学号、名字、成绩:",i+1);
scanf("%s %s %f", &L.elem[i].no, &L.elem[i].name, &L.elem[i].score);
}
L.length = i;
return OK;
}
int Dele(Sqlist &L, int i, Student &e) {
if (i<0 || i>L.length)
return ERROR;
e = L.elem[i - 1];
for (int j = i - 1; j < L.length; j++) {
L.elem[j] = L.elem[j + 1];
}
--L.length;
return OK;
}
int Insert(Sqlist &L, int i, Student e) {
if (i<0 || i>L.length)
return ERROR;
for(int j = L.length; j >=i; j--)
L.elem[j] = L.elem[j-1];
L.elem[i - 1] = e;
++L.length;
return OK;
}
void Print(Sqlist L) {
printf("=================================================\n");
printf("学号\t姓名\t成绩\n");
for (int i = 0; i < L.length; i++) {
printf("%-5s %-5s %.1f\n", L.elem[i].no, L.elem[i].name, L.elem[i].score);
}
printf("=================================================");
}
int main() {
Sqlist S;
Student e, k;
int j, i, m;
int loop = 1;
char a[20];
Init(S);
system("color f0");
do {
printf("\n ┌─┐ ─┐ ");
printf(" ★☆★☆★☆★☆★☆★☆★☆★\n");
printf(" │※│ /※/ ");
printf(" ☆ ☆\n");
printf(" │※│/※/ ");
printf(" ★ 1.初始化 ★\n");
printf(" │※ /※/─┬─┐ ");
printf(" ☆ 2.检查顺序表是否为空 ☆\n");
printf(" │※│※|※│※│ ");
printf(" ★ 3.遍历顺序表 ★\n");
printf("┌┴─┴─┐-┘─┘ ");
printf(" ☆ 4.查询(位置) ☆\n");
printf(" ☆ 5.查询(名字) ☆\n");
printf("│※※※※│※※※│");
printf(" ★ 6.插入 ★\n");
printf("│※┌──┘※※※│");
printf(" ☆ 7.删除 ☆\n");
printf("└┐※※※※※※┌┘");
printf(" ★ 8.学生个数 ★\n");
printf(" ★ 9.退出 ★\n");
printf(" └┐※※※※┌┘ ");
printf(" ☆ ☆\n");
printf(" │※※※※│ ");
printf(" ★☆★☆★☆★☆★☆★☆★☆★\n");
printf("█▆ ▅ ▇ ▆ ▅ ▄▃ ▄ ▅ ▆ ▄ ▅ ▄ ▃ ▂ ▃ ▄ ▆ ▅ ▇ ▆ ▄ ▅█ ▇ ▆ ▄ ▂\n");
printf("======================\n");
printf(" 请输入您要进行的操作:\n");
printf("======================\n");
scanf("%d",&j);
switch (j) {
case 1:
Creat(S);
break;
case 2:
Check(S);
break;
case 3:
Print(S);
break;
case 4:
printf("请输入要查询的位置:");
int n;
scanf("%d", &n);
Get(S, n, k);
printf("%-5s %-5s %.1f\n", k.no, k.name, k.score);
break;
case 5:
printf("请输入要查找学生的名字:");
scanf("%s", a);
Local(S,a);
break;
case 6:
printf("请依次输入要插入学生的学号、名字、成绩、位置:\n");
scanf("%s%s%f%d", &e.no, &e.name, &e.score, &i);
Insert(S, i, e);
break;
case 7:
printf("请输入要删除学生的位置:");
int d;
Student q;
scanf("%d", &d);
Dele(S, d, q);
printf("您删除的学生是:%s %s %f\n", q.no, q.name, q.score);
break;
case 8:
printf("学生个数:%d",S.length);
break;
case 9:
printf("你退出了系统.....");
loop=0;
}
printf("\n\n");
} while(loop);
}
2.链表
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<windows.h>
#define OK 1
#define ERROR 0
#define Maxlen 100
struct Data {
int studentID;
char studentName[20];
double studentScore;
};
typedef Data DataType;
typedef struct StudentList {
DataType data;
struct StudentList *next;
};
StudentList * InitList(StudentList *h) {
h=(StudentList *)malloc(sizeof(StudentList));
if(h==NULL) {
return 0;
}
h->next=NULL;
return h;
}
StudentList * InputList(StudentList *h) {
StudentList *s,*r;
int num;
r=h;
printf("请输入您要初始化的学生信息(学号,姓名,成绩)链表的长度:");
scanf("%d",&num);
for(int i=0; i<num; i++) {
printf("请输入第%d个学生信息:\n",i+1);
s=(StudentList*)malloc(sizeof(StudentList));
printf("请输入学生学号:\n");
scanf("%d",&s->data.studentID);
printf("请输入学生姓名:\n");
scanf("%s",&s->data.studentName);
printf("请输入学生成绩:\n");
scanf("%lf",&s->data.studentScore);
r->next=s;
r=s;
}
r->next=NULL;
printf("创建成功!\n");
return h;
}
int ListEmpty(StudentList *h) {
if(h->next==NULL) {
return 1;
} else {
return 0;
}
}
void OutputList(StudentList *L) {
printf("=================================================\n");
printf("学号\t姓名\t成绩\n");
StudentList *p;
p=L->next;
while(p!=NULL) {
printf("%d\t%s\t%.2lf\n", p->data.studentID, p->data.studentName, p->data.studentScore);
p=p->next;
}
printf("=================================================\n");
}
int InsertList(StudentList *L) {
StudentList *p,*s;
int num,j=0;
p=L->next;
printf("请输入新增学生的位置:\n");
scanf("%d",&num);
while(p->next!=NULL&&j<num-1) {
p=p->next;
j++;
}
if(j!=num-1) {
printf("插入位置不存在\n");
return 0;
}
s=(StudentList*)malloc(sizeof(StudentList));
printf("请输入学生学号:\n");
scanf("%d",&s->data.studentID);
printf("请输入学生姓名:\n");
scanf("%s",&s->data.studentName);
printf("请输入学生成绩:\n");
scanf("%lf",&s->data.studentScore);
s->next=p->next;
p->next=s;
printf("新增成功\n");
return 1;
}
int DeleteList(StudentList *L) {
StudentList *r,*pre;
int num,j=0;
r=L->next;
printf("请输入删除的学生学号:\n");
scanf("%d",&num);
while(r!=NULL) {
if(r->data.studentID==num) {
pre->next=r->next;
free(r);
printf("删除成功\n");
return 1;
}
pre=r;
r=r->next;
}
return 0;
}
int SelectList(StudentList *L) {
StudentList *r,*pre;
int num;
r=L->next;
printf("请输入查找学生的位置:\n");
scanf("%d",&num);
while(r!=NULL) {
if(r->data.studentID==num) {
printf("学号\t姓名\t成绩\n");
printf("%d\t%s\t%.2lf\n", r->data.studentID, r->data.studentName, r->data.studentScore);
return 1;
}
r=r->next;
}
printf("链表中不存在此学生!\n");
return 0;
}
int SelectList2(StudentList *L) {
StudentList *r,*pre;
char name[20];
r=L->next;
printf("请输入查找学生的名字:\n");
scanf("%s",&name);
while(r!=NULL) {
if(strcmp(r->data.studentName,name)==0) {
printf("学号\t姓名\t成绩\n");
printf("%d\t%s\t%.2lf\n", r->data.studentID, r->data.studentName, r->data.studentScore);
return 1;
}
r=r->next;
}
printf("不存在此学生!\n");
return 0;
}
int main() {
StudentList *h;
StudentList *L=NULL;
DataType e;
int n,loop=1;
int count = 0;
h=InitList(h);
system("color f0");
do {
printf("\n ┌─┐ ─┐ ");
printf(" ★☆★☆★☆★☆★☆★☆★☆★\n");
printf(" │※│ /※/ ");
printf(" ☆ ☆\n");
printf(" │※│/※/ ");
printf(" ★ 1.初始化 ★\n");
printf(" │※ /※/─┬─┐ ");
printf(" ☆ 2.检查链表是否为空 ☆\n");
printf(" │※│※|※│※│ ");
printf(" ★ 3.遍历链表 ★\n");
printf("┌┴─┴─┐-┘─┘ ");
printf(" ☆ 4.查询(位置) ☆\n");
printf(" ☆ 5.查询(名字) ☆\n");
printf("│※※※※│※※※│");
printf(" ★ 6.插入 ★\n");
printf("│※┌──┘※※※│");
printf(" ☆ 7.删除 ☆\n");
printf("└┐※※※※※※┌┘");
printf(" ★ 8.学生个数 ★\n");
printf(" ★ 9.退出 ★\n");
printf(" └┐※※※※┌┘ ");
printf(" ☆ ☆\n");
printf(" │※※※※│ ");
printf(" ★☆★☆★☆★☆★☆★☆★☆★\n");
printf("█▆ ▅ ▇ ▆ ▅ ▄▃ ▄ ▅ ▆ ▄ ▅ ▄ ▃ ▂ ▃ ▄ ▆ ▅ ▇ ▆ ▄ ▅█ ▇ ▆ ▄ ▂\n");
printf("======================\n");
printf(" 请输入您要进行的操作:\n");
printf("======================\n");
scanf("%d",&n);
switch(n) {
case 1:
L=InputList(h);
break;
case 2:
if(ListEmpty(h)==1) {
printf("链表为空!");
} else {
printf("链表不为空");
}
break;
case 3:
OutputList(L);
break;
case 4:
SelectList(L);
break;
case 5:
SelectList2(L);
break;
case 6:
InsertList(L);
break;
break;
case 7:
DeleteList(L);
break;
case 8:
StudentList *p;
p=L->next;
while(p!=NULL) {
count++;
p=p->next;
}
printf("学生个数:%d",count);
break;
case 9:
loop=0;
printf("你退出了系统......");
break;
}
printf("\n\n");
} while(loop);
}