关于链接列表中节点的混淆

时间:2022-07-11 07:19:36

I have a question about Linked Lists. I spoke to a friend and am now confused. Suppose that there are two variables of type struct node. One is ptr and the other is the header node.

我有关于链接列表的问题。我跟朋友说过话,现在很困惑。假设有两个类型为struct node的变量。一个是ptr,另一个是标题节点。

struct node
{
  int data;
  struct node *link;
};
struct node *ptr,*header;

What is the difference between

有什么区别

ptr=header

and

ptr->link=header

and

ptr->link=header->link

? Edit:I mean semantically.

?编辑:我的意思是语义。

3 个解决方案

#1


5  

Assuming header is pointing to an allocated node initially, it will look like

假设标头最初指向分配的节点,它看起来像

                       +----------------+-----------+
                       |                |           |
header  +----------->  |   data         |  link+-----------> other node/NULL
                       |                |           |
                       +----------------+-----------+

After ptr=header, both ptr and header points to the same node

在ptr = header之后,ptr和header都指向同一节点

                       +----------------+-----------+
pointer +----------->  |                |           |
header  +----------->  |   data         |  link+-----------> other node/NULL
                       |                |           |
                       +----------------+-----------+

After ptr->link=header,

                       +----------------+-----------+
pointer +----------->  |                |           |
header  +----------->  |   data         |  link+----------+ 
                +--->  |                |           |     |
                |      +----------------+-----------+     |
                +-----------------------------------------+  

after ptr->link=header->link, it would depend on where header and ptr are pointing

在ptr-> link = header-> link之后,它将取决于header和ptr指向的位置

  • if they point to the same node then this statement will have no effect.
  • 如果它们指向同一节点,那么此语句将不起作用。

  • if they point to some different nodes then link pointer of both nodes pointed by ptr and header will point to the same node (or NULL).

    如果它们指向某些不同的节点,那么由ptr和header指向的两个节点的链接指针将指向同一节点(或NULL)。

                           +----------------+-----------+
                           |                |           |
    header  +----------->  |   data         |  link+--------------+                                                       |
                           |                |           |         |
                           +----------------+-----------+         +------> |
                                                                           |other node/NULL 
                                                                  +------> |
                           +----------------+-----------+         |
                           |                |           |         |
    ptr     +----------->  |   data         |  link+--------------+ 
                           |                |           |
                           +----------------+-----------+
    

#2


0  

Assuming you execute only one of these cases:

假设您只执行以下一种情况:

  • ptr=header would make a pointer to the header node. This means the data of ptr would be the same as the data in header. ptr->link would also be the same as header->link
  • ptr = header将生成指向头节点的指针。这意味着ptr的数据与标题中的数据相同。 ptr-> link也与header-> link相同

  • ptr->link=header moves ptr infront of header in the list
  • ptr-> link = header移动列表中标题的ptr infront

  • ptr->link = header->link makes ptr parallel to header, e.g.:

    ptr-> link = header-> link使ptr与标题并行,例如:

    ptr->data != header->data; // TRUE
    a = ptr->link;
    b = header->link;
    a->data == b->data // TRUE
    

where a and b are of type struct node*

其中a和b的类型为struct node *

#3


0  

The best way to understand is to draw it out.关于链接列表中节点的混淆

理解的最好方法是把它画出来。

#1


5  

Assuming header is pointing to an allocated node initially, it will look like

假设标头最初指向分配的节点,它看起来像

                       +----------------+-----------+
                       |                |           |
header  +----------->  |   data         |  link+-----------> other node/NULL
                       |                |           |
                       +----------------+-----------+

After ptr=header, both ptr and header points to the same node

在ptr = header之后,ptr和header都指向同一节点

                       +----------------+-----------+
pointer +----------->  |                |           |
header  +----------->  |   data         |  link+-----------> other node/NULL
                       |                |           |
                       +----------------+-----------+

After ptr->link=header,

                       +----------------+-----------+
pointer +----------->  |                |           |
header  +----------->  |   data         |  link+----------+ 
                +--->  |                |           |     |
                |      +----------------+-----------+     |
                +-----------------------------------------+  

after ptr->link=header->link, it would depend on where header and ptr are pointing

在ptr-> link = header-> link之后,它将取决于header和ptr指向的位置

  • if they point to the same node then this statement will have no effect.
  • 如果它们指向同一节点,那么此语句将不起作用。

  • if they point to some different nodes then link pointer of both nodes pointed by ptr and header will point to the same node (or NULL).

    如果它们指向某些不同的节点,那么由ptr和header指向的两个节点的链接指针将指向同一节点(或NULL)。

                           +----------------+-----------+
                           |                |           |
    header  +----------->  |   data         |  link+--------------+                                                       |
                           |                |           |         |
                           +----------------+-----------+         +------> |
                                                                           |other node/NULL 
                                                                  +------> |
                           +----------------+-----------+         |
                           |                |           |         |
    ptr     +----------->  |   data         |  link+--------------+ 
                           |                |           |
                           +----------------+-----------+
    

#2


0  

Assuming you execute only one of these cases:

假设您只执行以下一种情况:

  • ptr=header would make a pointer to the header node. This means the data of ptr would be the same as the data in header. ptr->link would also be the same as header->link
  • ptr = header将生成指向头节点的指针。这意味着ptr的数据与标题中的数据相同。 ptr-> link也与header-> link相同

  • ptr->link=header moves ptr infront of header in the list
  • ptr-> link = header移动列表中标题的ptr infront

  • ptr->link = header->link makes ptr parallel to header, e.g.:

    ptr-> link = header-> link使ptr与标题并行,例如:

    ptr->data != header->data; // TRUE
    a = ptr->link;
    b = header->link;
    a->data == b->data // TRUE
    

where a and b are of type struct node*

其中a和b的类型为struct node *

#3


0  

The best way to understand is to draw it out.关于链接列表中节点的混淆

理解的最好方法是把它画出来。