0% found this document useful (0 votes)
8 views12 pages

Student and Book Management System

The document is a C++ program that implements a student and book management system using linked lists. It allows users to add, display, and delete students and books, as well as manage borrowed books. The program features a main menu that directs users to either the student or book menu for further actions.

Uploaded by

bereketkinfe205
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)
8 views12 pages

Student and Book Management System

The document is a C++ program that implements a student and book management system using linked lists. It allows users to add, display, and delete students and books, as well as manage borrowed books. The program features a main menu that directs users to either the student or book menu for further actions.

Uploaded by

bereketkinfe205
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

#include <iostream>

#include <string>
using namespace std;

struct Student {
string studId;
string studName;
string bookBorrowed;
string department;
int year;

Student* prev;
Student* next;
};

Student* studHead = NULL;


Student* studTail = NULL;

void addStudentFront() {
Student* s = new Student;

cout << "Enter Student ID (UGR/____): UGR/";


cin >> s->studId;

[Link]();
cout << "Enter Full Name: ";
getline(cin, s->studName);

cout << "Book Borrowed: ";


getline(cin, s->bookBorrowed);

cout << "Department: ";


getline(cin, s->department);

cout << "Year: ";


cin >> s->year;

s->prev = NULL;
s->next = studHead;

if (studHead != NULL)
studHead->prev = s;
else
studTail = s;
studHead = s;

cout << "\nStudent added at front.\n";


}

void addStudentEnd() {
Student* s = new Student;

cout << "Enter Student ID (UGR/____): UGR/";


cin >> s->studId;

[Link]();
cout << "Enter Full Name: ";
getline(cin, s->studName);

cout << "Book Borrowed: ";


getline(cin, s->bookBorrowed);

cout << "Department: ";


getline(cin, s->department);

cout << "Year: ";


cin >> s->year;

s->next = NULL;
s->prev = studTail;

if (studTail != NULL)
studTail->next = s;
else
studHead = s;

studTail = s;

cout << "\nStudent added at end.\n";


}

void addStudentMiddle() {
if (studHead == NULL) {
cout << "List empty. Adding at front.\n";
addStudentFront();
return;
}

int pos;
cout << "Enter position to insert (starting at 1): ";
cin >> pos;

Student* s = new Student;

cout << "Enter Student ID (UGR/____): UGR/";


cin >> s->studId;

[Link]();
cout << "Enter Full Name: ";
getline(cin, s->studName);

cout << "Book Borrowed: ";


getline(cin, s->bookBorrowed);

cout << "Department: ";


getline(cin, s->department);

cout << "Year: ";


cin >> s->year;

Student* temp = studHead;


int count = 1;

while (temp != NULL && count < pos) {


temp = temp->next;
count++;
}

if (temp == NULL) {
addStudentEnd();
return;
}

s->next = temp;
s->prev = temp->prev;

if (temp->prev != NULL)
temp->prev->next = s;
else
studHead = s;

temp->prev = s;

cout << "\nStudent added in middle.\n";


}

void displayStudents() {
if (studHead == NULL) {
cout << "No students in list.\n";
return;
}

Student* temp = studHead;


cout << "\n--- STUDENT LIST ---\n";
while (temp != NULL) {
cout << "ID: UGR/" << temp->studId << endl;
cout << "Name: " << temp->studName << endl;
cout << "Borrowed: " << temp->bookBorrowed << endl;
cout << "Department: " << temp->department << endl;
cout << "Year: " << temp->year << endl;
cout << "------------------\n";
temp = temp->next;
}
}

void displayStudentById() {
string id;
cout << "Enter ID after UGR/: ";
cin >> id;

Student* temp = studHead;


while (temp != NULL) {
if (temp->studId == id) {
cout << "\nSTUDENT FOUND\n";
cout << "ID: UGR/" << temp->studId << endl;
cout << "Name: " << temp->studName << endl;
cout << "Borrowed: " << temp->bookBorrowed << endl;
cout << "Department: " << temp->department << endl;
cout << "Year: " << temp->year << endl;
return;
}
temp = temp->next;
}

cout << "Student not found.\n";


}

void deleteStudentFront() {
if (studHead == NULL) {
cout << "List empty.\n";
return;
}

Student* temp = studHead;


studHead = studHead->next;

if (studHead != NULL)
studHead->prev = NULL;
else
studTail = NULL;

delete temp;
cout << "Student at front deleted.\n";
}

void deleteStudentEnd() {
if (studTail == NULL) {
cout << "List empty.\n";
return;
}

Student* temp = studTail;


studTail = studTail->prev;

if (studTail != NULL)
studTail->next = NULL;
else
studHead = NULL;

delete temp;
cout << "Last student deleted.\n";
}

