[英]What is the output of the following program? How do you trace such a program?
I have to determine the output of the program below. 我必须确定以下程序的输出。 The answer is BEADC
, but i'm not sure how to trace the program. 答案是BEADC
,但是我不确定如何跟踪程序。 Here's is what i know: 这是我所知道的:
We start off by declaring a structure called road_trip
with data type trip
. 我们首先声明一个数据类型为trip
名为road_trip
的结构。 In the main program we assign values to the member place
. 在主程序中,我们为成员place
分配值。 Then a linked list is created where the starting node is s2
and ending node is s3
. 然后创建一个链表,其中起始节点为s2
,结束节点为s3
。 Because you can see that the address of starting node is being stored in a separate pointer ptr
which is often known as head pointer. 因为您可以看到起始节点的地址存储在单独的指针ptr
,所以通常将其称为头指针。 s3
is the ending of the linked list because if you see the s3
you may notice that next
of s3
is NULL
. s3
是链表的结尾,因为如果看到s3
您可能会注意到s3
next
是NULL
。 That means s3
is not referring any other node. 这意味着s3
没有引用任何其他节点。
What I don't understand is how does the program prints the values stored in s2
( B
), s5
( E
), s1
( A
), s4
( D
), and s3
( C
) in that order. 我不明白的是程序如何按顺序打印存储在s2
( B
), s5
( E
), s1
( A
), s4
( D
)和s3
( C
)中的值。 I'm sure it has to do with the two lines I wrote comments after. 我确定这与我在之后写评论的两行有关。 An explanation would be of great help. 一个解释会很有帮助。
#include <stdio.h>
typedef struct road_trip
{
char place;
struct road_trip* next;
}trip;
int main (void)
{
trip s1, s2, s3, s4, s5, s6;
trip *ptr;
s1.place = 'A';
s2.place = 'B';
s3.place = 'C';
s4.place = 'D';
s5.place = 'E';
s5.next = &s1;
ptr = &s2;
s1.next = &s4;
s3.next = NULL;
s4.next = &s3;
s2.next = &s5;
while (ptr != NULL)
{
printf("%c ", ptr -> place); /*I don't get this line*/
ptr = ptr -> next; /*I don't get this line*/
}
return 0;
}
printf("%c ", ptr -> place); /*I don't get this line*/ ptr = ptr -> next; /*I don't get this line*/
ptr
is a pointer to the trip
(ie, struct road_trip
) . 在这里, ptr
是指向trip
的指针(即 struct road_trip
) 。 ->
( Structure dereference operator) operator is used 当您想使用指针访问结构的成员时,则使用->
(“ 结构解除引用运算符”)运算符 The answer is "BEADC", but i'm not sure how to trace the program. 答案是“ BEADC”,但我不确定如何跟踪程序。
s5.next = &s1;
ptr = &s2;
s1.next = &s4;
s3.next = NULL;
s4.next = &s3;
s2.next = &s5;
The above part of the code is useful to trace the program... 上面的代码对跟踪程序很有用...
Here 这里
ptr
points to s2
ptr
指向s2
and: 和:
next
member of s2
points to s5
s2
next
成员指向s5
next
member of s5
points to s1
s5
next
成员指向s1
next
member of s1
points to s4
s1
next
成员指向s4
next
member of s4
points to s3
s4
next
成员指向s3
next
member of s3
points to NULL
s3
next
成员指向NULL
You've given the correct explanation in your question 您在问题中给出了正确的解释
What I don't understand is how does the program prints the values stored in
s2
(B),s5
(E),s1
(A),s4
(D), ands3
(C) in that order 我不明白的是程序如何按顺序打印存储在s2
(B),s5
(E),s1
(A),s4
(D)和s3
(C)中的值
Now when you iterate using ptr
this way : 现在,当您以这种方式使用ptr
进行迭代时:
while (ptr != NULL)
{
printf("%c ", ptr -> place);
ptr = ptr -> next;
}
Tracing the loop : 跟踪循环:
- on first iteration
B
gets printed becauseptr
points tos2
and thusptr->place
is same ass2.place
(so using the Structure dereference operator the values ofplace
member accessed) 在第一次迭代B
被打印,因为ptr
指向s2
,因此ptr->place
与s2.place
相同(因此使用Structure取消引用运算符访问了place
成员的值)
then ptr = ptr->next
is same as ptr = s2->next
and thus ptr = &s5
as s2->next
points to s5
然后ptr = ptr->next
是相同ptr = s2->next
并且因此ptr = &s5
作为s2->next
点至s5
Similarly, this loop continues and you get the output in the way their next
members are connected 同样,此循环继续进行,您将以连接其next
成员的方式获得输出
The loop ends after printing s3
because s3->next
points to NULL
and thus ptr
becomes =NULL
after printing s3
循环在打印s3
之后结束,因为s3->next
指向NULL
,因此在打印s3
之后ptr
变为=NULL
Further reading : 进一步阅读:
This is a typical implementation of linked lists (click to know more) . 这是链表的典型实现(单击以了解更多信息) 。
This type of list where one member of structure points to next structure of the list is known as singly linked list (click to know more) . 结构的一个成员指向列表的下一个结构的这种类型的列表称为单链接列表 (单击以了解更多信息) 。
ptr is a pointer to s2, so displaying char ptr->place will display place of trip s2. ptr是指向s2的指针,因此显示char ptr-> place将显示行程s2的位置。 Then ptr is being changed to next pointer - next of s2 is s5. 然后将ptr更改为下一个指针-s2的下一个是s5。 Displaying ptr -> place will display now place of trip s5. 显示ptr-> place将显示行程s5的现在位置。 Then ptr changes to next of s5, which is s1 and it goes untill it reaches s3, which next pointer is NULL. 然后,ptr更改为s5的下一个,即s1,直到到达s3(下一个指针为NULL)为止。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.