创建一个 单向链表 来记录学生信息,人数3--5人;链表结点为结构变量,结构的要求如下:
struct stu_info
{
char stu_num[10]; //学号
char stu_name[8]; //姓名
char stu_sex[2]; //性别
int stu_score //成绩
struct stu_info *next;
};
程序设计要求:
(1)插入新的学生信息(插入节点的位置可任意指定)
(2)删除指定的学生信息
(3)根据学号查询并显示查询到的学生信息
(4)以上三项任务分别自定义函数实现,执行后显示执行结果
(5)程序运行后要求可以循环执行前三项操作,直到选择退出时结束程序
程序:
#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct stu_info)
struct stu_info
{
char stu_num[10];
char stu_name[8];
char stu_sex[2];
int stu_score;
struct stu_info *next;
};
int n,a;
int main()
{
struct stu_info * creat();
struct stu_info * del(struct stu_info ,char);
struct stu_infoinsert(struct stu_info*,struct stu_info*);
void print(struct stu_info*);
struct stu_info *head,stu;
char c;
char i='0';
printf("请输入学生总数:\n");
scanf("%d",&a);
printf("输入学生信息:\n");
head=creat();
printf("请输入要删除信息的学生的学号:\n");
scanf("%c",&c);
while(c!='0')
{
head=del(head,c);
printf("请输入要删除信息的学生的学号:\n");
scanf("%c",&c);
}
printf("请输入要插入的学生信息:");
stu=(struct stu_info)malloc(LEN);
scanf("%s,%s,%s,%d",&stu->stu_num,&stu->stu_name,&stu->stu_sex,&stu->stu_score);
while(stu->stu_num!='0')
{
head=insert(head,stu);
printf("请输入要删除信息的学生的学号:\n");
scanf("%c",&c);
}
print(head);
free(head);
printf("若停止进行操作请输入T,若继续进行操作请输入0\n");
scanf("%c",&i);
return 0;
}
struct stu_info *creat()
{
struct stu_info *head;
struct stu_info *p1,*p2;
n=0;
p1=p2=(struct stu_info )malloc(LEN);
scanf("%s,%s,%s,%d",&p1->stu_num,&p1->stu_name,&p1->stu_sex,&p1->stu_score);
head=NULL;
while(p1->stu_num!=0&&n<a-1)
{
n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct stu_info)malloc(LEN);
scanf("%s,%s,%s,%d",&p1->stu_num,&p1->stu_name,&p1->stu_sex,&p1->stu_score);
}
p2->next=NULL;
return(head);
}
struct stu_infodel(struct stu_infohead,char num)
{
struct stu_info *p1,*p2;
if(head==NULL)
{
printf("\n链表为空\n");
return(head);
}
p1=head;
while(num!=p1->stu_num&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(num==p1->stu_num)
{
if(p1==head)head=p1->next;
else p2->next=p1->next;
那你得说一下你哪里出错了呀!
不知道你这个问题是否已经解决, 如果还没有解决的话: