插入新元素导致链表c编程错误 [英] linked list c programming error by inserting new element
问题描述
我正在尝试插入一个元素,但出现错误进程已完成,退出代码为 11"
结构节点{整数键;结构节点*下一个;};结构节点* init(){结构节点 *head =NULL;回头;}void create(struct node * head,int num) {结构节点 * tmp = 头;结构节点 * prev = NULL;结构节点*新= malloc(大小(结构节点));new->key = num;上一个 = tmp;tmp = tmp->下一个;while(tmp!= NULL && tmp->key 下一个;}new->next = tmp;上一个->下一个=新的;如果(tmp== NULL)头=tmp;}int main() {整数;结构节点*头;头=初始化()printf("请输入数据:");scanf("%d",&num);创建(头,数量);}
我正在尝试将一个元素插入到链表中,并且该元素应该同时排序和输入.有人可以告诉我错误是?我似乎无法找出错误.
不清楚你的函数是什么create()
void create(struct node * head, int num) {结构节点 * tmp = 头;结构节点 * prev = NULL;结构节点*新= malloc(大小(结构节点));new->key = num;上一个 = tmp;tmp = tmp->下一个;while (tmp != NULL && tmp->key 下一个;}new->next = tmp;上一个->下一个=新的;如果(tmp == NULL)头 = tmp;}
应该这样做.你有效地向它传递了一个 NULL
指针并返回 void
,所以它所做的一切对外界来说都是毫无意义的.
tm 每个无 bs 链表实现的起点:
#include #include #include typedef struct node_tag {整数值;struct node_tag *next;} node_t;//编写函数来封装数据并提供稳定的接口:node_t* node_create_value(int 值){node_t *new_node = calloc(1, sizeof *new_node);if(new_node) new_node->value = value;返回新节点;}node_t* node_advance(node_t const *node) { return node->next;}typedef struct list_tag {//一个列表通常由node_t *头;//指向第一个和的指针node_t *尾;//指向最后一个元素的指针//size_t 大小;//一个人可能想要添加它.} list_t;list_t list_create(void){list_t 列表 = { NULL, NULL };退货清单;}//使基于这些函数的代码为自己说话":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);}//列表的常见操作:node_t* list_push_front(list_t *list, int 值){node_t *new_node = node_create_value(value);如果(!新节点)返回空;new_node->next = list->head;返回列表->head = new_node;}node_t* list_push_back(list_t *list, int 值){//空列表上的 push_back 是 push_front:如果(list_is_empty(列表))返回列表->tail = list_push_front(list, value);node_t *new_node = node_create_value(value);如果(!新节点)返回空;list->tail->next = new_node;返回列表->tail = new_node;}node_t* list_insert_after(list_t *list, node_t *node, int value){if (list_end(list) == 节点)返回 list_push_back(list, value);node_t *new_node = node_create_value(value);如果(!新节点)返回空;new_node->next = node->next;返回节点->next = new_node;}node_t* list_insert_sorted(list_t *list, int value){//首先处理不需要迭代整个列表的特殊情况:if (list_is_empty(list) || value value)返回 list_push_front(list, value);如果(值> list_end(列表)->值)返回 list_push_back(list, value);//一般(最坏)情况:for (node_t *current_node = list_begin(list); node_advance(current_node); current_node = node_advance(current_node))如果(值值)返回 list_insert_after(list, current_node, value);返回空;//不应该发生}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
", 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;免费(当前节点);}}//不应该要求用户代码了解有关内部工作的任何信息//我们的列表:int main(void){list_t list = list_create();for (int i = 1; i <10; i += 2) {如果 (!list_push_back(&list, i)) {list_free(&list);fputs("内存不足:(
", stderr);返回 EXIT_FAILURE;}}list_print(&list);putchar('
');for (int i = 0; i <11; i += 2) {如果 (!list_insert_sorted(&list, i)) {list_free(&list);fputs("内存不足:(
", stderr);返回 EXIT_FAILURE;}}list_print(&list);list_free(&list);}
输出:
13579012345678910
I am trying to insert an element but it get the error "Process finished with exit code 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);
}
i am trying to insert an element into a linked list and the element should be sorted and entered at the same time.can someone tell me that the error is ? i cannot seem to find out the error.
It's not clear what your function 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; }
is supposed to do. You effectively pass it a NULL
pointer and return void
, so everything it does is meaningless to the outside world.
Thetm starting point for every no bs linked list implementation:
#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
", 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 :(
", stderr);
return EXIT_FAILURE;
}
}
list_print(&list);
putchar('
');
for (int i = 0; i < 11; i += 2) {
if (!list_insert_sorted(&list, i)) {
list_free(&list);
fputs("Not enough memory :(
", stderr);
return EXIT_FAILURE;
}
}
list_print(&list);
list_free(&list);
}
Output:
1
3
5
7
9
0
1
2
3
4
5
6
7
8
9
10
这篇关于插入新元素导致链表c编程错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!