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

二级 · 程序填空:删除链表结点

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

函数 del 删除链表中第一个值为 x 的结点,返回新的头指针。程序输出 2 3。请在【】处填入正确的内容。

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

struct node {
    int data;
    struct node *next;
};

/* 尾插法:用 a[0]~a[n-1] 建一条链表,返回头指针(上一关学过) */
struct node *create(int a[], int n)
{
    struct node *head = NULL, *tail = NULL, *p;
    int i;
    for (i = 0; i < n; i++) {
        p = (struct node *)malloc(sizeof(struct node));
        p->data = a[i];
        p->next = NULL;
        if (head == NULL)
            head = p;
        else
            tail->next = p;
        tail = p;
    }
    return head;
}

/* 输出整条链表;空链表输出「空」 */
void print_list(struct node *head)
{
    struct node *p;
    if (head == NULL)
        printf("空");
    for (p = head; p != NULL; p = p->next)
        printf("%d ", p->data);
    printf("\n");
}

/* 释放整条链表 */
void free_list(struct node *head)
{
    struct node *q;
    while (head != NULL) {
        q = head->next;
        free(head);
        head = q;
    }
}

struct node *del(struct node *head, int x)
{
    struct node *p, *q;
    if (head != NULL && head->data == x) {
        q = head;
        head = head->【0】;
        free(【1】);
        return head;
    }
    p = head;
    while (p != NULL && p->next != NULL && p->next->data != x)
        p = p->next;
    if (p != NULL && p->next != NULL) {
        q = p->next;
        p->next = 【2】;
        free(q);
    }
    return head;
}

int main(void)
{
    int a[3] = {1, 2, 3};
    struct node *head;
    head = create(a, 3);
    head = del(head, 1);
    print_list(head);
    free_list(head);
    return 0;
}
请登录后作答。
去登录
👩‍🏫
AI
💬 题目评论
🔒 登录并作答后可看讨论