C单链表抛出错误

C单链表抛出错误,c,singly-linked-list,C,Singly Linked List,我试图在C中实现一个单链表,但我一直遇到以下错误: “/output.o/”中出现错误:双重释放或损坏(fasttop):0x09dd008* 我对C相当陌生,认为我的实现相当不错,但我似乎不知道这个问题是从哪里来的。任何帮助都将不胜感激 这是名单 #include <stdio.h> #include "list.h" #include <string.h> list llInit(){ list llist; llist.head = (node *)

我试图在C中实现一个单链表,但我一直遇到以下错误:

“/output.o/”中出现错误:双重释放或损坏(fasttop):0x09dd008*

我对C相当陌生,认为我的实现相当不错,但我似乎不知道这个问题是从哪里来的。任何帮助都将不胜感激

这是名单

#include <stdio.h>
#include "list.h"
#include <string.h>
list llInit(){
    list llist;
    llist.head = (node *)malloc(sizeof(node));
    llist.tail = (node *)malloc(sizeof(node));
    llist.curr = (node *)malloc(sizeof(node));
    return llist;
}

int llSize(list *myList){
    int count = 0;
    node *next = (node *)malloc(sizeof(node));
    if((*myList).head!=NULL){
        next = (*myList).head;
        while((*next).next!=NULL){
            count++;
            next = (*next).next;
        }
    }
    free(next);
    return count;
}

int llAddToFront(list *myList, char *toStore){
    if(toStore!=NULL){
        node *new = (node *)malloc(sizeof(node));
        (*new).string = (char *)malloc(sizeof(char));
        (*new).next = (node *)malloc(sizeof(node));
        (*new).string = strdup(toStore);
        if((*myList).head!=NULL){
            (*new).next = (*myList).head;
            if((*myList).head==(*myList).curr){
                (*myList).curr = new;
            }
            (*myList).head = new;
        }else{
            (*myList).head = new;
            (*myList).curr = new;
            (*myList).tail = new;
        }
        return 1;
    }
    return 0;
}

int llDeleteFirst(list *myList){
    if(llSize(myList)){
        if((*myList).curr==(*myList).head){
            (*myList).curr = (*myList).head->next;
        }
        (*myList).head = (*myList).head->next;
        free((*myList).head);
        return 1;
    }
    return 0;
}

int llAddToBack(list *myList, char *toStore){
    if(toStore!=NULL){
        node *new = (node *)malloc(sizeof(node));
        (*new).string = strdup(toStore);
        (*new).next = NULL;
        if((*myList).tail!=NULL){
            (*(*myList).tail).next = new;
            (*myList).tail = new;
        }else{
            (*myList).tail = new;
            (*myList).head = new;
            (*myList).curr = new;
        }
        return 1;
    }
    return 0;
}

int llInsertAfterCurr(list *myList, char *string){
    if(string!=NULL){
        node *new = (node*)malloc(sizeof(node));
        (*new).string = (char *)malloc(sizeof(char));
        (*new).next = (node *)malloc(sizeof(node));
        (*new).string = string;
        if((*myList).curr==NULL){
            (*myList).head = new;
            (*myList).curr = new;
            (*myList).tail = new;
        }else{
            (*new).next = (*(*myList).curr).next;
            (*(*myList).curr).next = new;
        }
        return 1;
    }
    return 0;
}

int llDeleteAfterCurr(list *myList){
    if(llSize(myList)&&(*myList).tail!=(*myList).curr){
        node *temp = (node *)malloc(sizeof(node));
        temp = (*(*(*myList).curr).next).next;
        free((*(*myList).curr).next);
        (*(*myList).curr).next = temp;
        free(temp);
        return 1;
    }
    return 0;
}

void llClear(list *myList){
    while(llSize(myList)){
        llDeleteFirst(myList);
    }
    (*myList).head = NULL;
    (*myList).tail = NULL;
    (*myList).curr = NULL;
}

int llNext(list *myList){
    if(llSize(myList)&&(*myList).curr!=(*myList).tail){
        (*myList).curr = (*(*myList).curr).next;
        return 1;
    }
    return 0;
}

int llRewind(list *myList){
    if(llSize(myList)){
        (*myList).curr = (*myList).head;
        return 1;
    }
    return 0;
}

