【问题标题】:linked list c programming error by inserting new element通过插入新元素的链表c编程错误
【发布时间】:2019-04-22 03:16:04
【问题描述】:

我正在尝试插入一个元素,但它收到错误“进程完成,退出代码 11”

struct node {
    int key;
    struct node *next;
};
struct node* init(){
    struct node *head =NULL;
    return head;
}
void create(struct node * head,int num) {
    struct node * tmp = head;
    struct node * prev = NULL;
    struct node* new = malloc(sizeof(struct node));
    new->key = num;
    prev = tmp;
    tmp = tmp->next;
    while(tmp!= NULL && tmp->key < num){
        prev = tmp;
        tmp = tmp->next;
    }
    new->next = tmp;
    prev->next = new;
    if (tmp== NULL)
        head=tmp;
    }
int main() {
    int num;
    struct node* head;
    head=init()
    printf("Enter data:");
    scanf("%d",&num);
    create(head,num);
}

我正在尝试将一个元素插入到链表中,并且该元素应该同时排序和输入。有人可以告诉我错误是什么吗?我似乎无法找出错误。

【问题讨论】:

  • 您是否尝试在调试器下运行您的代码?在哪一行出现错误?
  • 一个节点不是列表。列表不是节点。
  • tmp = tmp->next;我在这里遇到错误
  • 我该如何纠正它?
  • @GeorgeJose 抱歉,但由于您的代码中没有列表,因此没有简单的解决方法。

标签: c linked-list


【解决方案1】:

不清楚你的功能create()

void create(struct node * head, int num) {
  struct node * tmp = head;
  struct node * prev = NULL;
  struct node* new = malloc(sizeof(struct node));
  new->key = num;
  prev = tmp;
  tmp = tmp->next;
  while (tmp != NULL && tmp->key < num) {
      prev = tmp;
      tmp = tmp->next;
  }
  new->next = tmp;
  prev->next = new;
  if (tmp == NULL)
      head = tmp;
}

应该这样做。你有效地传递了一个NULL 指针并返回void,所以它所做的一切对外界来说都是毫无意义的。

每个无bs链表实现的tm起点:

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

typedef struct node_tag {
    int value;
    struct node_tag *next;
} node_t;

// write functions to encapsulate the data and provide a stable interface:

node_t* node_create_value(int value)
{
    node_t *new_node = calloc(1, sizeof *new_node);
    if(new_node) new_node->value = value;
    return new_node;
}

node_t* node_advance(node_t const *node) { return node->next; }


typedef struct list_tag {  // a list usually consists of
    node_t *head;          // a pointer to the first and
    node_t *tail;          // a pointer to the last element
    // size_t size;        // one might want to add that.
} list_t;

list_t list_create(void)
{
    list_t list = { NULL, NULL };
    return list;
}

// make code based on these functions "speak" for itself:

node_t* list_begin(list_t const *list) { return list->head; }
node_t* list_end  (list_t const *list) { return list->tail; }
bool list_is_empty(list_t const *list) { return !list_begin(list); }

// common operations for lists:

node_t* list_push_front(list_t *list, int value)
{
    node_t *new_node = node_create_value(value);
    if (!new_node)
        return NULL;

    new_node->next = list->head;
    return list->head = new_node;
}

node_t* list_push_back(list_t *list, int value)
{
    // push_back on an empty list is push_front:
    if (list_is_empty(list))
        return list->tail = list_push_front(list, value);

    node_t *new_node = node_create_value(value);
    if (!new_node)
        return NULL;

    list->tail->next = new_node;
    return list->tail = new_node;
}

node_t* list_insert_after(list_t *list, node_t *node, int value)
{
    if (list_end(list) == node)
        return list_push_back(list, value);

    node_t *new_node = node_create_value(value);
    if (!new_node)
        return NULL;

    new_node->next = node->next;
    return node->next = new_node;
}

node_t* list_insert_sorted(list_t *list, int value)
{
    // first handle the special cases that don't require iterating the whole list:

    if (list_is_empty(list) || value < list_begin(list)->value)
        return list_push_front(list, value);

    if (value > list_end(list)->value)
        return list_push_back(list, value);

    // the general (worst) case:

    for (node_t *current_node = list_begin(list); node_advance(current_node); current_node = node_advance(current_node))
        if (value < node_advance(current_node)->value)
            return list_insert_after(list, current_node, value);

    return NULL; // should never happen
}

void list_print(list_t const *list)
{
    for (node_t *current_node = list_begin(list); current_node; current_node = node_advance(current_node))
        printf("%d\n", current_node->value);
}

void list_free(list_t *list)
{
    for(node_t *current_node = list_begin(list), *next_node; current_node; current_node = next_node) {
        next_node = current_node->next;
        free(current_node);
    }
}

// user code should not be required to know anything about the inner workings
// of our list:

int main(void)
{
    list_t list = list_create();
    for (int i = 1; i < 10; i += 2) {
        if (!list_push_back(&list, i)) {
            list_free(&list);
            fputs("Not enough memory :(\n\n", stderr);
            return EXIT_FAILURE;
        }
    }

    list_print(&list);
    putchar('\n');

    for (int i = 0; i < 11; i += 2) {
        if (!list_insert_sorted(&list, i)) {
            list_free(&list);
            fputs("Not enough memory :(\n\n", stderr);
            return EXIT_FAILURE;
        }
    }

    list_print(&list);
    list_free(&list);
}

输出:

1
3
5
7
9

0
1
2
3
4
5
6
7
8
9
10

【讨论】:

  • 这不会以任何方式、形状或形式回答问题。
  • @n.m.如果那是您的意见,请投反对票。 Plan-B:就如何改进提出建议。问题中的代码是 fubar。
  • 问题似乎是关于 OP 发布的代码的错误。一个改进计划:从答案中删除代码,解释错误是什么,如何修复它,以及将来如何避免它。 不要为练习提供理想的书本答案。对于那些在语言的基本概念上苦苦挣扎的人来说,这不太可能有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多