C-如何用结构类型数据分割链表

问题描述 投票:-2回答:1

我有一个问题,需要使用以下函数将单链列表拆分为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;

请帮助:(

c singly-linked-list
1个回答
0
投票

概述

据我所知,您只是想从单个链接列表中删除一个子列表,例如list = el1 -> el2 -> el5 -> el6 -> el3 -> null。当您调用split(list, 2, 2)时,结果将是两个列表list and list1list = el1 -> el2 -> el3 -> nulllist1 = el5 -> el6 -> 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中的所有内容都是按值传递的,因此您可能会考虑也将[double]指针传递给split(...)并将结果ListPair放在其中,因为从C返回时,不必复制ListPair。功能3.通常,您不会将_t用于结构名称,而不会将其用于typedef本身。4.您键入了结构address_t的定义,并用elementtype键入了类型,但从未使用过address5.您有一个typedef,没有名称

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

只需删除该行中的typedef,因为您之前已经做过typedef。

© www.soinside.com 2019 - 2024. All rights reserved.