本文介绍了如何修复此链接列表插入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我得到了一个简单的任务,处理双链表,动态数据分配和递归。我创建了一个只有10个整数的数组,我试图使用递归将这些整数放入一个已排序的双向链表中。我在将节点插入链表时遇到了一些麻烦;我的输出是2 7 9 100,由于某种原因缺少其他6个整数。我究竟做错了什么?感谢您的任何帮助! (使用的语言是C) I was given an simple assignment that deals with doubly linked lists, dynamic data allocation, and recursion. I created an array of just 10 integers and I am trying to put these integers into a sorted doubly linked list using recursion. I am having some trouble with inserting nodes into the linked list; my output is "2 7 9 100" and is missing the other 6 integers for some reason. What am I doing wrong? Thank you for any help! (The language used is C)#include <stdio.h>#include <stdlib.h>#define N 10typedef struct node_ { int value; struct node_ *next; struct node_ *prev;} node;void insert(node **head, node *cur, node *p);void print_list(node *cur);void print_list(node *cur){ if (!cur) { printf("\n"); return; } else { printf("%d ", cur->value); print_list(cur->next); }}int main(int argc, char *argv[]){ int i; int data[N] = {2, 7, 3, 9, 4, 4, 0, 8, 7, 100}; node *p, *head; head = NULL; for (i = 0; i < N; i++) { p = (node *)malloc(sizeof(node)); p->value = data[i]; insert(&head, head, p); } print_list(head);}void insert(node **head, node *cur, node *p){ if(*head == NULL) { p->next = p->prev = NULL; *head = p; return; } if(p->value < cur->value) { p->prev = cur->prev; p->next = cur; cur->prev = p; if(cur->prev != NULL) cur->prev->next = p; else *head = p; return; } if(cur->next == NULL) { cur->next = p; p->next = NULL; p->prev = cur; } else insert(head, cur->next, p);} 推荐答案 cur->prev = p;if(cur->prev != NULL) 强烈建议使用人类可读的名字 - 所以你可以看到'cur '是'p'现在。 it is strongly recommended to use human readable names - so you can see 'cur' is 'p' now.if(p->prev != NULL) p->prev->next = p; 问候。regards. 这篇关于如何修复此链接列表插入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-21 22:46