C++ 学生信息录入系统(链表创建插入)
#include "stdio.h"
#include "malloc.h"
//定义链表结构
typedef struct student {
int num;
char name[300];
struct student *next;
}St;
//预声明
St* InsertSt();
//程序入口
int main() {
//创建链表头
St* head=NULL;// *p1, * p2, * p3;
//插入新链表元素并赋值
St* pl=head;
int tuul = 0;
while (1) {
St *k = InsertSt();
printf("请输入学生ID:\n");
scanf_s("%d", &k->num);
printf("请输入学生名字:\n");
scanf_s("%s",k->name,20);
if (pl != NULL)
{
pl->next = k;
}
else {
head = k;
}
pl = k;
void a();
tuul++;
printf("是否添加学生?1.不添加 2.添加\n");
int jud=0;
scanf_s("%d", &jud);
if (jud == 1) { printf("成功退出!\n"); break; }
else if (jud == 2) { printf("请继续添加新学生信息!\n"); }
else { printf("输入非法!退出程序..\n"); break; }
};
//获取链表长度
St* cx = head;
int len = 1;
while (1) {
if (cx->next != NULL){
printf("链表长度加1\n");
len++;
}
else {
printf("该链表存在:%d名学生信息!\n", len);
break;
};
cx = cx->next;
};
//释放内存
pl = NULL;
free(pl);
//循环读取链表内容
St *po=head;
int t = 0;
while (t < len) {
printf("学生id是:%d\n姓名是:%s\n", po->num, po->name);
po = po->next;
t++;
};
//fflush(stdout);
//释放内存
po = NULL;
free(po);
return 0;
};
//插入链表
St* InsertSt() {
St *p;
p = (St*)malloc(sizeof(St));
p->next = NULL;
return p;
};