C语言 如何检查链表是否为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20418624/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to check if a linked list is empty
提问by help_needed
I'm new to C and have a question.
我是 C 新手,有一个问题。
How would I check if a linked list is empty?
如何检查链表是否为空?
I have a struct _node
我有一个结构 _node
typedef struct _node{
int data;
struct _node *next;
}node;
If I have initialized node *list, but didn't do anything to it (i.e didn't assign list->dataa value), how would I check if it's empty?
如果我已经初始化node *list,但没有对它做任何事情(即没有赋值list->data),我将如何检查它是否为空?
I tried if (node == NULL){break}but didn't work.
我试过if (node == NULL){break}但没有用。
Thanks for the help!
谢谢您的帮助!
回答by DarkDust
Intro:
介绍:
There are usually two ways to use linked lists: with a root element and without.
通常有两种使用链表的方法:使用根元素和不使用。
Without a root, your list pointer is NULL when the list is empty:
如果没有根,当列表为空时,您的列表指针为 NULL:
node *list;
...
if (list == NULL) { /* empty list */ }
With root, there is always one element. But it can be used in two ways:
对于 root,总是有一个元素。但它可以通过两种方式使用:
Either simply to provide a pointer to the first element.
要么只是提供指向第一个元素的指针。
node *root;
...
if (root->next == NULL) { /* empty list */ }
Or to have the last element link back to the root to form a cycle. This concept has the advantage that the "next" element is never NULL and you thus don't need to check for it. In this case, the list is empty if the root points back to itself.
或者让最后一个元素链接回根以形成一个循环。这个概念的优点是“下一个”元素永远不会为 NULL,因此您不需要检查它。在这种情况下,如果根指向自身,则列表为空。
node *root;
...
if (root->next == root) { /* empty list */ }
Answer:
回答:
Now, according to your description, you have allocated a node. This either means you want the "root" approach (second or third example). But if you want to go with the first variant, you must not allocate the node, since it doesn't hold data.
现在,根据您的描述,您已经分配了一个节点。这要么意味着您想要“根”方法(第二个或第三个示例)。但是如果你想使用第一个变体,你不能分配节点,因为它不保存数据。
For the "root" approach, you indeed have one (and only one) node that doesn't hold data. But for simple linked lists, all nodes must contain data.
对于“根”方法,您确实有一个(并且只有一个)节点不保存数据。但是对于简单的链表,所有节点都必须包含数据。
回答by notnull
I'd keep it simple.
我会保持简单。
Check the head pointer - if it is NULL, there's no entry in the list.
检查头指针 - 如果它是 NULL,则列表中没有条目。
int isEmpty( node * list )
{
if( !list )
return 1;
return 0;
}
回答by steveha
Sometimes when you create a variable, C will zero out all the data for you. Other times, the variable will contain garbage. You need to learn the rules that govern this, but as a beginner, you should simply be certain always to initialize your variables explicitly.
有时,当您创建一个变量时,C 会为您将所有数据归零。其他时候,变量将包含垃圾。您需要学习管理这些的规则,但作为初学者,您应该确保始终明确地初始化您的变量。
For a linked list, when you append a node to a list, you should always be certain to set a NULLinto the nextpointer, so that the list will be properly terminated.
对于链表,当您将节点附加到列表时,您应该始终确保将 a 设置NULL为next指针,以便列表正确终止。
Your linked list should have a "head" pointer. If the head is set to NULLthen you have a zero-length linked list; if the head has any value other than NULLyou must have at least one node in the linked list, so the list must have length of at least 1.
你的链表应该有一个“头”指针。如果 head 设置为 ,NULL则您有一个零长度链表;如果头有任何值,NULL你必须在链表中至少有一个节点,所以链表的长度必须至少为 1。
So, as long as you have been careful about initializing your head pointer to NULLyou can trivially tell whether the list is empty by checking to see if the head pointer is NULL.
因此,只要您小心初始化指向的头指针,NULL您就可以通过检查头指针是否为来轻松判断列表是否为空NULL。
But your question was really about the datamember variable. There will always be some sort of value in there. As I said, sometimes C will set it to zero for you, but other times it will be unpredictable garbage (a value that could be any integer value). You need to initialize that to a sensible default when you allocate a new node. For example, the valid values of datamight all be greater than zero, so you might set datato zero to indicate that it is currently empty.
但是您的问题实际上是关于data成员变量的。那里总会有某种价值。正如我所说,有时 C 会为您将其设置为零,但有时它将是不可预测的垃圾(一个值可以是任何整数值)。分配新节点时,您需要将其初始化为合理的默认值。例如, 的有效值data可能都大于零,因此您可以设置data为零以指示它当前为空。
You can write a simple loop that traverses your linked list and checks each datamember, and return true if it finds any datathat is not zero (or whatever special value flags that it is unused). It should return false if it finds only default dataor if the list is zero length.
您可以编写一个简单的循环来遍历您的链表并检查每个data成员,如果找到任何data不为零的成员(或任何未使用的特殊值标志),则返回 true 。如果它只找到默认值data或者列表长度为零,它应该返回 false 。
回答by SoulRayder
Its better to have a headernode when building your linked list. Then you can add nodes to build the list. Let that list variable be your header node. Then to check if your linked list is empty, use this:
在构建链表时最好有一个头节点。然后您可以添加节点来构建列表。让该列表变量成为您的头节点。然后检查你的链表是否为空,使用这个:
typedef struct _node{
int data;
struct _node *next;
}*node;
and then instantiate
然后实例化
node list;
and to check the linked list is emptyuse
并检查链表是否为空使用
if((list->next)==null)
{
break;
}

