#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
typedef struct{
int num;
int lh;
int ssh;
char name[10];
int zw;
}DATA;
typedef struct node{
DATA data;
struct node *next;
}NODE;
void Write(NODE* head);
NODE* Read(NODE* head);
int FullList();
NODE* Create();
NODE* SeekList(NODE* head);
NODE* InsertList(NODE* head);
NODE* DeleteList(NODE* head);
NODE* ChangeList(NODE* head);
void AllList(NODE* head);
int main()
{
NODE *head;
head=(NODE*)malloc(sizeof(NODE));
head=NULL;
while(1){
switch(FullList()){
case 1:
head=Create();
break;
case 2:
head=Read(head);
break;
case 3:
Write(head);
break;
case 4:
head=InsertList(head);
break;
case 5:
head=SeekList(head);
break;
case 6:
head=DeleteList(head);
break;
case 7:
head=ChangeList(head);
break;
case 8:
AllList(head);
break;
case 9:
exit(0);
}
}
return 0;
}
void Write(NODE* head){
FILE *fp;
NODE *p;
p=head;
if((fp=fopen("input.txt","w"))==NULL){
printf("文件打开错误!\n");
exit(0);
}
p=p->next;
while(p){
if(fwrite(p,sizeof(NODE),1,fp)!=1){
printf("写入数据出错!\n");
fclose(fp);
exit(0);
}
p=p->next;
}
fclose(fp);
}
NODE* Read(NODE* head){
NODE *p,*s;
FILE *fp;
head=(NODE*)malloc(sizeof(NODE));
head->next=NULL;
if((fp=fopen("input.txt","r"))==NULL){
printf("文件打开失败!\n");
exit(0);
}
while(!feof(fp)){
if((p=(NODE*)malloc(sizeof(NODE)))==NULL){
printf("申请内存出错!\n");
fclose(fp);
exit(0);
}
if(fread(p,sizeof(NODE),1,fp)!=1){
printf("读取数据失败!\n");
free(p);
fclose(fp);
exit(0);
}
s->next=p;
s=p;
}
s->next=NULL;
fclose(fp);
return head;
}
int FullList(){
printf(" --------------------------|----------------|--------------------------\n");
printf(" --------------------------|学生宿舍管理系统|--------------------------\n");
printf(" --------------------------|----------------|--------------------------\n");
printf(" ----------------------------1.录入学生信息----------------------------\n");
printf(" ----------------------------2.读取学生信息----------------------------\n");
printf(" ----------------------------3.保存学生信息----------------------------\n");
printf(" ----------------------------4.插入学生信息----------------------------\n");
printf(" ----------------------------5.查询学生信息----------------------------\n");
printf(" ----------------------------6.删除学生信息----------------------------\n");
printf(" ----------------------------7.更改学生信息----------------------------\n");
printf(" ----------------------------8.查看全部信息----------------------------\n");
printf(" ------------------------------9.退出系统------------------------------\n");
printf("请输入序号:\n");
while(1){
int t;
scanf("%d", &t);
if(t>9||t<1)
printf("请重新输入\n");
else
return t;;
}
}
NODE* Create(){
NODE *head,*p,*s;
char t='x';
head=(NODE*)malloc(sizeof(NODE));
p=head->next=NULL;
p->next=NULL;
while(t=='n'){
printf("请输入学号 姓名 楼号 宿舍号 床号:\n");
p=(NODE*)malloc(sizeof(NODE));
scanf("%d%s%d%d%d",&p->data.num,p->data.name,&p->data.lh,&p->data.ssh,&p->data.zw);
printf("是否结束输入(y/n)");
scanf("%c",&t);
s->next=p;
s=p;
}
p->next=NULL;
return head;
}
NODE* SeekList(NODE* head){
int x,ans,numm;
char xmm[12];
NODE *p;
p=head->next;
scanf("%d",&ans);
printf("请输入1.姓名或2.学号:\n");
scanf("%d",&x);
if(x==1){
scanf("%s",xmm);
while(strcmp(xmm,p->data.name)!=0){
p=p->next;
}
printf("%d,%d,%d,%s,%d\n",p->data.num,p->data.lh,p->data.ssh,p->data.name,p->data.zw);
}else if(x==2){
scanf("%d",&numm);
while(numm!=p->data.num){
p=p->next;
}
printf("%d,%d,%d,%s,%d\n",p->data.num,p->data.lh,p->data.ssh,p->data.name,p->data.zw);
}else
printf("输入错误!\n");
return head;
}
NODE* InsertList(NODE* head){
NODE *s,*p;
int j=0;
p = head;
s = (NODE *)malloc(sizeof(NODE));
if (!s)
exit(0);
printf("请输入学号 楼号 宿舍号 姓名 座号:\n");
scanf("%d%d%d%s%d",&s->data.num,&s->data.lh,&s->data.ssh,s->data.name,&s->data.zw);
while(j<s->data.num-1 && p){
p = p->next;
j++;
}
if(p==NULL){
printf("无法插入数据!\n");
}else{
s->next = p->next;
p->next = s;
printf("数据插入成功!\n");
}
return head;
}
NODE* DeleteList(NODE* head){
int num;
NODE *p, *s;
printf("请输入要删除的数据(学号):\n");
s=head;
p=head->next;
scanf("%d",&num);
while (p&&num!=p->data.num) {
p=p->next;
s=s->next;
}
if(!p){
printf("无法删除数据!\n");
}
else{
s->next=p->next;
}
return head;
}
NODE* ChangeList(NODE* head){
NODE *p;
DATA y;
p = head->next;
printf("请输入您要更改的信息:学号,姓名,座号\n");
scanf("%d%s%d",&y.num,y.name,&y.zw);
while (p && p->data.num != y.num) {
p = p->next;
}
if (!p) {
printf("没有找到!\n");
} else {
memcpy(&p->data, &y, sizeof(DATA));
}
return head;
}
void AllList(NODE* head){
NODE *p;
p=head->next;
while(p) {
printf("学号:%d 姓名:%d 楼号:%d 宿舍号:%s 座号:%d\n",p->data.num,p->data.lh,p->data.ssh,p->data.name,p->data.zw);
p = p->next;
}
}
运行就出错,求大神。。
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
typedef struct{
int num;
int lh;
int ssh;
char name[10];
int zw;
}DATA;
typedef struct node{
DATA data;
struct node *next;
}NODE;
void Write(NODE* head);
NODE* Read(NODE* head);
int FullList();
NODE* Create();
NODE* SeekList(NODE* head);
NODE* InsertList(NODE* head);
NODE* DeleteList(NODE* head);
NODE* ChangeList(NODE* head);
void AllList(NODE* head);
int main()
{
NODE *head;
head=(NODE*)malloc(sizeof(NODE));
head=NULL;
while(1){
switch(FullList()){
case 1:
head=Create();
break;
case 2:
head=Read(head);
break;
case 3:
Write(head);
break;
case 4:
head=InsertList(head);
break;
case 5:
head=SeekList(head);
break;
case 6:
head=DeleteList(head);
break;
case 7:
head=ChangeList(head);
break;
case 8:
AllList(head);
break;
case 9:
exit(0);
}
}
return 0;
}
void Write(NODE* head){
FILE *fp;
NODE *p;
p=head;
if((fp=fopen("input.txt","w"))==NULL){
printf("文件打开错误!\n");
exit(0);
}
p=p->next;
while(p){
if(fwrite(p,sizeof(NODE),1,fp)!=1){
printf("写入数据出错!\n");
fclose(fp);
exit(0);
}
p=p->next;
}
fclose(fp);
}
NODE* Read(NODE* head){
NODE *p,*s;
FILE *fp;
head=(NODE*)malloc(sizeof(NODE));
head->next=NULL;
if((fp=fopen("input.txt","r"))==NULL){
printf("文件打开失败!\n");
exit(0);
}
while(!feof(fp)){
if((p=(NODE*)malloc(sizeof(NODE)))==NULL){
printf("申请内存出错!\n");
fclose(fp);
exit(0);
}
if(fread(p,sizeof(NODE),1,fp)!=1){
printf("读取数据失败!\n");
free(p);
fclose(fp);
exit(0);
}
s->next=p;
s=p;
}
s->next=NULL;
fclose(fp);
return head;
}
int FullList(){
printf(" --------------------------|----------------|--------------------------\n");
printf(" --------------------------|学生宿舍管理系统|--------------------------\n");
printf(" --------------------------|----------------|--------------------------\n");
printf(" ----------------------------1.录入学生信息----------------------------\n");
printf(" ----------------------------2.读取学生信息----------------------------\n");
printf(" ----------------------------3.保存学生信息----------------------------\n");
printf(" ----------------------------4.插入学生信息----------------------------\n");
printf(" ----------------------------5.查询学生信息----------------------------\n");
printf(" ----------------------------6.删除学生信息----------------------------\n");
printf(" ----------------------------7.更改学生信息----------------------------\n");
printf(" ----------------------------8.查看全部信息----------------------------\n");
printf(" ------------------------------9.退出系统------------------------------\n");
printf("请输入序号:\n");
while(1){
int t;
scanf("%d", &t);
if(t>9||t<1)
printf("请重新输入\n");
else
return t;;
}
}
NODE* Create(){
NODE *head,*p,*s;
char t='x';
head=(NODE*)malloc(sizeof(NODE));
p=head->next=NULL;
p->next=NULL;
while(t=='n'){
printf("请输入学号 姓名 楼号 宿舍号 床号:\n");
p=(NODE*)malloc(sizeof(NODE));
scanf("%d%s%d%d%d",&p->data.num,p->data.name,&p->data.lh,&p->data.ssh,&p->data.zw);
printf("是否结束输入(y/n)");
scanf("%c",&t);
s->next=p;
s=p;
}
p->next=NULL;
return head;
}
NODE* SeekList(NODE* head){
int x,ans,numm;
char xmm[12];
NODE *p;
p=head->next;
scanf("%d",&ans);
printf("请输入1.姓名或2.学号:\n");
scanf("%d",&x);
if(x==1){
scanf("%s",xmm);
while(strcmp(xmm,p->data.name)!=0){
p=p->next;
}
printf("%d,%d,%d,%s,%d\n",p->data.num,p->data.lh,p->data.ssh,p->data.name,p->data.zw);
}else if(x==2){
scanf("%d",&numm);
while(numm!=p->data.num){
p=p->next;
}
printf("%d,%d,%d,%s,%d\n",p->data.num,p->data.lh,p->data.ssh,p->data.name,p->data.zw);
}else
printf("输入错误!\n");
return head;
}
NODE* InsertList(NODE* head){
NODE *s,*p;
int j=0;
p = head;
s = (NODE *)malloc(sizeof(NODE));
if (!s)
exit(0);
printf("请输入学号 楼号 宿舍号 姓名 座号:\n");
scanf("%d%d%d%s%d",&s->data.num,&s->data.lh,&s->data.ssh,s->data.name,&s->data.zw);
while(j<s->data.num-1 && p){
p = p->next;
j++;
}
if(p==NULL){
printf("无法插入数据!\n");
}else{
s->next = p->next;
p->next = s;
printf("数据插入成功!\n");
}
return head;
}
NODE* DeleteList(NODE* head){
int num;
NODE *p, *s;
printf("请输入要删除的数据(学号):\n");
s=head;
p=head->next;
scanf("%d",&num);
while (p&&num!=p->data.num) {
p=p->next;
s=s->next;
}
if(!p){
printf("无法删除数据!\n");
}
else{
s->next=p->next;
}
return head;
}
NODE* ChangeList(NODE* head){
NODE *p;
DATA y;
p = head->next;
printf("请输入您要更改的信息:学号,姓名,座号\n");
scanf("%d%s%d",&y.num,y.name,&y.zw);
while (p && p->data.num != y.num) {
p = p->next;
}
if (!p) {
printf("没有找到!\n");
} else {
memcpy(&p->data, &y, sizeof(DATA));
}
return head;
}
void AllList(NODE* head){
NODE *p;
p=head->next;
while(p) {
printf("学号:%d 姓名:%d 楼号:%d 宿舍号:%s 座号:%d\n",p->data.num,p->data.lh,p->data.ssh,p->data.name,p->data.zw);
p = p->next;
}
}
运行就出错,求大神。。