如何将链表设置为NULL? [英] How to set linked list to NULL?

查看:161
本文介绍了如何将链表设置为NULL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的链表代码似乎在无限循环中打印出数据的值,我相信这是因为磁头永远不会设置为NULL。我不知道该怎么做。



我尝试了什么:



My linked list code seems to print out the value of data in an infinite loop and i believe it's because the head is never set to NULL. I am not sure how to do this.

What I have tried:

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

typedef struct Node
{
	int data;
	struct Node* next;
}Node;

typedef struct 
{
	Node* head;
}Linked_List;

/*------------------------------------------------------------------*/

Linked_List* create_empty_list(void)
{
	Linked_List* new_head; /*create list pointer*/
	new_head = (Linked_List*)malloc(sizeof(Linked_List)); /*assign it memory of head struct*/
	new_head->head = NULL;
	
	return new_head;
}

Node* insert_start(Linked_List* list, int data)
{
	Node* new_node;
	new_node = (Node*)malloc(sizeof(Node));
	new_node->data = data;
	new_node->next = list->head;
	list->head = new_node;
	
	return new_node;
}
	
void display(Linked_List* list)
{
	Node* temp;
	temp = list->head;
	while(temp != NULL)
	{
		printf("%d ", temp->data);
	}
	temp = temp->next;
}
	
int main(void)
{
	
	Linked_List* the_list = create_empty_list();
	
	the_list->head = insert_start(the_list, 5);
	
	display(the_list);
	
	
	return(0);
	
}

推荐答案

Jochen 已修复您的错误。

此外,请注意您既不需要 Linked_List 结构,也不需要 create_empty_list 函数,而您需要释放记忆。

尝试

Jochen already fixed your bug.
Additionally, please note you need neither the Linked_List struct nor the create_empty_list function, while you do need to release memory.
Try
#include <stdlib.h>
#include <stdio.h>

typedef struct Node
{
  int data;
  struct Node* next;
} Node;

Node * insert(Node* head, int data)
{
  Node* new_node;
  new_node = (Node*)malloc(sizeof(Node));
  new_node->data = data;
  new_node->next = head;
  return new_node;
}

void display(Node * head)
{
  Node * temp = head;
  while(temp != NULL)
  {
    printf("%d ", temp->data);
    temp = temp->next;
  }
}

void cleanup(Node * head)
{
  Node * temp = head;
  while ( temp != NULL)
  {
    Node * cur = temp;
    temp = temp->next;
    free(cur);
  }
}


int main()
{
  Node * head = NULL;

  head = insert(head, 1);
  head = insert(head, 3);
  head = insert(head, 5);
  head = insert(head, 12);

  display(head);
  printf("\n");
  cleanup(head);

  return 0;
}


引用:

我相信这是因为head永远不会设置为NULL。

i believe it's because the head is never set to NULL.



停止相信,学习调试器并确保。



有一个允许的工具你看看你的代码在做什么,它的名字是调试器。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。

当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

调试器在这里向您展示您的代码正在做什么,您的任务是与什么进行比较应该这样做。

调试器中没有魔法,它没有找到错误,它只是帮助你。如果代码没有达到预期的效果,那么你就接近了一个错误。


Stop believing, learn the debugger and ensure.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


更新 temp-> next 显示函数必须在内,而循环:

The update of temp->next in the display function must be inside the while loop:
void display(Linked_List* list)
{
    Node* temp;
    temp = list->head;
    while(temp != NULL)
    {
        printf("%d ", temp->data);
        temp = temp->next;
    }
}


这篇关于如何将链表设置为NULL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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