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 byptr
andheader
will point to the same node (orNULL
).如果它们指向某些不同的节点,那么由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 theheader
node. This means the data ofptr
would be the same as the data inheader
.ptr->link
would also be the same asheader->link
-
ptr->link=header
movesptr
infront ofheader
in the list -
ptr->link = header->link
makesptr
parallel toheader
, e.g.:ptr-> link = header-> link使ptr与标题并行,例如:
ptr->data != header->data; // TRUE a = ptr->link; b = header->link; a->data == b->data // TRUE
ptr = header将生成指向头节点的指针。这意味着ptr的数据与标题中的数据相同。 ptr-> link也与header-> link相同
ptr-> link = header移动列表中标题的ptr infront
where a
and b
are of type struct node*
其中a和b的类型为struct node *
#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 byptr
andheader
will point to the same node (orNULL
).如果它们指向某些不同的节点,那么由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 theheader
node. This means the data ofptr
would be the same as the data inheader
.ptr->link
would also be the same asheader->link
-
ptr->link=header
movesptr
infront ofheader
in the list -
ptr->link = header->link
makesptr
parallel toheader
, e.g.:ptr-> link = header-> link使ptr与标题并行,例如:
ptr->data != header->data; // TRUE a = ptr->link; b = header->link; a->data == b->data // TRUE
ptr = header将生成指向头节点的指针。这意味着ptr的数据与标题中的数据相同。 ptr-> link也与header-> link相同
ptr-> link = header移动列表中标题的ptr infront
where a
and b
are of type struct node*
其中a和b的类型为struct node *