#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 50
typedef struct Student {
int info;
char name[MAXSIZE];
int phone;
struct Student* next;
}stu,*s;
s student(int info = 0, char name[MAXSIZE] = 0,int phone=0) {
s node = new stu;
node->info = info;
if(name)for (int i = 0; i < MAXSIZE; i++)node->name[i] = name[i];
node->phone = phone;
node->next = NULL;
return node;
}
bool warn(s head) {
if (!head) {
printf("链表头指针为空,无法进行操作\n");
}
return head;
}
void add(s head,s node,int index=0) {
if (warn(head)) {
while (head && head->next && index--) {
head = head->next;
}
node->next = head->next;
head->next = node;
}
}
void del(s head, s node) {
if (warn(head)) {
while (head && head->next) {
if (head->next->info == node->info) {
s temp = head->next;
head->next = head->next->next;
delete temp;
printf("删除成功\n");
return;
}
head = head->next;
}
printf("查无此人\n");
}
}
void chg(s head, s node) {
if (warn(head)) {
while (head && head->next) {
if (head->next->info == node->info) {
printf("原来信息:\n");
printf("姓名:%s 学号:%d 手机号:%d\n", head->next->name, head->next->info, head->next->phone);
printf("修改为:\n");
printf("姓名:");
scanf("%s", head->next->name);
printf("学号:");
scanf("%d", &head->next->info);
printf("手机号:");
scanf("%d", &head->next->phone);
printf("修改成功\n");
return;
}
head = head->next;
}
printf("查无此人\n");
}
}
void srh(s head, s node) {
if (warn(head)) {
while (head && head->next) {
if (head->next->info == node->info) {
printf("姓名:%s 学号:%d 手机号:%d\n", head->next->name, head->next->info, head->next->phone);
return;
}
head = head->next;
}
printf("查无此人\n");
}
}
void show(s head) {
printf("show:\n");
if (warn(head)) {
head->info = 0;
printf("%10s %10s %10s\n", "name", "info", "phone");
while (head && head->next) {
head->info++;
printf("%10s %10d %10d\n", head->next->name, head->next->info, head->next->phone);
head = head->next;
}
}
printf("共有学生%d名\n", head->info);
printf("\n完毕\n");
}
int menu() {
int choice = 0;
while (true) {
printf("菜单\n");
printf("1、增加学生\n");
printf("2、删除学生\n");
printf("3、修改信息\n");
printf("4、查询信息\n");
printf("0、退出系统\n");
printf("5、全局打印\n");
printf("输入>");
scanf("%d", &choice);
system("cls");
if (choice >= 0 && choice <= 5)
return choice;
else
printf("错误的输入\n");
}
}
int mySys() {
s node;
s head = student();
int info = 0;
int n;
while (true) {
switch (menu()) {
case 0:
exit(0);
case 1:
printf("插入学生数量>");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
printf("添加第%d个学生\n", i + 1);
node = student();
printf("输入学号>");
scanf("%d", &node->info);
printf("输入姓名>");
scanf("%s", &node->name);
printf("输入手机号>");
scanf("%d", &node->phone);
printf("指定位置(无则填0)>");
scanf("%d", &info);
add(head, node,info);
}
break;
case 2:
printf("输入学号>");
scanf("%d", &info);
node = student(info);
del(head, node);
break;
case 3:
printf("输入学号>");
scanf("%d", &info);
node = student(info);
chg(head, node);
break;
case 4:
printf("输入学号>");
scanf("%d", &info);
node = student(info);
srh(head, node);
break;
case 5:
show(head);
break;
}
printf("输入回车继续工作\n");
getchar();
getchar();
system("cls");
}
}
int main(void) {
mySys();
return 0;
}
07-08
04-25
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交