void deleteStudentById() {
string id;
cout << "Enter student ID after UGR/: ";
cin >> id;

Student* temp = studHead;


while (temp != NULL) {
if (temp->studId == id) {
if (temp->prev != NULL)
temp->prev->next = temp->next;
else
studHead = temp->next;

if (temp->next != NULL)
temp->next->prev = temp->prev;
else
studTail = temp->prev;

delete temp;
cout << "Student deleted.\n";
return;
}
temp = temp->next;
}

cout << "Student not found.\n";


}

struct Book {
string bookId;
string bookName;
string author;
string type;
string department;

Book* next;
};

Book* head = NULL;

void addBookToStore() {
Book* newBook = new Book;

cout << "Book ID: ";


cin >> newBook->bookId;

cout << "Book Name: ";


[Link]();
getline(cin, newBook->bookName);

cout << "Author: ";


getline(cin, newBook->author);

cout << "Type: ";


getline(cin, newBook->type);
cout << "Department: ";
getline(cin, newBook->department);

newBook->next = NULL;

if (head == NULL) {
head = newBook;
newBook->next = head;
cout << "Book added.\n";
return;
}

Book* temp = head;


while (temp->next != head)
temp = temp->next;

temp->next = newBook;
newBook->next = head;

cout << "Book added.\n";


}

void addBookAtMiddle() {
if (head == NULL) {
cout << "List empty. Using addBookToStore().\n";
addBookToStore();
return;
}

string targetId;
cout << "Insert after Book ID: ";
cin >> targetId;

Book* temp = head;


bool found = false;

do {
if (temp->bookId == targetId) {
found = true;
break;
}
temp = temp->next;
} while (temp != head);

if (!found) {
cout << "Book ID not found.\n";
return;
}

Book* newBook = new Book;

cout << "New Book ID: ";


cin >> newBook->bookId;

cout << "Book Name: ";


[Link]();
getline(cin, newBook->bookName);

cout << "Author: ";


getline(cin, newBook->author);

cout << "Type: ";


getline(cin, newBook->type);

cout << "Department: ";


getline(cin, newBook->department);

newBook->next = temp->next;
temp->next = newBook;

cout << "Book added in middle.\n";


}

void displayBooks() {
if (head == NULL) {
cout << "No books.\n";
return;
}

Book* temp = head;

cout << "\n--- BOOK LIST ---\n";

do {
cout << "Book ID: " << temp->bookId << endl;
cout << "Name: " << temp->bookName << endl;
cout << "Author: " << temp->author << endl;
cout << "Type: " << temp->type << endl;
cout << "Department: " << temp->department << endl;
cout << "------------------------\n";
temp = temp->next;
} while (temp != head);
}

void deleteBookById() {
if (head == NULL) {
cout << "No books in the store.\n";
return;
}

string target;
cout << "Enter Book ID to delete: ";
cin >> target;

Book* temp = head;


Book* prev = NULL;

if (temp->bookId == target) {

if (temp->next == head) {
delete head;
head = NULL;
cout << "Book deleted.\n";
return;
}

Book* last = head;


while (last->next != head)
last = last->next;

last->next = head->next;
head = head->next;

delete temp;
cout << "Book deleted.\n";
return;
}

do {
prev = temp;
temp = temp->next;

if (temp->bookId == target) {
prev->next = temp->next;
delete temp;
cout << "Book deleted.\n";
return;
}

} while (temp != head);

cout << "Book ID not found.\n";


}

void showBorrowed() {
if (studHead == NULL) {
cout << "No students yet.\n";
return;
}

Student* temp = studHead;

cout << "\n--- BORROWED BOOKS ---\n";

while (temp != NULL) {


cout << temp->studName << " → " << temp->bookBorrowed <<
endl;
temp = temp->next;
}
}

void studentMenu() {
int choice;
do {
cout << "\n--- STUDENT MENU ---\n";
cout << "1. Add Student Front\n";
cout << "2. Add Student Middle\n";
cout << "3. Add Student End\n";
cout << "4. Display Students\n";
cout << "5. Display by ID\n";
cout << "6. Delete Front\n";
cout << "7. Delete by ID\n";
cout << "8. Delete End\n";
cout << "9. Back\n";
cout << "Choice: ";
cin >> choice;

switch (choice) {
case 1: addStudentFront();
break;
case 2: addStudentMiddle();
break;
case 3: addStudentEnd();
break;
case 4: displayStudents();
break;
case 5: displayStudentById();
break;
case 6: deleteStudentFront();
break;
case 7: deleteStudentById();
break;
case 8: deleteStudentEnd();
break;
}
} while (choice != 9);
}

