程序设计作业的一个问题,有关链表和指针(已解决)

程序设计作业的一个问题,有关链表和指针

程序设计作业的一个问题,有关链表和指针

已给出主函数和fun.c,要设计函数

#include <stdio.h>
#include "fun.h"

int main(void) {
    int n, num;
    scanf("%d", &n);

    struct Node* head = NULL;
    while (n--) {
        scanf("%d", &num);
        insert(&head, num);
    }
    print_linklist(head);
    delete_linklist(head);
}
#include <stdlib.h> 
struct Node {
    struct Node* next;
    int value;
};

void insert(struct Node** head, int num);

void print_linklist(struct Node* head);

void delete_linklist(struct Node* head);

以下是我写的函数的设计

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

void insert(struct Node** head, int num)
{
    struct Node *l;
    struct Node *p;
    l = *head;
    if(l -> next == NULL) (在这里提示出了问题)
    {
        l -> next -> value = num;
    }
    else
    {
        if(num >= l -> value)
        {
            l = l -> next;
            l -> value = num;
        }
        else
        {
            p = *head;
            while(num > p -> next -> value)
            {
                p = p -> next;
            }
            struct Node *m;
            struct Node *n;
            n = p -> next;
            m = p;
            m -> next -> value = num;
            m -> next -> next = n;
        }
    }
}

void print_linklist(struct Node* head)
{
    while(head -> next != NULL)
    {
        printf("%d", head -> next -> value);
    }
}

void delete_linklist(struct Node* head)
{
    struct Node *l;
    while(head -> next != NULL)
    {
        l = head -> next;
        free(head);
        head = l;
    }
}

出了一些问题,搞了好久还是不知道为什么,请教一下各位大神!

长日将尽
最佳答案

你的打印函数 print_linklist 也有问题,你直接打印 head-next 跳过了第一个节点,而且你没有在循环中让 head 向下一个节点前进,如果前面的插入函数没问题,这个循环就会是无限循环,一直打印。

1年前 评论
vanshit (楼主) 1年前
讨论数量: 8
长日将尽

你的打印函数 print_linklist 也有问题,你直接打印 head-next 跳过了第一个节点,而且你没有在循环中让 head 向下一个节点前进,如果前面的插入函数没问题,这个循环就会是无限循环,一直打印。

1年前 评论
vanshit (楼主) 1年前
长日将尽

你错了是因为你假定所有节点都存在。main 函数提供的是链表节点的指针,而且初始值为 NULL,因此,所有的要插入的节点需要你手动分配空间。也就是说,这个最终的链表是要你自己构造出来的。节点指针为 NULL 的情况下,对节点进行访问和对 value 赋值的操作都是错误的空指针操作。

1年前 评论

我修改了一下 void insert(struct Node* head, int num) { struct Node *l = (struct Node *)malloc(sizeof(struct Node)); struct Node *p; while(head == NULL) { l -> value = num; *head = l; return; } if(num >= l -> value) { l = l -> next; l -> value = num;(这里显示有问题是为什么啊) } else { p = *head; while(num > p -> next -> value) { p = p -> next; } struct Node *m; struct Node *n; n = p -> next; m = p; m -> next -> value = num; m -> next -> next = n; } }

1年前 评论
长日将尽 1年前
长日将尽
void insert(struct Node **head, int num) {
  struct Node *l = (struct Node *)malloc(sizeof(struct Node));
  struct Node *p;
  while (head == NULL) {
    l->value = num;
    *head = l;
    return;
  }
  if (num >= l->value) {
    l = l->next;
    l->value = num; // (这里显示有问题是为什么啊)

你依然没有保证对 l的访问是合法的,从思路上来说,应该先寻找合适的插入位置,然后构造一个新的链表节点插入进去。你的 while 循环只在最初传入 insert 的参数 headNULL 的时候执行了一次,插入第二个数字的时候,这个循环就执行不到了,因为 head 已经不是空链表了。而访问 l->value 的结果是为定义的整数

1年前 评论
vanshit (楼主) 1年前
长日将尽 (作者) 1年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!