Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
2 views30 pages

Project Report

The document outlines a project report for a library management system developed in C, utilizing a singly linked list to manage book data. Key functionalities include adding, deleting, searching, and sorting books based on various attributes, with a focus on efficient memory management. The implementation features a menu-driven interface and employs selection sort for organizing the inventory.

Uploaded by

sayfulrafi122
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views30 pages

Project Report

The document outlines a project report for a library management system developed in C, utilizing a singly linked list to manage book data. Key functionalities include adding, deleting, searching, and sorting books based on various attributes, with a focus on efficient memory management. The implementation features a menu-driven interface and employs selection sort for organizing the inventory.

Uploaded by

sayfulrafi122
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Project Report

Course code: CSE 1302


Course Title: Data Structures Lab
Section: 01
Semester: Fall
Submitted to: Somonindro Roy

Lecturer

Submitted by
Group-2
Eusha Mohtasim (241014028)
Kazi Sadat Hossain (241014032)
Sabiha Tabasuum (241014035)
Zarin Tasnim (241014073)

Submission Date: 24/12/2024


Problem Statement

You are tasked with developing a library management system that uses a linked

list to store book data dynamically. The system must allow the librarian to:

1. Add new books to the inventory.

2. Search for books by a specified attribute (e.g., Title, Author, Book ID, or

Price) using either Linear Search or Binary Search.

3. Sort the inventory based on a user-specified criterion (e.g., Title, Author,

Price, or Publication Year) using any sorting algorithm.

4. Display the inventory in a tabular format after sorting or searching.


Data structures are very important in programming, especially in C, because they help store
and manage data efficiently. In C, data structures like arrays, linked lists, stacks, queues,
trees, and graphs make it easier to organize and use data. For example, arrays are good when
you need to access data in order or by position, but they cannot change size easily. Linked
lists solve this problem by using nodes connected with pointers, making it easy to add or
remove items and adjust memory as needed. Pointers in C also allow direct control of
memory, which is helpful for creating more advanced structures like trees and graphs, used
in tasks like organizing databases or managing networks. Stacks and queues, built using
arrays or linked lists, are useful in solving problems like searching data or scheduling tasks.
Choosing the right data structure can make your program faster, use less memory, and work
better overall.

Linked List

A linked list is a way to store items in a sequence, but unlike arrays, it uses nodes. Each node
holds the data and a pointer to the next node in the list. This makes it easy to add or remove
items because you just update the pointers, without needing to shift other items. For example,
if you have a list of numbers, each number is stored in a node, and each node points to the
next number. Linked lists are flexible with memory but can be slower to search since you
have to go through each node one by one

A singly linked list is a type of linked list where each node has two parts: data and a pointer
to the next node. It moves in one direction, from the first node to the last. For example, in a
list of numbers, each node stores one number and points to the next. It is simple and uses less
memory but is slower to move backward or access previous nodes.

A doubly linked list improves on this by adding another pointer to each node, one pointing to
the next node and one pointing to the previous node. This allows movement in both
directions, making it easier to insert, delete, or traverse. However, it uses more memory
because of the extra pointer in each node.

.
Searching and sorting

Searching and sorting are important techniques in programming to organize or find data.
Sorting methods like selection sort and bubble sort are simple but effective for small data
sets. In selection sort, the algorithm repeatedly finds the smallest (or largest) item from the
unsorted part of the list and swaps it with the first unsorted item. This process continues until
the entire list is sorted. Bubble sort works by comparing each pair of adjacent items and
swapping them if they are in the wrong order. This "bubbling" of larger elements to the end
of the list continues for multiple passes until the list is sorted. While these methods are easy
to understand and implement, they are slower for large lists compared to advanced sorting
techniques

Linear Search

Linear search is a simple way to find an item in a list. It checks each item one by one,
starting from the first, until it finds the target or reaches the end of the list. For example, to
find "5" in a list, it compares "5" with each number until it matches or confirms it's not there.
The time it takes depends on the size of the list. In the worst case, it checks all items O(n),
and in the best case, it finds the target right away O(1).

Binary Search

Binary search is a fast way to find an item in a sorted list. It works by dividing the list into
two halves and checking the middle item. If the target is smaller, it looks in the left half; if
it’s larger, it looks in the right half. This process repeats until the target is found or the list is
empty. For example, to find "5," it compares "5" with the middle number, then narrows the
search to the correct half. Binary search is much faster than linear search for large lists, with
a time complexity of O(log n) because the list is divided in half at each step.

