简体   繁体   English

从头开始删除链表中的节点

[英]Node deletion in linked list from beginning

I want to delete the first node and return the value of the deleted node.我想删除第一个节点并返回已删除节点的值。 But I an getting this warning:但我收到了这个警告:

warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
 example=(**example).next;

So, my code does not work.所以,我的代码不起作用。 Can anyone help me to fix this?谁能帮我解决这个问题? Thanks.谢谢。

struct myStruct {
    int data;
    struct myStruct next;
}

int deleteNode(struct myStruct **example) {
    struct myStruct *temporary;
    if (temporary == NULL) { 
        emptyNode(temporary);   // this function only returns NULL
    }
    temporary = *example;
    example = (**example).next;
    free(temporary);

    return (**example).data;
}

This structure declaration contains at least two typos.此结构声明至少包含两个拼写错误。

struct myStruct 
{
  int data;
  struct myStruct next;
}

The first one is that there is no semicolon after the closing brace.第一个是右大括号后没有分号。 And the second one is that the data member next must have pointer type.第二个是数据成员 next 必须有指针类型。

It seems you mean看来你的意思

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

As for the error message then in this assignment至于错误信息,那么在这个作业中

example=(**example).next;

the left side hand operand has the type struct myStruct ** while the right hand side operand has the type struct myStruct * and these pointer types are not compatible.左侧操作数的类型为struct myStruct ** ,而右侧操作数的类型为struct myStruct * ,并且这些指针类型不兼容。 So the compiler issues an error.所以编译器发出错误。

Nevertheless the function in any case is invalid because you are using uninitialized variables like尽管如此,function 在任何情况下都是无效的,因为您使用的是未初始化的变量,例如

struct myStruct *temporary;
if(temporary==NULL) 
//...

The function interface is bad.because it is unclear what the function returns in case when it is called for an empty list. function 接口不好。因为不清楚 function 在调用空列表时返回什么。

The function can be declared and defined the following way. function 可以通过以下方式声明和定义。

int deleteNode( struct myStruct  **example, int *data )
{
    int success = *example != NULL;

    if ( success )
    {
        struct myStruct *temporary = *example;
        *example = ( *example )->next;
        *data = temporary->data;
        free( temporary );
    }

    return success;
}

And it can be called as it is shown below它可以被调用,如下图所示

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

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

int deleteNode( struct myStruct  **example, int *data )
{
    int success = *example != NULL;

    if ( success )
    {
        struct myStruct *temporary = *example;
        *example = ( *example )->next;
        *data = temporary->data;
        free( temporary );
    }

    return success;
}


int main(void) 
{
    struct myStruct *head = 0;

    //  fill the list

    int data;

    if ( deleteNode( &head, &data ) )
    {
        printf( "The deleted value is %d\n", data );
    }
    else
    {
        puts( "The list is empty." );
    }

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM