简体   繁体   English

链接列表和双指针

[英]Linked list and double pointers

In the below code, I am trying to insert a node after a particular node. 在下面的代码中,我尝试在特定节点之后插入一个节点。 In the function, I will be giving as input the address of the previous node after which I want to insert the new node. 在函数中,我将输入前一个节点的地址作为输入,然后在该地址之后插入新节点。 The problem is in the 10th line of function insertAfter() - it says I cannot access *prev_ref->next. 问题出在函数insertAfter()的第十行中-它说我无法访问* prev_ref-> next。

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

 struct node
 {
  int data;
  struct node* next;
 };

 void push(struct node **head_ref, int data)
{

struct node* newNode = (struct node*)malloc(sizeof(struct node)) ;
newNode->next= *head_ref;
newNode->data= data;
*head_ref= newNode;

}


void insertAfter(struct node **prev_ref, int data)
{
if(*prev_ref==NULL)
{
    printf("prev ref cant be null");
    return;
}
struct node * newNode;
newNode = (struct node*)malloc(sizeof(struct node)) ;
newNode->next= *prev_ref->next;
newNode->data= data;
*prev_ref->next= newNode;

}


 void printList(struct node *node)
    {
     while (node != NULL)
      {
       printf(" %d ", node->data);
       node = node->next;
    }
   }

main()
{
struct node* head = NULL;
push(&head, 7);
push(&head, 1);
insertAfter(&head, 8);
printf("\n Created Linked list is: ");
printList(head);
 getchar();
 return 0;

 }

Do you know (*p).s is equivalent to p->s ? 你知道(*p).s等于p->s吗? I would suggest you to try something like (*prev_ref)->next or (**prev_ref).next 我建议您尝试类似(*prev_ref)->next(**prev_ref).next

You seem to dereference prev_ref three-levels deep instead of two. 您似乎取消引用了prev_ref三个级别,而不是两个级别。

pointer->field is dereference of a pointer, equivalent to (*pointer).field pointer->field是指针的取消引用,等效于(*pointer).field

So, **prev_ref->next; 因此, **prev_ref->next; is in fact (***prev_ref).next; 实际上是(***prev_ref).next;

Either drop one asterisk or use . 放一个星号或使用. instead of -> . 代替->

EDIT: You seem to have skipped the parentheses we included in our answers. 编辑:您似乎已经跳过了我们在答案中包含的括号。

-> has higher precedence than * . ->具有比*高的优先级。

The effect is: 效果是:

(*prev_ref)->next

  • first uses '*' and finds the memory pointed to by prev_ref (let's call it memory location A ), 首先使用“ *”并找到prev_ref指向的内存(我们称其为内存位置A ),
  • then uses '->' to find memory pointed to by A , let's call it B , 然后使用'->'查找A指向的内存,我们称它为B
  • then location of the the next field of the structure, offset by a set distance from B , let's call it C 然后是结构的next字段的位置,该位置与B偏移了设定的距离,我们称其为C
  • and finally accesses (reads/writes) the value stored at C. 最后访问(读/写)存储在C处的值。

Now for *prev_ref->next 现在用于*prev_ref->next

  • first, uses -> and finds the memory pointed to by prev_ref (A), just the same 首先,使用->并找到prev_ref (A)指向的内存,就像
  • then the location of the the next field of the structure, offset by a set distance from A , which happens to be an entirely random location in memory (because A stored a pointer to the structure, not the structure itself); 然后,该结构的next字段的位置,与A偏移一个设定的距离,该位置恰好是内存中的一个完全随机的位置(因为A存储了指向该结构的指针,而不是结构本身); let's call that location D. 我们称该位置为D。
  • Then it tries to find the memory location at wherever D pointed to, which is entirely random. 然后,它尝试在D指向的任何位置查找内存位置,这完全是随机的。

Now, the system won't let you do that, because it sees A is not where a structure lies, but where a pointer to a structure lies, hence the error message 现在,系统不允许您这样做,因为它看到A不是结构所在,而是指向结构的指针,因此出现错误消息

And the fundamental reason of your problems is that you use pointers-to-pointers for no good reason. 问题的根本原因是您没有充分的理由使用指针到指针。 Nothing of this would have happened if you always used plain pointers. 如果您始终使用普通指针,则不会发生任何事情。 void push(struct node *head_ref, int data) , void insertAfter(struct node *prev_ref, int data) , prev_ref->next etc. Managing pointers to pointers is tricky, error-prone (as you've experienced) and in 99% cases completely unnecessary. void push(struct node *head_ref, int data)void insertAfter(struct node *prev_ref, int data)prev_ref->next等。管理指针的指针非常棘手,容易出错(如您prev_ref->next ),在99 %的情况完全没有必要。

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

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