And_Ii 2024-03-22 20:28 采纳率: 0%
浏览 0

链表综合问题PTA,急

本题要求实现多个函数完成对链表的各项基本操作:创建链表,按要求查找,在指定位置添加结点,删除结点,链表排序,输出链表等操作。
链表中为若干个学生的信息(学号、成绩),当输入学号为0时结束;查找并输出不及格的学生信息;新增一个学生信息到指定的学号前面;将指定学号的学生从链表中删除。 将链表中的学生信息按照分数降序排序。

函数接口定义:

Stu * Creatlink( );
void Printlink(Stu * head);
void Search(Stu * head);
Stu *Delnode(Stu * head,int n);
Stu * Addnode(Stu * head,int n);
Stu *Sortnode(Stu * head);

裁判测试程序样例:

#include <stdio.h>
#include <stdlib.h>
#define LEN sizeof(struct stu)
struct stu
{ 
  int num;
  float score;
  struct stu *next;
};
typedef struct stu Stu;

Stu *Creatlink();
void Printlink(Stu *head);
void Search(Stu *head);
Stu *Delnode(Stu *head,int n);
Stu *Addnode(Stu *head,int n);
Stu *Sortnode(Stu *head);

int main()
{ 
       Stu *head = NULL; 
       int num;
       head = Creatlink(); 
       if(head !=NULL)
             Printlink(head);
       else
       { 
        printf("Empty list\n"); 
              return 0;        
       }
       printf("The student no pass:\n"); 
       Search(head);
   
       scanf("%d",&num);
       head=Delnode(head,num);
       if(head !=NULL)
       {
               printf("After delete the student:\n"); 
           Printlink(head);
    }
    else
       { 
        printf("After delete:empty list\n"); 
              return 0;        
       }
   
       scanf("%d",&num);
       head=Addnode(head,num);
       printf("After add the student:\n");
       Printlink(head);
   
       head = Sortnode(head);
       printf("After sorted:\n");
       Printlink(head);
       return 0;
}
/* 请在这里填写答案 */

输入样例:
2,58
3,69
4,98
5,45
6,30
9,80
11,40
0,0
6
9
7,88
输出样例:
2,58
3,69
4,98
5,45
6,30
9,80
11,40
The student no pass:
2,58
5,45
6,30
11,40
After delete the student:
2,58
3,69
4,98
5,45
9,80
11,40
After add the student:
2,58
3,69
4,98
5,45
7,88
9,80
11,40
After sorted:
4,98
7,88
9,80
3,69
2,58
5,45
11,40
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB

  • 写回答

2条回答 默认 最新

  • 码农阿豪@新空间代码工作室 Java领域优质创作者 2024-03-22 20:28
    关注
    让阿豪来帮你解答,本回答参考chatgpt3.5编写提供,如果还有疑问可以评论或留言
    链表操作函数的代码实现如下:
    #include <stdio.h>
    #include <stdlib.h>
    #define LEN sizeof(struct stu)
    struct stu {
        int num;
        float score;
        struct stu* next;
    };
    typedef struct stu Stu;
    Stu* Creatlink();
    void Printlink(Stu* head);
    void Search(Stu* head);
    Stu* Delnode(Stu* head, int n);
    Stu* Addnode(Stu* head, int n);
    Stu* Sortnode(Stu* head);
    // 创建链表
    Stu* Creatlink() {
        Stu* head = NULL, * p1 = NULL, * p2 = NULL;
        p1 = (Stu*)malloc(LEN);
        if (p1 == NULL) return NULL;
        head = p2 = p1;
        scanf("%d,%f", &p1->num, &p1->score);
        while (p1->num) {
            p2->next = p1;
            p2 = p1;
            p1 = (Stu*)malloc(LEN);
            scanf("%d,%f", &p1->num, &p1->score);
        }
        p2->next = NULL;
        return head;
    }
    // 输出链表
    void Printlink(Stu* head) {
        Stu* p = head;
        while (p != NULL) {
            printf("%d,%.0f\n", p->num, p->score);
            p = p->next;
        }
    }
    // 查找不及格的学生
    void Search(Stu* head) {
        Stu* p = head;
        int flag = 0;
        while (p != NULL) {
            if (p->score < 60) {
                printf("%d,%.0f\n", p->num, p->score);
                flag = 1;
            }
            p = p->next;
        }
        if (!flag) printf("Not Found\n");
    }
    // 删除指定学号的学生
    Stu* Delnode(Stu* head, int n) {
        Stu* p1 = head, * p2 = head;
        while (p1 != NULL) {
            if (p1->num == n) {
                if (p1 == head)
                    head = p1->next;
                else
                    p2->next = p1->next;
                free(p1);
                printf("Deleted Successfully\n");
                break;
            }
            p2 = p1;
            p1 = p1->next;
        }
        if (p1 == NULL)
            printf("Not Found\n");
        return head;
    }
    // 在指定位置添加学生记录
    Stu* Addnode(Stu* head, int n) {
        Stu* p1 = head, * p2 = head, * pnew;
        pnew = (Stu*)malloc(LEN);
        pnew->next = NULL;
        scanf("%d,%f", &pnew->num, &pnew->score);
        while (p1 != NULL) {
            if (p1->num == n) {
                if (p1 == head) {
                    head = pnew;
                    pnew->next = p1;
                }
                else {
                    pnew->next = p1;
                    p2->next = pnew;
                }
                printf("Added Successfully\n");
                break;
            }
            p2 = p1;
            p1 = p1->next;
        }
        if (p1 == NULL)
            printf("Not Found\n");
        return head;
    }
    // 按成绩降序排序
    Stu* Sortnode(Stu* head) {
        Stu *p, *q, *tail = NULL;
        q = head;
        while (q != tail) {
            p = head;
            while (p->next != tail) {
                if (p->score < p->next->score) {
                    int tmp_num = p->num;
                    float tmp_score = p->score;
                    p->num = p->next->num;
                    p->score = p->next->score;
                    p->next->num = tmp_num;
                    p->next->score = tmp_score;
                }
                p = p->next;
            }
            tail = p;
        }
        return head;
    }
    

    主函数中的代码如下:

    int main(){
        Stu* head = NULL;
        int num;
        head = Creatlink();
        if (head != NULL)
            Printlink(head);
        else {
            printf("Empty list\n");
            return 0;
        }
        printf("The student no pass:\n");
        Search(head);
        scanf("%d", &num);
        head = Delnode(head, num);
        if (head != NULL)
            Printlink(head);
        else {
            printf("After delete: empty list\n");
            return 0;
        }
        scanf("%d", &num);
        head = Addnode(head, num);
        Printlink(head);
        head = Sortnode(head);
        printf("After sorted:\n");
        Printlink(head);
        return 0;
    }
    
    评论

报告相同问题?

问题事件

  • 创建了问题 3月22日

悬赏问题

  • ¥15 机器学习预测遇到的目标函数问题
  • ¥15 python的EOFError该怎么解决?
  • ¥15 Fluent,液体进入旋转区域体积分数不连续
  • ¥15 java linux下将docx文件转pdf
  • ¥15 maven无法下载依赖包
  • ¥15 关于pycharm, Callable[[str],bool]作为方法参数使用时, lambda 类型不提示问题
  • ¥15 layui数据重载无效
  • ¥15 寻找了解qq家园纵横四海的程序猿。
  • ¥15 optisystem
  • ¥15 VB.NET画图时的撤销编程