Я пытаюсь реализовать односвязанны список в C, но я получаю эту ошибку: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;
}
0) 'узел * Temp = (узел *) таНос (SizeOf (узел));' malloc'd затем присвоить '= временный указатель;', 'temp' был переписан. это утечка памяти. temp как временный рабочий указатель, определяемый только 'node * temp = NULL;' (или 'node * temp;'). не нужно 'free (temp);' – BLUEPIXY
Что такое 'list' и' node'? В качестве наблюдения я не понимаю, почему 'llSize()', который возвращает длину списка, должен выделять и освобождать память, а не объявлять локальную структуру, следуя цепочке указателей - если вообще. –
Нежный способ сказать это: это не Java или C#. Вы не выделяете, если вам не нужно * создавать * что-то. Пример: 'malloc (sizeof (node))' не имеет бизнеса в 'llSize'. '(* new) .next = (node *) malloc (sizeof (node))' не имеет бизнеса в 'llAddToFront' или' llInsertAfterCurr'. И 'node * temp = (node *) malloc (sizeof (node))' абсолютно положительно не имеет никакого бизнеса в 'llDeleteAfterCurr'. Я настоятельно рекомендую вам просмотреть как C-указатели, так и любую тысячу онлайн-примеров связанных списков, которые * работают *. – WhozCraig