Hi,
I am very frustrated right now. Sometimes my functions work and sometimes they dont. This is how I have implemented certain double link list functions.
For example, the destroy function doesnt work, the print doesnt work, EVEN THE LIST SIZE doesnt work.
Eg., if I do int a = list_length(L), then do printf(%i,a), the code runs. But if I do printf(%i, list_length(L)), it crashes.
Code:
/* Implementation file for the list ADT using a doubly (two-way) linked list */
#include <stdio.h>
#include <stdlib.h>
#include "listADT.h"
typedef struct node {
itemType data;
struct node *next;
struct node *previous;
} nodeType;
struct listTag {
nodeType *head;
nodeType *tail;
int size;
};
/* Functions Definitions (Implementation)
* **************************************/
//allocates memory space for a new list, initializes its fields and
//returns a pointer to the allocated space
List list_create() {
List L;
L = (List)malloc(sizeof(List));
if (L)
{
L->head = NULL;
L->tail = NULL;
L->size = 0;
}
return L;
}
void list_destroy( List L ) {
nodeType *P;
while (L->head!=NULL)
{
P = L->head;
L->head = L->head->next;
P->previous = NULL;
P->next = NULL;
free(P);
}
free(L);
L=NULL;
}
bool list_isEmpty( const List L ) {
return (L->size==0);
}
int list_length( const List L ) {
return (L->size);
}
void list_print( const List L ) {
if(L && L->head)
{
nodeType *P;
printf("( ");
P = L->head;
while (P != NULL)
{
printf("[%i]", P->data);
P = P->next;
if (P != NULL)
printf(" -> ");
}
printf(" )\n");
}
}
bool list_insertFront( itemType value, List L ) {
if(L)
{
nodeType *N;
N = (nodeType*)malloc(sizeof(nodeType));
if (N!=NULL)
{
N->previous = NULL;
N->next = NULL;
N->data = value;
if (L->size==0)
{
L->head = N;
L->tail = N;
}
else
{
N->next = L->head;
L->head->previous = N;
L->head = N;
}
L->size++;
}
return true;
}
return false;
}
int main()
{
List L = list_create();
list_insertFront(9,L);
list_insertFront(9,L);
list_insertFront(9,L);
//int a = list_length(L);
//printf("%i",list_length(L));
list_destroy(L);
//list_print(L);
system("PAUSE");
exit(EXIT_SUCCESS);
}
I have tried everything, asked some pro CS people, but just cant seem to understand what is wrong.
Thanks a lot for your help.