Hi All, I've been testing some extremely simple code about a linked list.
What I keep doing is actually writing some parts of a linked list and testing them.
Below are my codes:
---------------------------------------------------------------------------
/* new.h */
#ifndef NEW_H
#define NEW_H
typedef int ListEntry;
typedef struct listnode{
ListEntry info;
struct listnode *next;
}ListNode;
typedef struct list{
ListNode *head;
ListNode *tail;
int count;
}List;
void CreatList(List *list);
int ListSize(List *list);
#endif
-------------------------------------------------------
/* new.c */
#include <stdio.h>
#include <stdlib.h>
#include "new.h"
void CreatList(List *list){
list = (List*)malloc(sizeof(List));
if(list==NULL) Error("No memory available.");
list->head = list->tail = NULL;
list->count = 0;
}
int ListSize(List *list){
return (list->count);
}
--------------------------------------------
/* newtest.c */
#include <stdio.h>
#include <stdlib.h>
#include "new.h"
main(){
List *list;
CreatList(list);
printf("%d\n", ListSize(list));
}
--------------------------------------------------------
I have 3 files: .h, .c and test.c
I try to test if I successfully created a new linked list
If I did, I should have got a result ListSize(list) == 0;
But I didn't...below are the compiling and running results:
With one system, I got:
[phe@f438-07 hw4]$ #include "new.h"
[phe@f438-07 hw4]$ gccx newtest.c new.o
[phe@f438-07 hw4]$ gccx newtest.c new.c
[phe@f438-07 hw4]$ ./a.out
-2130706312
With another system, I got:
[phe@inceptor mergesort]$ #include "new.h"
[phe@inceptor mergesort]$ gccx newtest.c new.o
[phe@inceptor mergesort]$ gccx newtest.c new.c
[phe@inceptor mergesort]$ ./a.out
33949697
Then, what's wrong...??? I am really frustrated, this is rather simple code and test, but I got so wired errors...Shouldn't the ListSize(list) be zero when I just created a list?
Or, anything wrong with memory?
Really appreciate your help!!!