【问题标题】:C - How to split a linked list with struct type dataC - 如何使用结构类型数据拆分链表
【发布时间】:2020-04-21 17:15:00
【问题描述】:

我有一个问题,需要使用函数将单链表拆分为 2 部分:Split(n1, n2) 其中 n1 = 元素的位置,n2 是要拆分的元素数。我设法想出了一个算法和一个测试程序:

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

 struct Node
{
int value;
struct Node *next;
 };

int push_front( struct Node **head, int value )
{
struct Node *new_node = malloc( sizeof( struct Node ) );
int success = new_node != NULL;

if ( success )
{
    new_node->value = value;
    new_node->next = *head;

    *head = new_node;
}

return success;
}

void display( const struct Node *head )
{
for ( ; head != NULL; head = head->next )
{
    printf( "%d -> ", head->value );
}

//puts( "null" );
}

struct ListPair
{
struct Node *head1;
struct Node *head2;
};


struct ListPair split( struct Node **head, size_t pos, size_t n )
{
struct ListPair p = { .head1 = NULL, .head2 = NULL };

struct Node **current1 = &p.head1;
struct Node **current2 = &p.head2;

for ( size_t i = 0; *head != NULL && i != pos; i++ )
{
    *current2 = *head;
    *head = ( *head )->next;
    ( *current2 )->next = NULL;
    current2 = &( *current2 )->next;
}

while ( *head != NULL && n-- )
{
    *current1 = *head;
    *head = ( *head )->next;
    ( *current1 )->next = NULL;
    current1 = &( *current1 )->next;
}

while ( *head != NULL )
{
    *current2 = *head;
    *head = ( *head )->next;
    ( *current2 )->next = NULL;
    current2 = &( *current2 )->next;
}

return p;
}


int main(void) 
{
const size_t N = 15;
struct Node *head = NULL;

srand( ( unsigned int )time( NULL ) );  

for ( size_t i = 0; i < N; i++ )
{
    push_front( &head, rand() % N );
}

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

struct ListPair p = split( &head, 6, 3  );

display( head );
display( p.head1 );
display( p.head2 );

return 0;
}

结果是:

12 -> 14 -> 3 -> 0 -> 12 -> 5 -> 4 -> 0 -> 2 -> 14 -> 1 -> 0 -> 6 -> 0 -> 5 -> null

null
5 -> 4 -> 0 -> 2 -> 14 -> null
12 -> 14 -> 3 -> 0 -> 12 -> 1 -> 0 -> 6 -> 0 -> 5 -> null

但不知道如何将上述实现到我的链表中,即:

typedef struct address_t
{
char name[30];
char storage[5];
char screen[5];
int price;
} address;


typedef address elementtype;
typedef struct node node;
typedef struct node{
elementtype element;
node *next;
};

node *root, *cur, *prev;

请帮忙:(

【问题讨论】:

  • 您展示了我为某个问题提供的代码,问题是什么> 使用 malloc 创建节点后,用所需值填充其数据成员。
  • 我认为首先你必须编写自己的代码。之后,如果您有任何问题,请提出问题。

标签: c singly-linked-list


【解决方案1】:

概述

据我了解,您只是想从单个链接列表中删除子列表,例如list = el1 -&gt; el2 -&gt; el5 -&gt; el6 -&gt; el3 -&gt; null。当您调用split(list, 2, 2) 时,结果将是两个列表list and list1list = el1 -&gt; el2 -&gt; el3 -&gt; nulllist1 = el5 -&gt; el6 -&gt; null

但真正不清楚的是您所面临的实际问题是什么,到目前为止您为使其发挥作用做了什么尝试?

回答

从我阅读问题的方式来看,每个列表元素的内容看起来并不重要。那么,为什么不直接从示例中取出 struct Node 并将字段 int value 替换为 address value,如下所示:

typedef struct {
  char name[30];
  char storage[5];
  char screen[5];
  int price;
} address;

struct Node
{
  address value;
  struct Node *next;
 };

您的代码中存在一些您可能想要更改的问题。 1. 您的 split 函数将传入的指针 head 设置为 NULL。这可能不是想要的行为 2. 你从split(...) 返回一个结构。由于 C 中的所有内容都是按值传递的,您可能会考虑将(双)指针也传递给 split(...) 并将结果 ListPair 放在那里,因为这样在从功能 3. 通常你不会将_t 用于结构名称,但不会用于 typedef 本身。 4.你typedef结构address_t和typedef类型elementtype,但从未使用address 5.你有一个没有名字的typedef

typedef struct node{
 elementtype element;
 node *next;
};

只需删除该行中的typedef,因为您之前已经在一行中进行了 typedef。

【讨论】:

  • 他已经展示了将一个列表分成两个列表的代码。 Ge 不知道如何填充他的数据结构的字段,那么它在提供的代码中显示了多个字段。
  • 好的。感谢您的澄清。不幸的是,我还不允许在问题下面写 cmets...
猜你喜欢
  • 2014-06-12
  • 1970-01-01
  • 2016-07-30
  • 1970-01-01
  • 2021-09-15
  • 1970-01-01
  • 2022-01-02
  • 2013-03-02
  • 2017-11-10
相关资源
最近更新 更多