meetze
asked on
free nodes in a link list
I am writting an appan I have to read info from a database so I am going to the info in a link list and right now I am just building my link list functions...well I can add info to the link list...but when it comes to free all the nodes in the link list when ever I go to free the "main_tail" node my program crashes...anyone have clue as to why this is happening?? The code I am using is below
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
struct file_collection
{
char *filename;
char *dir;
struct file_collection *prev, *next;
};
void addrec(struct file_collection **, struct file_collection**, char *, char *);
void allocspace(struct file_collection **, struct file_collection **, struct file_collection **);
void freeallnodes(struct file_collection **, struct file_collection **);
void main(void)
{
struct file_collection *main_top, *main_tail;
main_top=main_tail=NULL;
addrec(&main_top, &main_tail, "this is a test", "this is a dir test");
addrec(&main_top, &main_tail, "this is a test2", "this is a dir test2");
freeallnodes(&main_top, &main_tail);
}
void addrec(struct file_collection **main_top, struct file_collection **main_tail, char *temp_filename, char *temp_dir)
{
struct file_collection *curr, *top, *tail;
top = *main_top;
tail = *main_tail;
allocspace(&top, &curr, &tail);
curr->filename = temp_filename;
curr->dir = temp_dir;
*main_top = top;
*main_tail = tail;
}
void allocspace(struct file_collection **top, struct file_collection **curr, struct file_collection **tail)
{
*curr = (struct file_collection *)malloc(sizeof(struct file_collection));
(*curr)->prev=(*curr)->nex t=NULL;
if (*top == NULL)
{
*top = *curr;
*tail = *curr;
}
else
{
(*curr)->prev = *tail;
(*tail)->next = *curr;
(*tail) = (*curr);
}
}
void freeallnodes(struct file_collection **main_top, struct file_collection **main_tail)
{
struct file_collection *p, *curr;
for(p=(*main_top);p != NULL; p=p->next)
{
curr = p->next;
if (curr != NULL)
free(curr);
}
if (*main_top != NULL)
free(*main_top);
if (*main_tail != NULL)
free(*main_tail);
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
struct file_collection
{
char *filename;
char *dir;
struct file_collection *prev, *next;
};
void addrec(struct file_collection **, struct file_collection**, char *, char *);
void allocspace(struct file_collection **, struct file_collection **, struct file_collection **);
void freeallnodes(struct file_collection **, struct file_collection **);
void main(void)
{
struct file_collection *main_top, *main_tail;
main_top=main_tail=NULL;
addrec(&main_top, &main_tail, "this is a test", "this is a dir test");
addrec(&main_top, &main_tail, "this is a test2", "this is a dir test2");
freeallnodes(&main_top, &main_tail);
}
void addrec(struct file_collection **main_top, struct file_collection **main_tail, char *temp_filename, char *temp_dir)
{
struct file_collection *curr, *top, *tail;
top = *main_top;
tail = *main_tail;
allocspace(&top, &curr, &tail);
curr->filename = temp_filename;
curr->dir = temp_dir;
*main_top = top;
*main_tail = tail;
}
void allocspace(struct file_collection **top, struct file_collection **curr, struct file_collection **tail)
{
*curr = (struct file_collection *)malloc(sizeof(struct file_collection));
(*curr)->prev=(*curr)->nex
if (*top == NULL)
{
*top = *curr;
*tail = *curr;
}
else
{
(*curr)->prev = *tail;
(*tail)->next = *curr;
(*tail) = (*curr);
}
}
void freeallnodes(struct file_collection **main_top, struct file_collection **main_tail)
{
struct file_collection *p, *curr;
for(p=(*main_top);p != NULL; p=p->next)
{
curr = p->next;
if (curr != NULL)
free(curr);
}
if (*main_top != NULL)
free(*main_top);
if (*main_tail != NULL)
free(*main_tail);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I did it like this
void freeallnodes(struct file_collection **main_top, struct file_collection **main_tail)
{
struct file_collection *p, *curr;
for(p=(*main_top);p != NULL;)
{
curr = p;
if (curr != NULL)
{
p=p->next;
free(curr);
}
}
if (*main_top != NULL)
free(*main_top);
if (*main_tail != NULL)
free(*main_tail);
}
but now even though my main_top is freed and I am checking to see if it is NULL it still falls through the statement...any reason for this??
void freeallnodes(struct file_collection **main_top, struct file_collection **main_tail)
{
struct file_collection *p, *curr;
for(p=(*main_top);p != NULL;)
{
curr = p;
if (curr != NULL)
{
p=p->next;
free(curr);
}
}
if (*main_top != NULL)
free(*main_top);
if (*main_tail != NULL)
free(*main_tail);
}
but now even though my main_top is freed and I am checking to see if it is NULL it still falls through the statement...any reason for this??
ASKER
well i noticed that I do not need to free main_top or main_tail...thanks for the help..
ASKER
well i noticed that I do not need to free main_top or main_tail...thanks for the help..
for( p=(*main_top); p!= NULL; p=curr) {
curr = p->next;
if ( p != NULL)
free( p);
}