Need help on writing subject summary in C linked list. The semester project Libr
ID: 3696693 • Letter: N
Question
Need help on writing subject summary in C linked list.
The semester project Library Book Management System will be based on the data type struct book as defined below.
struct book{
int ID;
char Title[50];
char Author[50];
int SubjectCode;
int BorrowingAge;
char BorrowerName[50];
struct book *nextbook;
};
Each book that is in the library list will have a ID, Title, Author Name, Subject Code, Borrowing Age (reflects the number of days passed after it has been borrowed from the library), name of the Borrower and a pointer nextBook which points to a struct book.
Subject code for different subjects as follows:
000 – General works, Computer science and Information, 100 – Philosophy and psychology, 200 – Religion, 300 – Social sciences, 400 – Language, 500 – Pure Science, 600 – Technology, 700 – Arts & recreation, 800 – Literature, 900 – History & geography. All the code you are writing will need to be placed in prj1.c
1. void subjectSummary (struct book * head);
In this function you are to count and display the number of books you have in the library for different subjects (do this using the subject codes). The books borrowed by the users should not get included in the count.
#prj1.c
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
struct book{
int ID;
char Title[50];
char Author[50];
int SubjectCode;
int BorrowingAge;
char BorrowerName[50];
struct book *nextbook;
};
struct book *head = NULL;
struct book *curr = NULL;
struct book* create_list(char title[50], char author[50], int subcode)
{
printf(" creating list with headnode as [%d] ",subcode);
struct book *ptr = (struct book*)malloc(sizeof(struct book));
if(NULL == ptr)
{
printf(" Node creation failed ");
return NULL;
}
ptr->subcode = subcode;
ptr->nextbook = NULL;
head = curr = ptr;
return ptr;
}
struct test_struct* add_to_list(int subcode, bool add_to_end)
{
if(NULL == head)
{
return (create_list(subcode));
}
if(add_to_end)
printf(" Adding node to end of list with value [%d] ",subcode);
else
printf(" Adding node to beginning of list with value [%d] ",subcode);
struct book *ptr = (struct book*)malloc(sizeof(struct book));
if(NULL == ptr)
{
printf(" Node creation failed ");
return NULL;
}
ptr->subcode = subcode;
ptr->nextbook = NULL;
if(add_to_end)
{
curr->nextbook = ptr;
curr = ptr;
}
else
{
ptr->nextbook = head;
head = ptr;
}
return ptr;
}
struct book* search_in_list(int subcode, struct book **prev)
{
struct test_struct *ptr = head;
struct test_struct *tmp = NULL;
bool found = false;
printf(" Searching the list for value [%d] ",subcode);
while(ptr != NULL)
{
if(ptr->subcode == subcode)
{
found = true;
break;
}
else
{
tmp = ptr;
ptr = ptr->nextbook;
}
}
if(true == found)
{
if(prev)
*prev = tmp;
return ptr;
}
else
{
return NULL;
}
}
int delete_when_borrow(int subcode)
{
struct book *prev = NULL;
struct book *del = NULL;
printf(" borrow a book [%d] from list ",subcode);
del = search_in_list(subcode,&prev);
if(del == NULL)
{
return -1;
}
else
{
if(prev != NULL)
prev->nextbook = del->nextbook;
if(del == curr)
{
curr = prev;
}
else if(del == head)
{
head = del->nextbook;
}
}
free(del);
del = NULL;
return 0;
}
void print_list(void)
{
struct book *ptr = head;
printf(" -------Printing list for a subject code------- ");
while(ptr != NULL)
{
printf(" [%d] ",ptr->subcode);
ptr = ptr->nextbook;
}
printf(" -------Printing list End------- ");
return;
}
int main(void)
{
int i = 0, ret = 0;
struct book *ptr = NULL;
print_list();
for(i = 5; i<10; i++)
add_to_list(i,true);
print_list();
for(i = 4; i>0; i--)
add_to_list(i,false);
print_list();
for(i = 1; i<10; i += 4)
{
ptr = search_in_list(i, NULL);
if(NULL == ptr)
{
printf(" Search [subcode = %d] failed, no such element found ",i);
}
else
{
printf(" Search passed [subcode = %d] ",ptr->subcode);
}
print_list();
ret = delete_when_borrow(i);
if(ret != 0)
{
printf(" delete [subcode = %d] failed, no such element found ",i);
}
else
{
printf(" delete [subcode = %d] passed ",i);
}
print_list();
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.