Selection Sort

Selection sort is a simple way to arrange items in order. It works by finding the smallest item
in the list and swapping it with the first item. Then, it finds the next smallest item and swaps
it with the second item. This process repeats until the whole list is sorted. For example, in a
list of numbers, it selects the smallest number and moves it to the front, then continues with
the rest. The time complexity is O(n²) because it compares each item with all others,
especially for larger lists.

Implementation

The key points of the implementation of this library management system revolve around the
use of a linked list to manage book records. Each book is represented as a node in the linked
list, where the node stores details like book ID, title, author, price, and publication year. The
system provides functions to add books, delete books by ID, search for books based on
different criteria (ID, title, author), and display the inventory. Books can also be sorted based
on attributes like title, author, price, or publication year using the qsort function. The
program dynamically allocates memory for each node using malloc and deallocates it using
free to manage memory efficiently. The menu-driven interface allows the user to interact
with the system, providing options for all major operations. Input handling is done with fgets
to ensure safe string reading, and memory management is handled manually for each node
created. The program showcases basic linked list operations, sorting algorithms, and
memory management in C. In this code singly linked list will be used.The sorting method
used in this code is Selection Sort.

Solution:

#include <stdio.h>

#include <stdlib.h>

typedef struct Book {


int book_id;

char title[50];

char author[50];

float price;

int publication_year;

} Book;

typedef struct Node {

Book book;

struct Node* next;

} Node;

