gpt4 book ai didi

C单链表抛出错误

转载 作者:行者123 更新时间:2023-11-30 19:43:37 24 4
gpt4 key购买 nike

我正在尝试用 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;
}

以及我正在使用的简单测试

#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;
}

最佳答案

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

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

关于C单链表抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29450171/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com