怎么做
  1. 题面里的虚线方框就是要填的空
  2. 点候选项,它会填进当前高亮的那个空
  3. 点已填好的空,可以把选项取回来重选
  4. 全部填满后「提交」才会亮起
卡住了就点右下角的 👩‍🏫 问 AI 老师,她会给提示但不直接给答案。

二级 · 程序填空:带头结点链表排序

👁️ 31 人浏览 💬 0 人评论 ❤️ 添加收藏

函数 fun 把带头结点链表中的数据按从小到大排序(只交换 data,不动指针)。程序输出 1 3 5 8。请在【】处填入正确的内容。

#include <stdio.h>
#include <stdlib.h>

typedef struct list {
    int data;
    struct list *next;
} SLIST;

/* 用 a[0]~a[n-1] 建一条带头结点的链表,返回头结点的地址 */
SLIST *creat(int a[], int n)
{
    SLIST *h, *p, *q;
    int i;
    h = p = (SLIST *)malloc(sizeof(SLIST));
    h->data = 0;
    for (i = 0; i < n; i++) {
        q = (SLIST *)malloc(sizeof(SLIST));
        q->data = a[i];
        p->next = q;
        p = q;
    }
    p->next = NULL;
    return h;
}

/* 输出所有数据结点;没有数据结点输出「空」 */
void outlist(SLIST *h)
{
    SLIST *p = h->next;
    if (p == NULL)
        printf("空");
    while (p != NULL) {
        printf("%d ", p->data);
        p = p->next;
    }
    printf("\n");
}

/* 释放整条链表(含头结点) */
void freelist(SLIST *h)
{
    SLIST *q;
    while (h != NULL) {
        q = h->next;
        free(h);
        h = q;
    }
}

void fun(SLIST *h)
{
    SLIST *p, *q;
    int t;
    p = h->【0】;
    while (p != NULL) {
        q = p->【1】;
        while (q != NULL) {
            if (p->data 【2】 q->data) {
                t = p->data;
                p->data = q->data;
                q->data = t;
            }
            q = q->next;
        }
        p = p->next;
    }
}

int main(void)
{
    int a[4] = {5, 1, 8, 3};
    SLIST *h;
    h = creat(a, 4);
    fun(h);
    outlist(h);
    freelist(h);
    return 0;
}
请登录后作答。
去登录
👩‍🏫
AI
💬 题目评论
🔒 登录并作答后可看讨论