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

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

问题描述

谁能告诉我 while(thead != NULL)while(thead->next !=NULL) 之间有什么区别,因为用于遍历列表 thead != NULL 不起作用而 thead->next 起作用.根据我的理解,头节点只是一个指向起始节点的指针,而不是起始节点本身.
如果你有疑问,请看这个.这里的头只是存储地址.

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

#include#include#include结构节点{整数数据;结构节点*下一个;};结构节点*头;无效插入(int x){struct node *temp=(struct node *)malloc(sizeof(struct node));温度->数据=x;temp->next=NULL;如果(头==空){头=温度;}别的{结构节点 * 头;头=头;while(thead->next!=NULL){thead=thead->next;}thead->next=temp;}}无效打印(){结构节点 *temp;温度=头;而(温度!=NULL){printf("%d",temp->data);temp=temp->next;}}int main(){头=空;int i,n,x;printf("输入节点数");scanf("%d",&n);for(i=0;i

如果我们用 thead !=NULL 替换 thead ->next != NULL 那么 dev c++ 停止工作.反之亦然发生在 printf 中进行遍历......

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

此外,头节点是第一个同时包含数据和地址的节点,还是只存储上图中的地址?

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

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

谢谢

解决方案

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

temp=head;而(温度!=NULL){printf("%d",temp->data);temp=temp->next;}//此时 temp == NULL 代码不关心最后一个节点是什么.

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

//首先处理链表头部为NULL的特殊情况//否则 ....while(thead->next!=NULL) {thead = thead->next;}//此时 temp->next == NULL//代码可以使用 `thead` 来访问最后一个节点的成员.thead->next = temp;

<小时><块引用>

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

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

<块引用>

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

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

<块引用>

指向结构的指针何时为 NULL?

问题不清楚.指向 struct 的指针在其值等于 NULL 时为 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 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();
}

If we replace thead ->next != NULL by thead !=NULL then dev c++ stops working.Vice versa happens in the printf for traversal ...

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?

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

And when is a pointer to a structure NULL?

Thanks

解决方案

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.

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 is a pointer. When head != NULL, it pointer the the frist node which contains data and a next pointer.

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

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

when is a pointer to a structure 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天全站免登陆