简体   繁体   English

C语言中链表的麻烦

[英]Hassle in Linkedlist in C

Suppose I have 3 nodes already in the list (ie 10 , 20). 假设我已经在列表中有3个节点(即10,20)。 And I want to insert 30. So wrote as below 我要插入30。因此如下所示

struct node *p,*temp;
p=start;
temp=(struct node*)malloc(sizeof(struct node));
temp->info=30;
temp->link=NULL;
while(p-link!=NULL) 
{
  p=p->link;
}
p->link=temp;

It worked perfectly until I do this: while(p!=NULL) rest is same.. Similarly in case of showing the node I wrote as follows 在执行此操作之前,它一直运行良好: while(p!=NULL) rest是相同的。类似地,在显示节点的情况下,我如下编写

while(p!=NULL)
    {
        printf("%d \n",p->info);
        p=p->link;
    }

This also worked fine until I changed it to: while(p->link!=NULL) 直到我将其更改为: while(p->link!=NULL)这也可以正常工作

I want to know what is happening why is not working? 我想知道发生了什么,为什么不起作用? Give me the reason why we are using while(p!=NULL) in case of showing the all the data and while(p->link!=NULL) in case of inserting any node? 请告诉我为什么在显示所有数据的情况下使用while(p!= NULL)而在插入任何节点的情况下使用while(p-> link!= NULL)的原因?

A linked list, in C is a struct that contains data and a pointer to the next element (and maybe a pointer to the previous element, but that doesn't seem to be your case). C中的链表是一个结构,其中包含数据和指向下一个元素的指针(可能还指向上一个元素的指针,但这似乎不是您的情况)。 It would be something like this: 就像这样:

struct node {
    int data; //could be any type
    struct node *next;
};

To insert elements in the end of the list, you need to go to the end of it - when there is no next element. 要将元素插入列表的末尾,您需要转到列表的末尾-当没有下一个元素时。 To do that, you check node->next == NULL . 为此,请检查node->next == NULL Then you make node->next = new_node 然后你使node->next = new_node

To find an element in the list, you need to loop for all elements until you find it. 要在列表中找到一个元素,您需要循环搜索所有元素,直到找到它为止。 If the element doesn't exists in the list, you need to prevent the user from accessing an invalid element, so you test node == NULL to finish the loop safely. 如果列表中不存在该元素,则需要防止用户访问无效的元素,因此请测试node == NULL以安全地完成循环。 This case is also applicable if you want to just print all data in the list - you need to reach all elements, the test would be the same (if you were testing node->next == NULL , you would skip the last element). 如果您只想打印列表中的所有数据,这种情况也适用-您需要访问所有元素,测试将是相同的(如果您正在测试node->next == NULL ,则将跳过最后一个元素) 。

Before the first while loop, you've got something like this. 在第一个while循环之前,您已经有了类似的东西。 Two nodes (not three as you state) in a linked-list, pointed to by start and a new node, pointed to by temp : 链表中的两个节点(不是您所说的三个),由start指向,而新的节点由temp指向:

start-->+---------+  +-->+---------+      +---------+<--temp
        |info:10  |  |   |info:20  |      |info:30  |
        |link:xxxx|--+   |link:NULL|      |link:NULL|
        +---------+      +---------+      +---------+

To add the new node to the end of the list, you need to advance p from the first node (pointed to by start ) until p points at the node whose link is NULL (ie, until p->link!=NULL fails to be true): 要将新节点添加到列表的末尾,您需要从第一个节点(由start指向)前进p直到p指向linkNULL的节点(即直到p->link!=NULL失败)为止p->link!=NULL是真实的):

start-->+---------+  +-->+---------+      +---------+<--temp
        |info:10  |  |   |info:20  |      |info:30  |
        |link:xxxx|--+   |link:NULL|      |link:NULL|
        +---------+      +---------+      +---------+
                            ^
                            |
                         p -+

Once you have found the last node, you then "plumb" in your new node to get a three-element list: 一旦找到了最后一个节点,就可以在新节点中“铅垂”,以获得三元素列表:

start-->+---------+  +-->+---------+  +-->+---------+
        |info:10  |  |   |info:20  |  |   |info:30  |
        |link:xxxx|--+   |link:yyyy|--+   |link:NULL|
        +---------+      +---------+      +---------+

However, when you're printing all of the nodes, you start p at the first node (ie start ) and print the details of every node until p has been set to NULL (ie p!=NULL fails to be true). 但是,当您打印所有节点时,请在第一个节点处启动p (即start ),并打印每个节点的详细信息,直到p设置为NULL (即p!=NULL不能为真)。 If you stopped when p->link was NULL , you would not have printed the last node. 如果在p->linkNULL时停止,则不会打印最后一个节点。

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

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