Question
So I have this function in my C program it is a doubly linked list it takes a title[30] and a genre[30] and a rating.
So I have this function in my C program it is a doubly linked list it takes a title[30] and a genre[30] and a rating. I need help changing up this function so it is able to sort also what it is sorting so if the user enter a genre that is the same as another genre that was enter they will be side by side in the list.
Example what I mean
Title genre rating
Hi horror 4
Hello comedy 2
fun horror 1
//here is what it should look like sorted and if possible sort the rating too from greatest to smallest so if both items have genre: horror and one has the rating 4 and the other one a rating 1 the one with the rating 4 should be printed first
Title genre rating
Hi horror 4
fun horror 1
Hello comedy 2
the sorting takes place based on genre and then rating
The function down below is the doubly linked list you can edit this code if needed
movieInfo insert(char title[],char genre[],int rating, movieInfo** head, movieInfo** tail) {
movieInfo* newBlock = NULL; movieInfo* beforeElement = NULL; movieInfo* afterElement = NULL;
newBlock = (movieInfo*)malloc(sizeof(movieInfo)); newBlock->title = (char*)malloc((strlen(title) + 1)); newBlock->genre = (char*)malloc((strlen(genre) + 1));
if (newBlock == NULL) { printf("No memory was allocated "); return **head; }
strcpy(newBlock->title, title); strcpy(newBlock->genre, genre);
newBlock->prev = newBlock->next = NULL;
if (*head == NULL) { *head = *tail = newBlock; return**head; }
else if (strcmp((*head)->title, title) >= 0) { newBlock->next = *head; (*head)->prev = newBlock; *head = newBlock; }
else { beforeElement = *head; afterElement = (*head)->next;
while (afterElement != NULL) { if (strcmp(afterElement->title, title) >= 0) { break; } beforeElement = afterElement; afterElement = afterElement->next; } newBlock->prev = beforeElement; newBlock->next = afterElement; beforeElement->next = newBlock; if (afterElement == NULL) { *tail = newBlock; } else { afterElement->prev = newBlock; }
}
return **head; }
The sorting no matter what needs to be done in that function
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started