I am currently working on a library management system project.
I have implemented a hash table for storing book information and a hash table for storing student information.
in two of the modules in this project named as book.h/.c and student.h/.c, I have two identical functions which free the memory.
Is there any way I can rewrite these two functions as one function in my shared_func.h/.c and call it in both module without including header files of both modules in shared_func.h/.c
Any help will be appreciated.
book.h
typedef struct bnode {
...
some variables
struct bnode *next;
} Book;
void free_all_books();
book.c
#include "book.h"
static Student *book_table[MAX_SIZE] = {NULL};
....
void free_all_books()
{
for( i = 0; i < MAX_SIZE; i++ ) {
Book *head = book_table[i];
if( head == NULL ) {
continue;
} else {
Book *temp;
while ( head != NULL ) {
temp = head;
head = head->next;
free(temp);
}
}
}
}
student.h
typedef struct snode {
...
some variables
struct snode *next;
} Student;
void free_all_students();
student.c
#include "student.h"
static Student *student_table[MAX_SIZE] = {NULL};
....
void free_all_students()
{
for( i = 0; i < MAX_SIZE; i++ ) {
Student *head = student_table[i];
if( head == NULL ) {
continue;
} else {
Student *temp;
while ( head != NULL ) {
temp = head;
head = head->next;
free(temp);
}
}
}
}