469,890 Members | 1,421 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,890 developers. It's quick & easy.

Initialize the HashTable

The declaration is:
1
2 struct ListNode;
3 typedef struct ListNode *Position;
4 typedef Position List;
5 struct HashTbl;
6 typedef struct HashTbl *HashTable;
7
8 struct ListNode{
9 char name[4];
10 int GroupID;
11 Position Next;
12 };
13
14 struct HashTbl
15 {
16 int TableSize;
17 List *TheLists;
18 };
19
20 //The Initialization Function:
21
22 struct HashTbl *InitializeTable(int TableSize)
23 {
24 HashTable H;
25 int i;
26
27 H->TableSize=TableSize+3;
28
29 /* Allocate Table */
30 H=(HashTable)malloc(sizeof(struct HashTbl));
31 if(H == NULL)
32 fprintf(stderr,"Out of space!!!");
33
34 /* Allocate array of lists */
35 H->TheLists=(List *)malloc(sizeof(List)*H-
>TableSize);
36 if(H->TheLists == NULL)
37 fprintf(stderr,"Out of space!!!");
38
39 for(i = 0; i < H->TableSize; i++)
40 H->TheLists[i]=NULL;
41 return H;
42 }
43
44
Function Call:
HashTable AllThePersons= InitializeTable(1000*1000);

My idea is to Initialize the hash table with nodes of type (struct
ListNode *=List) pointers, distribute1000*1000 unit spaces to store
pointers.

The routine passed through the compiling but shut off with excepting
when running.

Could you help me?

Oct 18 '07 #1
1 7112
westlaker <we*******@126.comwrites:
The declaration is:
1
2 struct ListNode;
3 typedef struct ListNode *Position;
4 typedef Position List;
5 struct HashTbl;
6 typedef struct HashTbl *HashTable;
7
8 struct ListNode{
9 char name[4];
10 int GroupID;
11 Position Next;
I think lists are much clearer if you make the link explicit:
struct ListNode *Next;
Now everyone can see at a glance that this is a linked list.
12 };
13
14 struct HashTbl
15 {
16 int TableSize;
17 List *TheLists;
18 };
19
20 //The Initialization Function:
21
22 struct HashTbl *InitializeTable(int TableSize)
An unsigned type is often better for a size (in particular size_t).
If you use an int, you should check that the value it is not negative.
23 {
24 HashTable H;
25 int i;
26
27 H->TableSize=TableSize+3;
28
29 /* Allocate Table */
30 H=(HashTable)malloc(sizeof(struct HashTbl));
Too late. You have used H on line 27. The cast is not needed (and
may be a problem). The c.l.c idiom is:

H = malloc(sizeof *H);

(I had to check that HashTable is indeed a synonym for a pointer to a
struct HashTbl.)
31 if(H == NULL)
32 fprintf(stderr,"Out of space!!!");
Having found H to be NULL, it is not safe to just carry on!
33
34 /* Allocate array of lists */
35 H->TheLists=(List *)malloc(sizeof(List)*H->TableSize);
36 if(H->TheLists == NULL)
37 fprintf(stderr,"Out of space!!!");
Ditto.
38
39 for(i = 0; i < H->TableSize; i++)
40 H->TheLists[i]=NULL;
41 return H;
42 }
--
Ben.
Oct 18 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Ante | last post: by
4 posts views Thread by A.M-SG | last post: by
3 posts views Thread by MioTheGreat | last post: by
2 posts views Thread by Mike | last post: by
5 posts views Thread by Victor Paraschiv | last post: by
7 posts views Thread by J L | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.