如何删除链接列表的头和所有节点

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

我正在尝试编写两个函数:

  1. 删除并返回链接列表的标题,如果列表为空,则返回-1。>
  2. #include <stdlib.h>
    #include <stdio.h>
    #include <assert.h>
    #include "LL.h"
    
    
    int LLremoveHead(LL_t * intlist) 
    {
        if (intlist == NULL)
        {
            return -1;
        }
        LL_t * tmp = intlist;
        intlist = intlist->next;
    
        //delete tmp;
        //return intlist;
        return tmp;
    
    }
    
  1. 删除列表的所有节点
  2. void LLdestroy(LL_t * intlist) 
    {
        LL_t * current = *intlist;
        LL_t * next;
    
        while (current != NULL) 
        { 
            next = current->next;
            free(current); 
            current = next; 
        }
    
        *intlist = NULL;
    }
    

这两个代码将无法编译,我也不知道为什么。我对链接列表不熟悉,有人知道如何解决这些问题吗?

我在编译期间不断遇到的错误:

error: 'LL_t' {aka 'struct <anonymous>'} has no member named 'next'

intlist = intlist->next;


warning: returning 'LL_t *' {aka 'struct <anonymous> *'} from a function with return type 'int' makes integer from pointer without a cast [-Wint-conversion]

return tmp;

       ^~~

头文件:LL.h

#ifndef _LL_H_
#define _LL_H_


typedef struct _node {
    int data;
    struct _node * next;
} node_t


typedef struct {
    node_t * head;
    node_t * tail;
} LL_t;


// Creates and returns a new, empty list
LL_t * LLcreate();

// Adds a new element to the tail end of a list
void LLappend(LL_t * intlist, int value);

// Outputs the list elements in sequence from head to tail
void LLprint(LL_t * intlist);


// Return 1 iff target is in intlist; 0 otherwise.
int LLsearch(LL_t * intlist, int target);

// joins different L1, L2 such that L1 <- L1 + L2, frees L2
void LLcatenate(LL_t * L1, LL_t * L2);


// joins different L1, L2 such that L1 <- L1 + L2, frees L2
void LLinsert(LL_t * L1, int value);

// Recycles a list and all the nodes it contains
void LLdestroy(LL_t * intlist);

// Removes and returns the head of the list; 
// returns -1 if empty
int LLremoveHead(LL_t * intlist);

#endif // _LL_H_

我正在尝试编写两个函数:删除并返回链接列表的头部,如果列表为空,则返回-1 #include #include #include #...

c++
1个回答
0
投票
typedef struct {
    node_t * head;
    node_t * tail;
} LL_t;
© www.soinside.com 2019 - 2024. All rights reserved.