【问题标题】:How to create a copy of a Linked List to another address in C? [closed]如何在 C 中创建链接列表的副本到另一个地址? [关闭]
【发布时间】:2018-09-02 08:03:52
【问题描述】:

假设您有一个链接列表,并且您想要制作不同的副本并向该列表添加许多元素,但您不知道数量。每个链接列表必须有不同的地址改变。链表:

struct path {
   int node;
   struct path *next;
};

每次我将链表的起始地址发送给函数时,函数会在另一个地址复制链表,并在链表的末尾添加值(多次) .

我实现了以下代码以在链表上创建和添加新元素:

/* Head and Tail for Linked Lists */
struct path *pathHead = NULL;
struct path *pathTail = NULL;

void newPathElement(int node) {

struct path *rv = malloc(sizeof (struct path));
rv->node = node;

if (pathHead == NULL) {
    pathHead = rv;
    pathHead -> next = NULL;
    pathTail = pathHead;
} else {
    pathTail -> next = rv;
    pathTail = rv;
    pathTail -> next = NULL;
}
}

注意我想制作链表的不同副本。每个副本都有其头部和尾部,每个副本的列表都会有所不同。

函数签名如下:

struct path *copyPath(struct path *head, struct path *tail, int node){

}

【问题讨论】:

  • 创建一个新的空链表。循环遍历您的第一个链表,并将每个元素添加到新链表中。
  • StackOverflow 不是免费的编码服务。所以希望你try to solve your own problem first。请更新您的问题以在minimal reproducible example 中显示您已经尝试过的内容。如需更多信息,请参阅How to Ask,并拨打tour :)
  • @Barmar 我知道,但我需要帮助。我不知道该怎么做。如何创造新的?与:struct path *newPath = Null
  • @SaeedRahmani 你能从头开始创建一个简单的列表,例如包含 3 个节点吗?
  • 第一个链表是如何创建的?以同样的方式创建新的链表。

标签: c list linked-list copy singly-linked-list


【解决方案1】:

我们初学者应该互相帮助。:)

事实上,编写一个将新节点附加到列表的函数或将其代码合并到复制列表的函数中就足够了。

你来了。

#include <stdio.h>
#include <stdlib.h>

struct path 
{
    int node;
    struct path *next;
};

int append( struct path **head, int node )
{
    struct path *tmp = malloc( sizeof( struct path ) );
    int success = tmp != NULL;

    if ( success )
    {
        tmp->node = node;
        tmp->next = NULL;

        while ( *head ) head = &( *head )->next;

        *head = tmp;
    }

    return success;
}

struct path * copy( const struct path *source )
{
    struct path *destination = NULL;

    for ( ; source; source = source->next ) append( &destination, source->node );

    return destination;
}

void display( const struct path *head )
{
    for ( ; head; head = head->next )
    {
        printf( "%d ", head->node );
    }
}

int main(void) 
{
    struct path *first_list = NULL;
    const int N = 10;

    for ( int i = 0; i < N; i++ ) append( &first_list, i );

    display( first_list );
    putchar( '\n' );

    struct path *second_list = copy( first_list );

    display( second_list );
    putchar( '\n' );

    return 0;
}

程序输出是

0 1 2 3 4 5 6 7 8 9 
0 1 2 3 4 5 6 7 8 9 

您可以通过以下方式使函数copy更高效

struct path * copy( const struct path *source )
{
    struct path *destination = NULL;

    for ( struct path **tmp = &destination; source; source = source->next ) 
    {
        append( tmp, source->node );
        tmp = &( *tmp )->next;
    }       

    return destination;
}

【讨论】:

  • 每次目的地都不一样?这意味着我需要很多不同的副本。
  • 您的 append() 函数效率不高。您遍历整个列表以附加一个新节点。使用尾指针,您可以立即追加到列表中。当然,你必须在之后更新尾指针......
  • @machine_1 当我们运行时:struct path *head = Null 被认为是一个新地址?如果我重复这个命令 10 次,所有这些都不一样?
  • @saeed 调用函数追加时头部被修改一次。即使你多次调用它,它也不会多次修改它。
  • @machine_1 我需要一个复制功能,以便每次都有不同的头部和尾部。例如,我有一个列表,然后我需要一个带有新头尾和末尾添加节点的副本。我会多次重复这个过程。
猜你喜欢
  • 2014-01-29
  • 1970-01-01
  • 2021-01-20
  • 1970-01-01
  • 2022-01-22
  • 2021-08-24
  • 1970-01-01
  • 2021-12-18
  • 2017-06-01
相关资源
最近更新 更多