c - 链表、指针和节点

标签 c pointers linked-list

我正在尝试加深对链表、节点和指针的理解。我编写了一些代码,收到了一些编译错误,我不确定如何修复这些错误,希望在这里得到一些指导。下面是我的代码+底部的编译错误。

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

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

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

unsigned int removeNumber(LL_t * L, int target);

LL_t * LLcreate() {
    LL_t * ret = malloc(sizeof(LL_t));
    if (ret != NULL) {
        ret->head = NULL;
        ret->tail = NULL;
        }
    return ret;
}

// Adds a new element to the tail end of a list
void LLappend(LL_t * LL, int value) {
    node_t * newNode = malloc(sizeof(node_t));
    if (newNode != NULL) {
        newNode->data = value;
        LLappendNode(LL, newNode);
    }
}

void LLappendNode(LL_t * LL, node_t * newNode) {
    if (newNode != NULL) {
        newNode->next = NULL;
        if (LL->tail == NULL) {
            // empty list
            assert(LL->head == NULL);
            LL->head = newNode;
            LL->tail = newNode;
        } else {
            // non empty list
            LL->tail->next = newNode; // seg fault
            LL->tail = newNode;
        }
    }
}



// Post: removes data target from list L
unsigned int removeNumber(LL_t * LL, int target) {
int count=1;
node_t * curr = LL->head;

while(LL->head && LL->head->data == target){
    node_t * temp= LL->head;
    LL->head=LL->head->next;
    free(temp);
    count=0;
    }

while (curr->next!=NULL){
    node_t * temp = curr->next; 
    if (curr->next->data == target){
        curr->next = temp->next;
        curr=curr->next;
        free(temp); 
        count=0;
        return count;
        }
    }
}

int main(){
    int LL[6]={1,2,5,3,4,6};
    int i;
    LLcreate();
    for (i=0; i<10; i++)
        {
            LLappend(LL_t * LL, i);
        }
    for (i=0; i<10;i++)
        {
            printf("%d", LL[i]);
        }

}

编译错误

splcie.c:36:6: warning: conflicting types for 'LLappendNode' [enabled by default]
 void LLappendNode(LL_t * LL, node_t * newNode) {
      ^
splcie.c:32:9: note: previous implicit declaration of 'LLappendNode' was here
         LLappendNode(LL, newNode);
         ^
splcie.c: In function 'main':
splcie.c:84:22: error: expected expression before 'LL_t'
             LLappend(LL_t * LL, i);
                      ^
splcie.c:84:22: error: too few arguments to function 'LLappend'
splcie.c:28:6: note: declared here
 void LLappend(LL_t * LL, int value) {
      ^

最佳答案

错误 1:LL_t”之前的预期表达式 LLappend(LL_t * LL, i);

修复:在类型转换时将 LL_t * 括在括号中:LLappend((LL_t *) LL, i);

错误 2:警告:“LLappendNode”的类型冲突

void LLappendNode(LL_t * LL, node_t * newNode) {
  ^

之前的“LLappendNode”隐式声明位于此处 LLappendNode(LL, newNode);

修复:LLappend 中调用之前为 LLappendNode 提供原型(prototype)/声明:

void LLappendNode(LL_t * LL, node_t * newNode); //prototype

void LLappend(LL_t * LL, int value) {
    node_t * newNode = malloc(sizeof(node_t));
    if (newNode != NULL) {
        newNode->data = value;
        LLappendNode(LL, newNode);
    }
}

关于c - 链表、指针和节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36209108/

相关文章:

c++ - 链表节点组合器函数

c - 如何避免变量自动分配到我的指针指向的内存单元的情况?

c++ - 如何在不删除对象的情况下从 boost::ptr_vector 中删除指针?

c++ - 重载 << C++

cocoa - 与 NSDate 不兼容的指针类型

c++ - 为什么在单个变量上我需要指定内存地址而在数组上我不需要它?

java - 所有列表变量中的设置值都在更新

c - 在一台机器上构建 opencv exe 并在另一台没有安装 opencv 的机器上运行?

c - Perl 结构流向 C

c - 使用和不使用 & 传递指向函数的指针