int llIterate(list *myList, fun f){
    if(llSize(myList)){
        llRewind(myList);
        while((*myList).curr!=(*myList).tail){
            f((*(*myList).curr).string);
            llNext(myList);
        }
        return 1;
    }
    return 0;
}
#包括
#包括“list.h”
#包括
列出它(){
列表列表列表;
llist.head=(node*)malloc(sizeof(node));
llist.tail=(node*)malloc(sizeof(node));
llist.curr=(node*)malloc(sizeof(node));
返回李斯特;
}
int llSize(列表*myList){
整数计数=0;
node*next=(node*)malloc(sizeof(node));
if((*myList.head!=NULL){
next=(*myList).head;
while((*next).next!=NULL){
计数++;
下一步=(*next).next;
}
}
免费(下一个);
返回计数;
}
int llAddToFront(列表*myList,字符*toStore){
如果(toStore!=NULL){
node*new=(node*)malloc(sizeof(node));
(*new).string=(char*)malloc(sizeof(char));
(*new).next=(node*)malloc(sizeof(node));
(*new).string=strdup(toStore);
if((*myList.head!=NULL){
(*new.next=(*myList.head);
if((*myList.head==(*myList.curr){
(*myList).curr=新;
}
(*myList)。头=新;
}否则{
(*myList)。头=新;
(*myList).curr=新;
(*myList).tail=新;
}
返回1;
}
返回0;
}
int llDeleteFirst(列表*myList){
if(llSize(myList)){
if((*myList.curr==(*myList.head){
(*myList.curr=(*myList.head->next;
}
(*myList.head=(*myList.head->next;
自由((*myList.head);
返回1;
}
返回0;
}
int llAddToBack(列表*myList,字符*toStore){
如果(toStore!=NULL){
node*new=(node*)malloc(sizeof(node));
(*new).string=strdup(toStore);
(*new).next=NULL;
if((*myList.tail!=NULL){
(*(*myList.tail).next=new;
(*myList).tail=新;
}否则{
(*myList).tail=新;
(*myList)。头=新;
(*myList).curr=新;
}
返回1;
}
返回0;
}
int-llInsertAfterCurr(列表*myList,字符*string){
if(字符串!=NULL){
node*new=(node*)malloc(sizeof(node));
(*new).string=(char*)malloc(sizeof(char));
(*new).next=(node*)malloc(sizeof(node));
(*new).string=string;
if((*myList).curr==NULL){
(*myList)。头=新;
(*myList).curr=新;
(*myList).tail=新;
}否则{
(*new.next=(*(*myList.curr).next;
(*(*myList).curr.next=new;
}
返回1;
}
返回0;
}
int llDeleteAfterCurr(列表*myList){
if(llSize(myList)&(*myList.tail!=(*myList.curr){
node*temp=(node*)malloc(sizeof(node));
temp=(*(*(*myList).curr.next).next;
免费((*(*myList.curr.next);
(*(*myList).curr.next=温度;
免费(临时);
返回1;
}
返回0;
}
void llClear(列表*myList){
while(llSize(myList)){
llDeleteFirst(myList);
}
(*myList).head=NULL;
(*myList).tail=NULL;
(*myList).curr=NULL;
}
int llNext(列表*myList){
if(llSize(myList)&&(*myList.curr!=(*myList.tail){
(*myList.curr=(*(*myList.curr).next;
返回1;
}
返回0;
}
int llRewind(列表*我的列表){
if(llSize(myList)){
(*myList.curr=(*myList.head);
返回1;
}
返回0;
}
智力文盲(列表*我的列表,乐趣f){
if(llSize(myList)){
ll倒带(myList);
while((*myList.curr!=(*myList.tail){
f((*(*myList).curr.string);
llNext(myList);
}
返回1;
}
返回0;
}
还有我正在使用的简单测试

#include <stdio.h>
#include "list.h"

int main(){
    list myList = llInit();
    llAddToFront(&myList,"To my friends ");
    llAddToBack(&myList,"Hello");
    llInsertAfterCurr(&myList, "I say ");
    /* iterate list front to back */
    node *aNode = myList.head;
    while(aNode!=NULL) {
        printf("%s",aNode->string);
        aNode = aNode->next;
    }
    printf("\nClearing List\n");
    llClear(&myList);
    return 0;
}
#包括
#包括“list.h”
int main(){
list myList=llInit();
llAddToFront(&myList,“致我的朋友”);
llAddToBack(&myList,“你好”);
llInsertAfterCurr(我说是myList);
/*前后迭代列表*/
节点*阳极=myList.head;
while(阳极!=NULL){
printf(“%s”,阳极->字符串);
阳极=阳极->下一步;
}
printf(“\n搜索列表\n”);
llClear(&myList);
返回0;
}

这不是答案,但评论太多了。我不知道您的
struct
声明,但是在链表中查找项数的迭代非常简单,根本不需要强制转换

int llSize(list *myList){
    int count = 0;
    while (myList) {
        count++;
        myList = myList->next;
    }
    return count;
}

0)
node*temp=(node*)malloc(sizeof(node))malloc'd然后分配
temp=指针
temp
已重写。这是内存泄漏。临时工作指针定义为临时工作指针,仅
节点*temp=NULL(或
节点*temp;
)。不需要
免费(临时)什么是
列表
节点
?作为观察,我不明白为什么返回列表长度的
llSize()
需要分配和释放内存,而不是声明一个遵循指针链的本地结构(如果有的话)。没有温和的说法:这不是Java或C。除非你需要创造一些东西,否则你不会分配。例如:
malloc(sizeof(node))
llSize
中没有业务<代码>(*new).next=(node*)malloc(sizeof(node))
llAddToFront
llInsertAfterCurr
中没有业务。而
node*temp=(node*)malloc(sizeof(node))
绝对没有业务在
llDeleteAfterCurr
中。我强烈建议您查看C指针和上千个在线链接列表示例中的任何一个<代码>列表和节点是不同的类型<代码>列表
节点*
成员
,和
cur
,而