wmail: debug macro fixes.
[dockapps.git] / wmmp3 / song_hash.c
blobfc46609d89e6b73a7c233aeee2afa38d488b600a
1 #include "song_hash.h"
3 #define HASH_TABLE_SIZE 11
5 struct hash_elt *Table[HASH_TABLE_SIZE];
7 int hash_fn(int num);
8 void free_elt(struct hash_elt *elt);
10 int hash_fn(int num)
12 return (num % HASH_TABLE_SIZE);
15 void insert_song(int track_num, char *title, char *filename)
17 int hash_value;
18 struct hash_elt *hash_list;
19 struct hash_elt *new_elt;
22 new_elt = (struct hash_elt *) malloc(sizeof(struct hash_elt));
23 if (new_elt != NULL)
25 new_elt->track_num = track_num;
26 new_elt->title = (char *)strdup(title);
27 new_elt->filename = (char *)strdup(filename);
28 new_elt->next = NULL;
31 hash_value = hash_fn(track_num);
32 hash_list = Table[hash_value];
33 if (hash_list != NULL) {
34 while (hash_list->next != NULL) {
35 hash_list = hash_list->next;
37 hash_list->next = new_elt;
39 else
41 Table[hash_value] = new_elt;
45 struct hash_elt *get_song(int track_num)
47 int hash_value;
48 struct hash_elt *hash_list;
50 hash_value = hash_fn(track_num);
51 hash_list = Table[hash_value];
52 if (hash_list) {
53 while ((hash_list->track_num != track_num) &&
54 (hash_list->next != NULL)) {
55 hash_list = hash_list->next;
57 if (hash_list->track_num == track_num) {
58 return hash_list;
60 else {
61 return NULL;
64 else {
65 return NULL;
69 void free_elt(struct hash_elt *elt)
71 if (elt != NULL)
73 if (elt->next != NULL) {
74 free_elt(elt->next);
76 free(elt);
80 void empty_hash()
82 int i;
84 for (i = 0; i < HASH_TABLE_SIZE; i++) {
85 free_elt(Table[i]);
86 Table[i] = NULL;
90 void print_hash()
92 int i;
93 struct hash_elt *hash_list;
95 for (i = 0; i < HASH_TABLE_SIZE; i++) {
96 printf("%d:\n", i);
97 hash_list = Table[i];
98 while (hash_list != NULL) {
99 printf("\t%d: %s\n",
100 hash_list->track_num,
101 hash_list->title);
102 hash_list = hash_list->next;