链表遍历** while(thead!= NULL)**和while(thead-> next!= NULL)之间的差异 [英] Difference between linked list traversal **while(thead != NULL)** and while(thead->next !=NULL)

查看:264
本文介绍了链表遍历** while(thead!= NULL)**和while(thead-> next!= NULL)之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我 while(thead!= NULL) while(thead-> next!= NULL)有什么区别,因为遍历列表<当 thead-> next 工作时,strong> thead!= NULL 不起作用. 根据我的理解,头节点只是指向起始节点的指针,而不是起始节点本身.
如果您有疑问,请参阅此.这里只是存储地址.

Can anyone tell me what is the difference between while(thead != NULL) and while(thead->next !=NULL) because for traversing the list thead != NULL is not working while thead->next works. According to my understanding head node is just a pointer to the starting node and not the the starting node itself.
See this if u have doubt.Here head just stores address.

//thead表示临时头变量,用于存储地址头的指向.
这是插入代码.

//thead means temp head variable to store the address head points to.
This is the code for insertion.

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

struct node
{
    int data;
    struct node *next;
};
struct node *head;
void insert(int x)
{
    struct node *temp=(struct node *)malloc(sizeof(struct node));   
    temp->data=x;   
    temp->next=NULL;   
    if(head==NULL)     
    {     
         head=temp;    
    }
    else
    {
        struct node * thead;
        thead=head;  
        while(thead->next!=NULL)
        {
            thead=thead->next;  
        }
        thead->next=temp;
    }
}

void print()
{
    struct node *temp;
    temp=head;
    while(temp!=NULL)
    {

        printf("%d",temp->data);
            temp=temp->next;
    }
}
int main()
{
    head=NULL;
    int i,n,x;
    printf("enter number of nodes");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        printf("enter no");
        scanf("%d",&x);
        insert(x);
    }
    print();
}

如果我们将thead-> next!= NULL替换为thead!= NULL,则dev c ++停止工作.在printf中反之亦然.

那么有人可以回答以上两者之间的区别吗?

So can someone answer the difference between the above two?

此外,头节点是包含数据和地址的第一个节点,还是仅存储地址(如上图所示)?

Also,Is head node the first node which contains both data and address or does it just stores addresses like in the diagram above?

如果头节点只是一个存储地址的指针,那么我们如何访问thead-> next呢?

Also if the head node is only a pointer which stores address then how are we able access thead->next ?

什么时候指向结构的指针为NULL?

And when is a pointer to a structure NULL?

谢谢

推荐答案

使用print(),代码无需记住循环后的最后一个节点地址

With print(), code does not need to remember the last node address after the loop

temp=head;
while(temp!=NULL) {
    printf("%d",temp->data);
    temp=temp->next;
}
// temp == NULL at this point and code does not care what the last node was.

对于insert(),代码确实需要记住循环后的最后一个节点地址.

With insert(), code does need to remember the last node address after the loop.

// First handle special case where the head of the list is NULL
// otherwise ....
while(thead->next!=NULL) {
    thead = thead->next;  
}
// temp->next == NULL at this point
// Code can use `thead` to access the last node's members.
thead->next = temp;


头节点是包含数据和地址的第一个节点,还是像上图中那样仅存储地址?

Is head node the first node which contains both data and address or does it just stores addresses like in the diagram above?

struct node *head是一个指针. head != NULL时,它指向包含数据的第一个节点和下一个指针.

struct node *head is a pointer. When head != NULL, it pointer the the frist node which contains data and a next pointer.

如果头节点只是一个存储地址的指针,那么我们如何访问thead-> next?

if the head node is only a pointer which stores address then how are we able access thead->next ?

theadhead初始化.通过取消引用thead,代码可以访问该节点的成员,包括.next.

thead is initialized with head. By de-referencing thead, code has access to that node's members including .next.

何时指向结构NULL的指针?

when is a pointer to a structure NULL?

问题尚不清楚.当struct的指针的值等于NULL时,它就是NULL.通常,这是链表代码使用的结尾.

Question is unclear. A pointer to a struct is NULL when it has a value equal to NULL. This is usually as the end in the linked-list code employs.

这篇关于链表遍历** while(thead!= NULL)**和while(thead-> next!= NULL)之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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