Blank 3
Blank 3
h>
#include <stdlib.h>
#include <string.h>
// Define the structure for a book
typedef struct Book {
int bookID;
char title[100];
char author[100];
struct Book *next; // Pointer to the next book in the list
} Book;
// Function to create a new book record
Book* createBook(int bookID, char title[], char author[]) {
Book *newBook = (Book *)malloc(sizeof(Book));
newBook->bookID = bookID;
strcpy(newBook->title, title);
strcpy(newBook->author, author);
newBook->next = NULL;
return newBook;
}
// Function to add a book to the end of the list
void addBook(Book **head, int bookID, char title[], char
author[]) {
Book *newBook = createBook(bookID, title, author);
if (*head == NULL) {
*head = newBook;
return;
}
Book *temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newBook;
}
// Function to search for a book by title
void searchBook(Book *head, char title[]) {
Book *temp = head;
while (temp != NULL) {
if (strcmp(temp->title, title) == 0) {
printf("Book Found:\n");
printf("Book ID: %d\n", temp->bookID);
printf("Title: %s\n", temp->title);
printf("Author: %s\n", temp->author);
return;
}
temp = temp->next;
}
printf("Book with title '%s' not found.\n", title);
}
// Function to display all book records
void displayBooks(Book *head) {
if (head == NULL) {
printf("No books in the library.\n");
return;
}
Book *temp = head;
printf("Book Records:\n");
while (temp != NULL) {
printf("Book ID: %d\n", temp->bookID);
printf("Title: %s\n", temp->title);
printf("Author: %s\n", temp->author);
printf("-------------------------\n");
temp = temp->next;
}
}
// Main function
int main() {
Book *head = NULL;
int choice, bookID;
char title[100], author[100];
while (1) {
printf("\nLibrary Menu:\n");
printf("1. Add Book\n");
printf("2. Search Book by Title\n");
printf("3. Display All Books\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter Book ID: ");
scanf("%d", &bookID);
getchar(); // To consume newline character
printf("Enter Title: ");
fgets(title, sizeof(title), stdin);
title[strcspn(title, "\n")] = '\0'; // Remove trailing
newline
printf("Enter Author: ");
fgets(author, sizeof(author), stdin);
author[strcspn(author, "\n")] = '\0'; // Remove trailing
newline
addBook(&head, bookID, title, author);
break;
case 2:
printf("Enter Title to Search: ");
getchar(); // To consume newline character
fgets(title, sizeof(title), stdin);
title[strcspn(title, "\n")] = '\0'; // Remove trailing
newline
searchBook(head, title);
break;
case 3:
displayBooks(head);
break;
case 4:
printf("Exiting the program.\n");
return 0;
default:
printf("Invalid choice! Please try again.\n");