我的代码:
/*
**利用链表实现一个简单的学校教职工信息管理系统:
*(1)教职工信息包括工号、姓名、性别、职称、学位、职务、毕业学校和所学专业等;
*(2)系统的主要功能包括:教职工信息的创建、输出教职工信息、查询教职工信息、增加教职工信息、删除教职工信息。
*
*/
#include<stdio.h>
#include<stdlib.h>
/*
*定义一个“教师”结构体
*/
struct teacher{
/*
*数据域
*/
char teacher_num[6];
char teacher_name[6];
char sex;
char job_title[20];//职称
char degree[20];//学位
char duty[20];//职务
char graduation_school[20];//毕业学校
char major[15];//专业
/*
*指针域
*/
struct teacher* next;
};
typedef struct teacher* tList;
typedef struct teacher tNode;
/*
*构造一个空链表
*/
int InitList(tList L){
//malloc 函数返回的是一个地址
L=(tNode*)malloc(sizeof(tNode));//创建头节点并设置头结点
L->next=NULL;//首元结点设为空
return 1;
}
/*
*
*/
int add_teacher_List(tList L){
tNode *t=(tNode*)malloc(sizeof(tNode));//创建一个节点
printf("请输入教职工号:");
scanf("%s",t->teacher_num);
printf("请输入教师姓名:");
scanf("%s",t->teacher_name);
// t.teacher_num="123";
// t.teacher_name="song";
//
t->next=L->next;//////////////////、、、。。。,,,,,问题所在行
L->next=t;//头插
// printf("%s",L->next->teacher_num);
// printf("%s",t.teacher_name);
return 1;
}
/*
*trouble
*/
void scan(struct teacher *L){
struct teacher *p=L->next;//让p指针指
//循环遍历链表L,输出数据
while(p!=NULL){
printf("教师姓名:%s ",p->teacher_name);
printf("教师工号:%s ",p->teacher_num);
p=p->next;
}
}
int main(){
tList teaList;//创建一个头指针即链表
InitList(teaList);//初始化链表,参数是头指针
printf("1 增加信息 2删除信息 3查询信息 4查看信息 5退出\n");
while(1){
int choice;
scanf("%d",&choice);
switch(choice){
case 1:add_teacher_List(teaList);
break;
case 2:
break;
case 3:
break;
case 4:scan(teaList);
break;
case 5:
return 0;
}
}
}