【问题标题】:How can I display only list[i] from the function?如何仅显示函数中的 list[i]?
【发布时间】:2022-01-09 04:02:58
【问题描述】:

我在下面重写了这段代码,在主函数中我调用了 push_front 函数,如下所示: push_front( &link[i], rollnumber, src, dst, gentime ); 我的问题是如何只显示链接[i],例如链接[1]?

 int push_front( Node **head, int rollnumber, int src, int dst, double gentime )
{
    Node *new_node = malloc( sizeof( Node ) );
    int success = new_node != NULL;

    if ( success )
    {
        new_node->rollnumber=rollnumber;
        new_node->src = src;
        new_node->dst=dst;
        new_node->gentime=gentime;
        new_node->next = *head;
        *head = new_node;
    }

    return success;
}

void output( Node **head )
{
    for( Node *current =*head; current != NULL; current = current->next )
    {
        //printf( "%d %d %d %0.1f ",current->rollnumber, current->src, current->dst, current->gentime );
        printf("Roll Number:%2d\t",current->rollnumber);
        printf("src:%2d\t", current->src);
        printf("dest:%2d\t", current->dst);
        printf("gentime:%0.1f\n", current->gentime);
    }
    printf( "%s\n", "NULL" );
}

void display( Node **set, size_t n )
{
    for ( size_t i = 0; i <= n; i++ )
    {
        output( set++ );
        putchar( '\n' );
    }
} 

显示所有列表时得到的输出如下:

Roll Number: 6  src: 1  dest:10 gentime:64.1
Roll Number: 5  src: 1  dest: 4 gentime:51.5
Roll Number: 4  src: 1  dest:17 gentime:38.0
Roll Number: 3  src: 1  dest:20 gentime:25.9
Roll Number: 2  src: 1  dest:15 gentime:13.9
Roll Number: 1  src: 1  dest: 3 gentime:1.6
NULL

Roll Number: 6  src: 2  dest:17 gentime:64.8
Roll Number: 5  src: 2  dest: 6 gentime:52.6
Roll Number: 4  src: 2  dest: 5 gentime:39.5
Roll Number: 3  src: 2  dest:20 gentime:26.0
Roll Number: 2  src: 2  dest:19 gentime:14.0
Roll Number: 1  src: 2  dest: 4 gentime:1.9
NULL

...等所有变量:src

list[i] 是基于变量:src。那么,我怎样才能只打印 src: 1 呢?

【问题讨论】:

  • 由于列表的结构,每个节点都通过指针连接到下一个节点,(如果非循环,则从头到尾)因此它们的访问方式与数组不同(例如 struct.array 的数组)。 )。然后下一个节点将不会由list[i] 表示,而是由list = list-&gt;next 表示。
  • @ryyker 不能只显示我已经拥有的功能的第一个链接吗?
  • 不是这样。输出函数会出现for( Node *current =*head; current != NULL; current = current-&gt;next ) 的问题。一方面,中心表达式应该检查current-&gt;next != NULL 而不是current!= NULL。但是,当使用未初始化的指针值 current-&gt;next 进行赋值时,第三个表达式也会出错。如果只想打印头节点的成员,则将头指针作为参数传递给只接受节点指针的函数,并输出成员值
  • 请参阅答案中的编辑,例如输出一个节点的成员的非常简单的方法。 (没有错误检查,如果计划使用,您应该添加。)
  • 在一个评论中您使用了短语“第一个链接”,在另一个评论中您使用了“一个列表”。在您的原始帖子中使用了“列表 [i]”。 ('list[i]' 是数组表示法,在处理列表时通常不使用。)如果我没记错的话,它们似乎都暗示了同样的事情。 IE。您想要输出列表的一个节点。如果这是正确的,那么我帖子中的最新编辑显示了如何做到这一点。如果不是,请澄清您的问题。请记住,“列表”是称为“节点”的指针的集合。每个节点都指向所包含数据的一个实例以及另一个节点。

标签: c list function


【解决方案1】:

修改您的原型以通过列表。因为您没有更改任何内容,所以它不需要是指向指针的指针。然后遍历列表,直到条件满足条件:

在伪代码中:(假设您有一个Node *next 成员...)

void display( Node *set, size_t n )//you do not need size of list here
{                                  //but you can use n to indicate node to output
    Node *temp = set;
    
    if(temp->src == n)//check head node
    {
        output(temp)
        return;
    }
    while(temp->next)//check all remaining nodes in list
    {
        temp = temp->next;
        if(temp->src == n)
        {
            output(temp)
            return;
        }
    }
    ...

编辑以解决 cmets 中有关输出单个节点的问题。创建列表后,可以从任何可以看到列表的位置调用output()

void output( Node *h )//
{
    printf("Roll Number:%2d\t",h->rollnumber);
    printf("src:%2d\t", h->src);
    printf("dest:%2d\t", h->dst);
    printf("gentime:%0.1f\n", h->gentime);
}

例如,在一个例子中main()

int main(void)
{
    Node *head = malloc(sizeof(*head));
    memset(head, 0, sizeof *head); //initialize
    push_front(&head, 1, 10, 100, 1.0); //first node
    push_front(&head, 2, 20, 200, 2.0); //first node
    push_front(&head, 3, 30, 200, 2.0);//first node,
    output(head); //outputs 1st node
    head = head->next;//points to 2nd node
    output(head); //outputs 2nd node
    head = head->next;//points to third node
    output(head); //outputs 3rd node.
    ...

        

请注意,由于您的代码使用了push_front 方法,因此每个新节点都放置在列表的开头,使每个新添加的节点成为第一个节点
正如评论中提到的,在实际应用程序中使用之前,需要通过错误检查来支持这一点。

此外,不要忘记在代码中包含一些 free() 语句以防止内存泄漏。

【讨论】:

  • 感谢您抽出宝贵时间,但仍然不知道如何实现只显示一个列表
  • 编辑后我不明白如何显示我想要的特定头像,例如不是第一个而是第三个作为第一个。
  • 在上例中的第三个 'push_front()' 之后,您有头节点。通过设置 'head = head->next' 两次,将两个节点遍历到第三个节点。现在 head 指向第三个节点。将其传递给输出。
  • @wajaap - 见最后编辑的例子。
猜你喜欢
  • 1970-01-01
  • 2018-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-15
  • 2012-10-26
  • 2016-09-18
  • 2017-03-11
相关资源
最近更新 更多