简体   繁体   English

以下程序的输出是什么? 您如何跟踪这样的程序?

[英]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 nextNULL 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. 我不明白的是程序如何按顺序打印存储在s2B ), s5E ), s1A ), s4D )和s3C )中的值。 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*/ 
  • Here, ptr is a pointer to the trip (ie, struct road_trip ) . 在这里, ptr是指向trip的指针(即 struct road_trip
  • when you want to access the members of a structure using a pointer, then -> ( 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), and s3 (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 because ptr points to s2 and thus ptr->place is same as s2.place (so using the Structure dereference operator the values of place member accessed) 第一次迭代 B被打印,因为ptr指向s2 ,因此ptr->places2.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.

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