从链表中删除第一个节点,并在不分配或取消分配内存的情况下返回它。(C编程)

问题描述 投票:0回答:2

我不允许释放内存或分配新内存(该列表已经为我分配了)。所以从本质上讲,我试图写一个像

这样的函数
struct node* returnAndRemoveFirstNode(struct node* head)
{
    struct node* returned = head;
    struct node* temp = head->next;
    returned->next = NULL;
    head = temp;
    return returned;
}

这不起作用,因为当我将return-> next设置为null时,我还将head的下一个设置为null。我不确定如何解决此问题,我不确定有很多解决方法,只是不确定如何解决。对于形式为(1-> 2-> 3-> 4)的列表,原始列表和返回的节点都看起来像(1-> Null)

//Here is the node struct in case you need it
//I am not allowed to alter the struct..
struct node{
int data;
struct node *next;
};
c pointers linked-list dynamic-memory-allocation singly-linked-list
2个回答
0
投票
struct node* returnFirstNode(struct node** head)
{
    struct node* returned = *head;
    struct node* next = (*head)->next;
    *head = next;
    returned->next = NULL;
    return returned;
}

0
投票

我确定您不正确理解原始任务。

但是,从列表中删除第一个节点并返回它的函数可以通过以下方式查看

struct node * returnAndRemoveFirstNode( struct node **head )
{
    struct node *returned = *head;

    if ( *head != NULL ) 
    {
        *head = ( *head )-next;
        returned->next = NULL;
    }

    return returned;
}

通常请注意,列表可以为空。因此,指向头节点的指针可以等于NULL。

如果要遵循您评论中的描述

他的原始任务是从列表中删除包含以下内容的所有节点:特定数据,并将所有这些节点添加到其他列表中,返回它。

然后,该函数可以如下所示,如下所示。

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

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

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

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

    return success;
}

void output( struct node *head )
{
    for ( ; head != NULL; head = head->next )
    {
        printf( "%d -> ", head->data );
    }

    puts( "null" );
}

struct node * remove_if( struct node **head, int cmp( int data ) )
{
    struct node *new_list = NULL;

    for ( struct node **current = &new_list; *head != NULL; )
    {
        if ( cmp( ( *head )->data ) )
        {
            *current = *head;
            *head = ( *head )->next;
            ( *current )->next = NULL;
            current = &( *current )->next;
        }
        else
        {
            head = &( *head )->next;
        }
    }

    return new_list;
}

int odd( int data )
{
    return data % 2 != 0;
}

int even( int data )
{
    return data % 2 == 0;
}

int main(void) 
{
    const int N = 10;

    struct node *head = NULL;

    for ( int i = N; i != 0; --i ) push_front( &head, i );

    output( head );

    putchar( '\n' );

    struct node *even_head = remove_if( &head, even );

    output( head );
    output( even_head );

    putchar( '\n' );

    struct node *odd_head = remove_if( &head, odd );

    output( head );
    output( odd_head );

    putchar( '\n' );

    return 0;
}

程序输出为

1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> null

1 -> 3 -> 5 -> 7 -> 9 -> null
2 -> 4 -> 6 -> 8 -> 10 -> null

null
1 -> 3 -> 5 -> 7 -> 9 -> null
© www.soinside.com 2019 - 2024. All rights reserved.