单链表求助
#include <stdio.h>#include <stdlib.h>
#include <string.h>
#include <conio.h>
typedef struct Student
{
char name;
char num;
float math;
float computer;
struct Student *next;
}People;
People *head;
void addstudent(People **buffer);
void delstudent();
void browsestudent();
void updatestudent();
void inputstudent();
void releasestudent();
void savestudent();
void loadstudent();
void meau();
void meau()
{
printf("\t-----------meau----------\n");
printf("\t| 1-add2-del3-update|\n");
printf("\t| 4-browse 5-save|\n");
printf("\t| 6-load 7-out|\n");
printf("\t-------------------------\n\n\n");
}
void addstudent(People **buffer) //新建节点,作用链接单个结构
{
People *temp, *star; //定义头
star = head->next;
if(star != NULL)
{
temp = star;
star = *buffer;
(*buffer)->next = temp;
}
else
{
star = *buffer;
(*buffer)->next = NULL;
}
}
void inputstudent()
{
People *buffer;
buffer = (People *)malloc(sizeof(People));
if(buffer == NULL)
{
printf("申请内存失败!\n");
exit(1);
}
printf("请输入学生姓名:");
scanf("%s", buffer->name);
printf("请输入学号:");
scanf("%s", buffer->num);
printf("请输入数学成绩:");
scanf("%f", &buffer->math);
printf("请输入计算机成绩:");
scanf("%f", &buffer->computer);
addstudent(&buffer);
}
void delstudent()
{
People *pb, *pf;
char str;
printf("请输入要删除的学生姓名:");
scanf("%s", str);
pb = head->next;
while(strcmp(str, pb->name) && pb->next != NULL) //循环退出条件是链表结束或者找到要删除的节点
{
pb = pf;
pb = pb->next;
}
if(strcmp(str, pb->name) == 0)
{
if(pb == head->next)//找到的节点是头结点
{
head->next = (head->next)->next;//指向第二个节点
}
else
{
pf->next = pb->next;
}
free(pb);
}
else
{
printf("查无此人!\n");
return ;
}
}
void updatestudent()
{
People* star = head->next;
char str;
int op;
float new_math;
float new_computer;
if(star == NULL)
{
printf("此链表为空!\n");
return ;
}
printf("请输入要更新信息的学生姓名:");
scanf("%s", str);
while(star != NULL)
{
if(strcmp(str, star->name) == 0)
{
printf("请输入要更新的内容(1-math,2-computer):");
scanf("%d", &op);
switch(op)
{
case 1: {printf("请输入新的数学成绩:");scanf("%f", &star->math);} break;
case 2: {printf("请输入新的计算机成绩:");scanf("%f", &star->computer);} break;
}
}
star = star->next;
}
}
void browsestudent()
{
People *star = head->next;
if(star == NULL)
{
printf("此链表为空!\n");
return ;
}
printf("姓名\t 学号\t\t数学\t计算机\n");
while(star != NULL)
{
printf("%s %s\t%.1f\t%.1f\n", star->name, star->num, star->math, star->computer);
star = star->next;
}
}
void savestudent()
{
People *star;
star = head->next;
FILE *fp;
if((fp = fopen("date.txt", "w")) == NULL)
{
printf("新建文件失败!\n");
}
while(star != NULL)
{
fprintf(fp, "%s %s %f %f\n", star->name, star->num, star->math, star->computer);
star = star->next;
}
fclose(fp);
}
void loadstudent()
{
FILE *fp;
People *buffer;
if((fp = fopen("date.txt", "r")) == NULL)
{
printf("打开文件错误!");
exit(1);
}
while(!feof(fp))
{
buffer = (People *)malloc(sizeof(People));
fscanf(fp, "%s%s%f%f", buffer->name, buffer->num, &buffer->math, &buffer->computer);
addstudent(&buffer);
}
printf("加载完成!\n");
fclose(fp);
}
void releasestudent()
{
People *temp, *star;
star = head->next;
while(star != NULL)
{
temp = star;
star = star->next;
free(temp);
}
}
int main(void)
{
head = (People *)malloc(sizeof(People));
head->next = NULL;
int op;
meau();
do
{
printf("请输入要执行的命令:");
scanf("%d", &op);
switch(op)
{
case 1: inputstudent(); break;
case 2: delstudent(); break;
case 3: updatestudent(); break;
case 4: browsestudent(); break;
case 5: savestudent(); break;
case 6: loadstudent(); break;
case 7: exit(1);
}
}while(1);
releasestudent();
return 0;
}
为什么我的链表连接不上,无法打印出来,求指点 本帖最后由 上善若水··· 于 2020-5-11 18:00 编辑
你的链表设计的有问题:首先在add的时候,将链表的next指针赋值为null
void addstudent(People **buffer) //新建节点,作用链接单个结构
{
...
(*buffer)->next = NULL;
}
在browsestudent的时候,直接使用的还是next,这时的next是为null的。
void browsestudent()
{
People *star = head->next;
if(star == NULL)
{
printf("此链表为空!\n");
return ;
}
}
所以会打印此链表为空···
最简单的做法是将链表头保存下来,查找的时候,直接用这个头链表对象去遍历。
页:
[1]