I've started C programming yesterday and tried to implement a linked list (just really really basic).
So far everything works pretty fine except for freeing a list.
First, here's my code:
#include <stdio.h>
#include <stdlib.h>
/*
* Struct for elements of the linked list
*/
struct element {
struct element* nextelement;
int value;
};
/*
* Struct for a list itself
*/
struct liste {
struct element* headelement;
};
/*
* Function to create new elements
*/
struct element* createelement(int value) {
struct element* dummyelement = malloc(sizeof(struct element));
dummyelement->nextelement = NULL;
dummyelement->value = value;
return dummyelement;
}
/*
* Function to create new (empty) lists
*/
struct liste* createlist() {
struct liste* dummylist = malloc(sizeof(struct liste));
dummylist->headelement = NULL;
return dummylist;
}
/*
* Add an element to a given list
*/
void addelement(struct liste* liste, int value) {
struct element* dummyelement = createelement(value);
if (liste->headelement == NULL) {
liste->headelement = dummyelement;
} else {
struct element* iterelement = liste->headelement;
while (iterelement->nextelement != NULL) {
iterelement = iterelement->nextelement;
}
iterelement->nextelement = dummyelement;
}
}
/*
* Plot the elements of a given list
*/
void plotlist(struct liste* liste) {
if (liste->headelement != NULL) {
struct element* iterelement = liste->headelement;
printf("%d\n", iterelement->value);
while (iterelement->nextelement != NULL) {
iterelement = iterelement->nextelement;
printf("%d\n", iterelement->value);
}
} else {
printf("Where is my head?\n");
}
}
/*
* This should completely remove the list, but it fails...
*/
void removelist(struct liste* liste) {
if (liste->headelement != NULL) {
struct element* iterelement = liste->headelement;
struct element* nextelement = iterelement->nextelement;
free(iterelement);
while (nextelement != NULL) {
iterelement = nextelement;
nextelement = iterelement->nextelement;
free(iterelement);
}
}
free(liste);
}
int main(void) {
/*
* Creates a new list
* Plots the (empty) list
* Adds two elements to the list
* Plots the list again
* Removes the list
* Last plot shouldn't really happen, but it does.
*/
struct liste* mylist = createlist();
printf("First plot.\n");
plotlist(mylist);
addelement(mylist, 1);
addelement(mylist, 2);
printf("Second plot.\n");
plotlist(mylist);
removelist(mylist);
printf("Third plot.\n");
plotlist(mylist);
return 0;
}
I get the following output:
First plot.
Where is my head?
Second plot.
1
2
Third plot.
33
Well, obviously the '33' is my problem. I really don't know how this can be the output... Furthermore I don't know why my 'removelist' isn't working properly. I am freeing everything which I've allocated with 'malloc'. What am I doing wrong?