weixin_37817412 2017-03-15 05:22 采纳率: 0%
浏览 686

链表问题,大神求助,帮忙修改错误

#include
#include
typedef struct student {
int score[3];
char name[10];
float averagescore;

struct student *next;

}STU;
STU creat(int n) {
STU *head, *p, *b;
int i;
head = (STU
)malloc(sizeof(STU));
p = head;
if (head != NULL) {
printf("please input the name,score\n");
scanf("%s%d%d%d", head->name, &head->score[0],& head->score[1], &head->score[2]);
head->averagescore = (head->score[0] + head->score[1] +head->score[2])/3;
head->next = NULL;

}
else {
    printf("Application for memory faliure\n");
    exit(1);

}
for (i = 1; i < n; i++) {
    b = (STU *)malloc(sizeof(STU));
    while (b != NULL)
    {
        printf("please input name,score\n");
        scanf("%s%d%d%d", b->name, &b->score[0], &b->score[1], &b->score[2]);
        b->averagescore = (b->score[0] + b->score[1] + b->score[2]) / 3;
        p->next = b;
        p = b;

    }
}
b = (STU *)malloc(sizeof(STU));
b->next = NULL;

}//创建链表
void insert(STU *p) {
STU *pnew;

pnew = (STU *)malloc(sizeof(STU));
if (pnew != NULL);
{
    printf("please input the number and name,score\n");
    scanf("%s%d%d%d", pnew->name, &pnew->score[0], &pnew->score[1], &pnew->score[2]);
    pnew->averagescore = (pnew->score[0] + pnew->score[1] +pnew->score[2]) / 3;


}
while (p != NULL)
{
    p = p->next;

}
if (p == NULL)
{
    p->next = pnew;
    pnew->next = NULL;


}

}//插入链表
void deletinquiry(STU *p,int c){
char del[10];
STU *d;
printf("please input the delete name \n");
scanf("%s", del);
while (p != NULL) {
d = p;
p = p->next;

    if (p->name == del&&c == 0) {
        d->next = p->next;
        free(p);
    }
    else if (p->name == del&&c == 1)
    {
        printf("Student's information is %s %d %d %d %f", p->name, p->score[0], p->score[1], p->score[2], p->averagescore);

    }

}

}

//删除和查询
void selectsort(STU *l) {
STU *p, *q, *small,*temp;
for (p = l->next; p->next != NULL; p = p->next)//遍历节点,并每次移动一个节点
{
small = p;
for (q = p->next; q->next != NULL; q = q->next)//遍历节点,并交换最小值
{
if (q->averagescore < small->averagescore)
{
small = q;

        }

    }
    if (small != p)
    {
        temp = p;
        p = small;
        small = temp;


    }
}

}//链表选择排序
void print(STU *p) {
while (p != NULL)
{
printf("%s %d %d %d %f\n", p->name, p->score[0], p->score[1], p->score[2], &p->averagescore);

}

}
int main(void) {
STU *head;
int k;

printf("please input the numbers of student\n");
scanf("%d", &k);
head = creat(k);
print(head);
insert(head);
print(head);
k = 0;
deletinquiry(head, k);
print(head);
k = 1;
deletinquiry(head, k);
selectsort(head);
print(head);

}

  • 写回答

2条回答 默认 最新

  • shen_wei 2017-03-15 07:01
    关注

    http://blog.csdn.net/hopeyouknow/article/details/6677974

    先学习对比,理解。。然后看自己代码。。。

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题