void bookMenu() {
int choice;
do {
cout << "\n--- BOOK MENU ---\n";
cout << "1. Add Book to Store (End)\n";
cout << "2. Add Book at Middle\n";
cout << "3. Display Books\n";
​ cout << “4. Delete Book By ID\n”;
cout << "5. Back\n";
cout << "Choice: ";
cin >> choice;

switch (choice) {
case 1: addBookToStore();
break;
case 2: addBookAtMiddle();
break;
case 3: displayBooks();
break;
case 4: deleteBookById();
​ break;
}
} while (choice != 5);
}

int main() {
int choice;
do {
cout << "\n===== MAIN MENU =====\n";
cout << "1. Student Menu\n";
cout << "2. Book Menu\n";
cout << "3. Book Borrowed\n";
cout << "4. Exit\n";
cout << "Choice: ";
cin >> choice;

switch (choice) {
case 1: studentMenu();
break;
case 2: bookMenu();
break;
case 3: showBorrowed();
break;
}

} while (choice != 4);

cout << "\nSystem Closed.\n";


return 0;
}

Common questions

Powered by AI

In book management, a circular linked list is employed, where the `next` pointer of the last node points back to the `head`. This setup is used in functions like `addBookToStore` and `displayBooks`, facilitating easy iteration and insertions. The circular nature allows easy insertion at the end by simply connecting the new last node to `head`. Conversely, student management utilizes a doubly linked list, with separate `prev` and `next` pointers, allowing traversal in both directions and easier middle node insertion or deletion. The choice of data structure is influenced by operational needs; circular lists simplify simple cyclical operations, while doubly linked lists enhance complex insertions and deletions .

To implement a 'return book' feature, both the student and book management systems need to be updated. Structurally, a `bookBorrowDate` field could be added to track borrow durations. In the student record, the `bookBorrowed` field should handle multiple or zero values to support book returns. A function to check a student's borrowings, update their records on book return, and potentially move returned books back to the available list would be needed. Additionally, this function would adjust the book's availability status in the book linked list, potentially indicating a reversal of the `addBookToStore` process upon return .

The `deleteStudentById` function traverses the student list to locate the node with the specified ID. If found, it updates the `prev` and `next` pointers of the adjacent nodes to bypass the node being deleted. If the node to be deleted is at the head, `studHead` is moved to the next node; if it's at the tail, `studTail` is moved to the previous node. If the student is not found after full traversal, it prints "Student not found." .

The `studentMenu` functionality is preferred when a user-friendly interface is required to manage student data. It abstracts the complex function calls into a menu-driven system that sequentially guides users through options like adding, displaying, or deleting students with sequential input prompts. This design improves usability, reduces the likelihood of user errors associated with incorrect function usage, and allows for a more structured interaction with the data .

Student IDs are prefixed with 'UGR/', ensuring a standardized identifier format. This format aids in input validation during insertion and search operations, bolstering the integrity of records. Effective searching is realized through functions like `displayStudentById` and `deleteStudentById`, which traverse the linked list comparing user-inputted IDs with stored IDs to find the correct node. This search technique supports uniqueness by providing error feedback if duplicate IDs exist, thereby enforcing consistently unique entries across operations .

The `addBookAtMiddle` function uses target book identification to determine the insertion point. It takes a target Book ID as input and traverses the circular linked list starting from the head to find the node that matches this ID. Once located, a new book node is inserted right after this node by adjusting the `next` pointers. If the target Book ID is not found, the function prints an error message indicating that the Book ID was not found .

The `addStudentMiddle` function allows inserting a student at a specific position in a doubly linked list. It first checks if the list is empty; if it is, a student is added at the front using `addStudentFront()`. Otherwise, it traverses the list node by node until it reaches the specified position (starting from 1). If the position is beyond the current length of the list, the student is added at the end using `addStudentEnd()`. For insertion in the middle, the function updates the `prev` and `next` pointers of the adjacent nodes to include the new student node between them .

The `showBorrowed` function traverses the student linked list starting from `studHead`. For each student node, it retrieves and outputs the student's name and the book they borrowed in a simple format (e.g., `Student Name → Book Name`). This listing provides a summary of all borrowed books in association with the borrowers, displaying information until the end of the list is reached .

The `deleteBookById` function locates the book to be deleted by its Book ID. If the book is the only node, it deletes the head node and sets the head to NULL. If the target book is the head and there are multiple nodes, the last book's `next` pointer and `head` are updated to skip the current head. For other cases, it updates the previous node's `next` pointer to bypass the node being deleted. If the Book ID is not found, it informs the user that the ID was not found .

When displaying the list of students using `displayStudents`, the function must first check if the list is empty and inform the user if there are no students. Then, it iterates from `studHead` to `studTail`, printing the details of each student (ID, Name, Book Borrowed, Department, Year) followed by a separator. This ensures a comprehensive output of the list's contents .

You might also like