Node* create_node(Book book) {

Node* new_node = (Node*)malloc(sizeof(Node));


new_node->book = book;

new_node->next = NULL;

return new_node;

void add_book(Node** head, Book book) {

Node* new_node = create_node(book);

if (*head == NULL) {

*head = new_node;

} else {

Node* current = *head;

while (current->next != NULL) {

current = current->next;

}
current->next = new_node;

void delete_book(Node** head, int book_id) {

Node *current = *head, *prev = NULL;

if (current != NULL && current->book.book_id == book_id) {

*head = current->next;

free(current);

printf("Book with ID %d deleted.\n", book_id);

return;

}
while (current != NULL && current->book.book_id != book_id) {

prev = current;

current = current->next;

if (current == NULL) {

printf("Book with ID %d not found.\n", book_id);

return;

prev->next = current->next;

free(current);

printf("Book with ID %d deleted.\n", book_id);

Node* search_book(Node* head, const char* search_type, const char* value) {


Node* current = head;

Node* result = NULL;

while (current != NULL) {

if (search_type[0] == 'I' && current->book.book_id == atoi(value)) {

result = current;

break;

int i = 0;

if (search_type[0] == 'T') {

while (value[i] != '\0' && current->book.title[i] != '\0') {

if (value[i] != current->book.title[i]) {

break;

i++;
}

if (value[i] == '\0') {

result = current;

break;

else if (search_type[0] == 'A') {

i = 0;

while (value[i] != '\0' && current->book.author[i] != '\0') {

if (value[i] != current->book.author[i]) {

break;

i++;

if (value[i] == '\0') {
result = current;

break;

current = current->next;

return result;

void display_inventory(Node* head) {

printf("\n%-10s%-30s%-30s%-10s%-10s\n", "Book ID", "Title", "Author", "Price",


"Year");

printf("------------------------------------------------------------------------------------\n");
Node* current = head;

while (current != NULL) {

printf("%-10d%-30s%-30s%-10.2f%-10d\n", current->book.book_id,
current->book.title, current->book.author, current->book.price,
current->book.publication_year);

current = current->next;

int compare_title(const void* a, const void* b) {

Node* node_a = *((Node**)a);

Node* node_b = *((Node**)b);

int i = 0;

while (node_a->book.title[i] != '\0' && node_b->book.title[i] != '\0') {

if (node_a->book.title[i] < node_b->book.title[i]) return -1;


if (node_a->book.title[i] > node_b->book.title[i]) return 1;

i++;

if (node_a->book.title[i] == '\0' && node_b->book.title[i] != '\0') return -1;

if (node_a->book.title[i] != '\0' && node_b->book.title[i] == '\0') return 1;

return 0;

int compare_author(const void* a, const void* b) {

Node* node_a = *((Node**)a);

Node* node_b = *((Node**)b);

int i = 0;

while (node_a->book.author[i] != '\0' && node_b->book.author[i] != '\0') {

if (node_a->book.author[i] < node_b->book.author[i]) return -1;

if (node_a->book.author[i] > node_b->book.author[i]) return 1;


i++;

if (node_a->book.author[i] == '\0' && node_b->book.author[i] != '\0') return -1;

if (node_a->book.author[i] != '\0' && node_b->book.author[i] == '\0') return 1;

return 0;

int compare_price(const void* a, const void* b) {

Node* node_a = *((Node**)a);

Node* node_b = *((Node**)b);

return (node_a->book.price > node_b->book.price) - (node_a->book.price <


node_b->book.price);

}
int compare_pub_year(const void* a, const void* b) {

Node* node_a = *((Node**)a);

Node* node_b = *((Node**)b);

return node_b->book.publication_year - node_a->book.publication_year;

void sort_inventory(Node* head, const char* criterion) {

int count = 0;

Node* current = head;

while (current != NULL) {

count++;

current = current->next;

}
if (count < 2) return;

Node* nodes[count];

current = head;

for (int i = 0; i < count; i++) {

nodes[i] = current;

current = current->next;

if (criterion[0] == 'T') {

qsort(nodes, count, sizeof(Node*), compare_title);

} else if (criterion[0] == 'A') {

qsort(nodes, count, sizeof(Node*), compare_author);


} else if (criterion[0] == 'P') {

qsort(nodes, count, sizeof(Node*), compare_price);

} else if (criterion[0] == 'Y') {

qsort(nodes, count, sizeof(Node*), compare_pub_year);

head = nodes[0];

for (int i = 0; i < count - 1; i++) {

nodes[i]->next = nodes[i + 1];

nodes[count - 1]->next = NULL;

printf("Inventory sorted by %s.\n", criterion);

int main() {
Node* head = NULL;

int choice;

while (1) {

printf("\nLibrary Management System\n");

printf("1. Add a Book\n");

printf("2. Delete a Book\n");

printf("3. Search for a Book\n");

printf("4. Sort the Inventory\n");

printf("5. Display the Inventory\n");

printf("6. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

if (choice == 1) {

Book book;
printf("Enter Book ID: ");

scanf("%d", &book.book_id);

printf("Enter Title: ");

getchar();

fgets(book.title, 50, stdin);

int len = 0;

while (book.title[len] != '\0' && book.title[len] != '\n') len++;

book.title[len] = '\0';

printf("Enter Author: ");

fgets(book.author, 50, stdin);

len = 0;

while (book.author[len] != '\0' && book.author[len] != '\n') len++;

book.author[len] = '\0';

printf("Enter Price: ");

scanf("%f", &book.price);
printf("Enter Publication Year: ");

scanf("%d", &book.publication_year);

add_book(&head, book);

printf("Book added successfully!\n");

} else if (choice == 2) {

int book_id;

printf("Enter Book ID to delete: ");

scanf("%d", &book_id);

delete_book(&head, book_id);

} else if (choice == 3) {

char search_type, value[50];

printf("Search by (I - ID, T - Title, A - Author): ");

scanf(" %c", &search_type);

printf("Enter search value: ");

getchar();
fgets(value, 50, stdin);

int len = 0;

while (value[len] != '\0' && value[len] != '\n') len++;

value[len] = '\0';

Node* found = search_book(head, &search_type, value);

if (found) {

printf("Book found: ID: %d, Title: %s, Author: %s, Price: %.2f, Year: %d\n",

found->book.book_id, found->book.title, found->book.author,

found->book.price, found->book.publication_year);

} else {

printf("No book found with the given criteria.\n");

} else if (choice == 4) {

char criterion;

printf("Sort by (T - Title, A - Author, P - Price, Y - Year): ");


scanf(" %c", &criterion);

sort_inventory(head, &criterion);

} else if (choice == 5) {

display_inventory(head);

} else if (choice == 6) {

printf("Exiting the program. Goodbye!\n");

break;

} else {

printf("Invalid choice. Please try again.\n");

return 0;

Discussion
This C program is a library management system that uses a singly linked list to store book
information such as ID, title, author, price, and publication year. It allows users to perform
various operations: adding books, deleting books by ID, searching for books by ID, title, or
author, displaying the entire inventory, and sorting the books by title, author, price, or
publication year. The program uses malloc for memory allocation and free to release memory
when deleting books. Sorting is done using the qsort() function with custom comparison
functions, and the system provides a simple menu interface for users to interact with the
program.

The code runs smotothly and operate as per the requirements.

Photos executing the program

You might also like