如何修复此链接列表插入? [英] How Do I Fix This Linked List insertion?

查看:89
本文介绍了如何修复此链接列表插入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了一个简单的任务,处理双链表,动态数据分配和递归。我创建了一个只有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 10

typedef 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);
}

推荐答案

显然,你自己的代码对你来说就像一个黑盒子。它不能达到您的预期。

以下内容不是您问题的直接解决方案,而是帮助您自行了解错误的关键。

调试器是你的朋友。它将向您显示您的代码实际正在做什么。

按照执行,检查变量,您将看到有一个点,它停止做您期望的。

掌握Visual Studio 2010中的调试 - 初学者指南 [< a href =http://www.codeproject.com/Articles/79508/Mastering-Debugging-in-Visual-Studio-A-Beginntarget =_ blanktitle =New Window> ^ ]

细节可能会根据您的编译器而改变,这个想法保持不变。
Obviously, your own code is like a blackbox to you. It don't do what you expect.
What follow is not directly a solution to your problem, but a key that will help you to understand by yourself what is wrong.
The debugger is your friend. It will show you what your code is really doing.
Follow the execution, inspect variables and you will see that there is a point where it stop doing what you expect.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Details may change depending on your compiler, the idea stay the same.


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.


这篇关于如何修复此链接列表插入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