我正在尝试在C中实现单链表,但我不断收到此错误:
*'./output.o/'出错:双重免费或损坏(快速登陆):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;
}
答案 0 :(得分:0)
这不是一个答案,但对评论来说太过分了。我不知道你的struct
声明,但是一个迭代来查找链表中的项目数非常简单,根本不需要强制转换。
int llSize(list *myList){
int count = 0;
while (myList) {
count++;
myList = myList->next;
}
return count;
}