I'm new to C and am creating a program that initializes two different linked lists of books. The goal of the program will eventually merge the data from the two linked lists into a dynamically allocated array of movie pointers. Once ordered by book title, and the second time ordered by the book author. The linked list I'm creating is supposed to be a doubly linked list. I'm having trouble with my mergeBooks() function in book.c which is supposed to add every book from each linked list to an array in the order that is specified by the sort type. I'm supposed to expand the dynamically allocated array every time a new book is added, and I'm not allowed to use functions such as realloc() or memcpy() to help achieve this.


I'm struggling with figuring out how to iterate through the nodes in each linked list and then add the items to the array. Since there are 2 lists, I'm not sure how I can add the contents of the 2nd list to the array without overwriting the first.


I tried searching online and found some posts discussing how to add elements from an array to a linked list but not the other way around. I would appreciate some help or guidance!

defs.h

Code:
	typedef struct{
		char *title; 
		char *author;
	} BookType;


	typedef struct Node {
	  BookType *data;
	  struct Node *prev;
	  struct Node *next;
	} BookNode; 


	typedef struct{
		BookNode *head;
		BookNode *tail;
	} BookList;
	
	typedef struct {
	  int size;
	  BookType **books;
	} BookArray;
	
	typedef enum{
		BY_TITLE,
		BY_AUTHOR
	}SortType;
book.c

Code:
	void initBook(char *t, char *a, BookType **b){
		*b = malloc(sizeof(BookType));
		(*b)->title = t; 
		(*b)->author = a;
	}




	void addBookToList(SortType s, BookList *list, BookType *b){
		BookNode* current;
		BookNode* previous;
		BookNode* temp;
		
		current = list->head;
		previous = NULL;
		
		
		temp = malloc(sizeof(BookNode));
		temp->data = b;
		temp->next = NULL;
		temp->prev = NULL;
		


		while(current != NULL){
			if(s == BY_TITLE){
				if(strcmp(b->title, current->data->title) < 0){
					break;
				}
			}
			if(s == BY_AUTHOR){
				if(strcmp(b->author, current->data->author) < 0){
					break;
				}
			}
			previous = current;
			current = current->next;
		}
		
		if(previous == NULL){
			list->head = temp;
			list->tail = temp;
		}
		else{
			previous->next = temp;
			temp->prev = previous;
			temp->next = current;
		}
		
		if(temp->next == NULL){
			list->tail = temp;
		}else{
	        current->prev = temp;
		}
		temp->next = current;
	}




	void mergeBooks(SortType s, BookArray *arr, BookList *listOne, BookList *listTwo){


		BookArray **newArr;
		newArr = calloc(arr->size + 1, sizeof(BookType*));
		
		BookNode* temp;
		temp = malloc(sizeof(BookNode));
		
		arr[0] = temp->data;
		temp->next = NULL;
		
		int i;
		for(i = 0 ; i < arr->size; ++i){
		if(s == BY_TITLE){
			listOne->head = temp;
			BookNode* tempTwo;
				tempTwo = malloc(sizeof(BookNode));
				temp->next = tempTwo;
				arr[i] = tempTwo->data;
				tempTwo->next = NULL;
				temp = tempTwo
			}
		if(s == BY_AUTHOR){
			listTwo->head = temp;
			BookNode* tempTwo;
				tempTwo = malloc(sizeof(BookNode));
				temp->next = tempTwo;
				arr[i] = tempTwo->data;
				tempTwo->next = NULL;
				temp = tempTwo
			}
		}	
		
		free(arr->books);
		arr->books = newArr;
		++(arr->size);
	}

main.c

Code:
	int main(){
		
		BookList *sarahsList = malloc(sizeof(BookList));
		BookList *davidsList = malloc(sizeof(BookList));
		
		sarahsList->head = NULL;
		sarahsList->tail = NULL;
		
		davidsList->head = NULL;
		davidsList->tail = NULL;	
		
		initBooks(sarahsList, davidsList);


		return 0;
		
	}
init.c

Code:
	void initBooks(BookList *listOne, BookList *listTwo){
	  BookType *b;


	  initBook("Hamlet", "William Shakespeare", &b);
	  addBookToList(BY_TITLE, BY_AUTHOR, b);
	  initBook("The Hobbit", "Tolkien", &b);
	  addBookToList(BY_TITLE, BY_AUTHOR, b);
	}