简体   繁体   English

制作链表时的困惑

[英]confusion in making of a linked list

i am trying to understand a code of creating and displaying a linked list我正在尝试理解创建和显示链接列表的代码

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

int create(int n);
void print();

struct node
{
    int data;
    struct node *next;
}
*head=NULL;

int main(){
    int n,data;
    printf("Enter the Number of Nodes\n");
    scanf("%d",&n);
    create(n);
    print();
}

int create(int n){
    int i=1,data;
    struct node *temp=(struct node*)malloc(sizeof(struct node));
    printf("Enter element %d:\n",i++);
    scanf("%d",&data);
    temp->data=data;
    temp->next=NULL;
    head=temp;
    while(n-->1){
        struct node *t=(struct node *)malloc(sizeof(struct node));
        printf("Enter element %d:\n",i++);
        scanf("%d",&data);
        t->data=data;
        t->next=NULL;
        temp->next=t;
        temp=t;
    }
    printf("Done :)\n");
}

void print(){
    struct node *temp=head;
    printf("Elements in list are:\n");
    if(temp==NULL)
        printf("List is Empty\n");
    else
    while(temp!=NULL){
        printf("%d ",temp->data);
        temp=temp->next;
    }
    printf("\n");
}

Now, i understand most of the working but there is some confusion I wanted to clarify.现在,我了解大部分工作,但我想澄清一些混乱。

The first node is created as head.the second node as t, which is then connected to head by pointing the next of head to itself.第一个节点创建为 head。第二个节点创建为 t,然后通过将 head 的下一个指向自身来连接到 head。 then how does the third node know that we are connecting it to second node(because all nodes are named t).那么第三个节点如何知道我们将它连接到第二个节点(因为所有节点都命名为 t)。 is it because of temp = t at the end of the while loop, so at next run the new t node gets connected to the temp which is previous t node.是不是因为在 while 循环结束时temp = t ,所以在下一次运行时,新的 t 节点连接到前一个 t 节点的 temp。

If that is the case, then I assume that only addresses are being connected to each other.如果是这种情况,那么我假设只有地址相互连接。

More precisely, with malloc we assign a memory of data and next to temp(which is pointer to node) then put first element in data and NULL in next and that is our first node.更准确地说,使用 malloc 我们分配一个data内存,然后在 temp (它是指向节点的指针) next ,然后将第一个元素放在data ,将 NULL 放在next ,这就是我们的第一个节点。 after that we have a head pointer which points to this first node.之后,我们有一个指向第一个节点的头指针。

My real confusion is what is happening is that we create new address bunches with each loop run and the print() uses those addresses to iterate from head to the last node.我真正的困惑是正在发生的事情是我们在每次循环运行时创建新的地址串,并且print()使用这些地址从头到最后一个节点进行迭代。 and exept head there is nothing to access our linked list after we exit the function.并且exept head在我们退出函数后没有任何东西可以访问我们的链表。

CORRECT?正确的?

the terminology may not be to the point.术语可能不是重点。

is it because of temp = t at the end of the while loop, so at next run the new t node gets connected to the temp which is previous t node.是不是因为在 while 循环结束时 temp = t,所以在下一次运行时,新的 t 节点连接到前一个 t 节点的 temp。

Yes.是的。 temp keeps track of the latest node. temp跟踪最新的节点。

struct node *next holds the memory location of the next node. struct node *next保存下一个节点的内存位置。

and except head there is nothing to access our linked list after we exit the function.在我们退出函数后,除了 head 没有任何东西可以访问我们的链表。

Yes.是的。 Without knowing head , you wont be able to go through your list.不知道head ,您将无法浏览您的列表。

第三个节点通过语句 temp->next=t 连接到第二个节点(temp 的地址为第二个,'t' 是一个新节点),最后,你正在使 temp=t 所以在下一次迭代中,temp将有第三个节点地址等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM