0% found this document useful (0 votes)
3 views11 pages

Single Linked List Example 2

The document contains a C++ program that implements a linked list to manage a collection of books, allowing operations such as inserting, deleting, searching, and displaying books. It defines a structure for book information and provides functions for manipulating the linked list based on user input. The main function presents a menu for users to perform various book operations.

Uploaded by

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

Single Linked List Example 2

The document contains a C++ program that implements a linked list to manage a collection of books, allowing operations such as inserting, deleting, searching, and displaying books. It defines a structure for book information and provides functions for manipulating the linked list based on user input. The main function presents a menu for users to perform various book operations.

Uploaded by

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

#include <iostream>

#include<string>

using namespace std;

struct BookInfo{

string title;

string author;

int ISBN;

string publisher;

float price;

int numberOfPages;

bool borrowStatus;

BookInfo* next;

};

BookInfo* head = nullptr;

void insertAtbeginning(BookInfo book) {

BookInfo* newBook = new BookInfo();

*newBook = book;

newBook->next = head;

head = newBook;

cout<<"Book Inserted at beginning."<<endl;

void insertAtMiddle(BookInfo book, int position) {

if(position < 1) {

cout<<"Invalid position. Try again."<<endl;

return;
}

BookInfo* newBook = new BookInfo();

*newBook = book;

if(position == 1) {

newBook->next = head;

head = newBook;

BookInfo* current = head;

for (int i = 1; i < position - 1 && current!=nullptr; i++) {

current = current->next;

if(current == nullptr) {

cout<<"Invalid position";

newBook->next = current->next;

current->next = newBook;

cout<<"Book inserted at "<< position<< endl;

void insertAtEnd(BookInfo book) {

BookInfo* newBook = new BookInfo();

*newBook = book;

newBook->next = nullptr;

if(head == nullptr) {

head = newBook;

}
BookInfo* current = head;

while(current->next!= nullptr) {

current = current->next;

current->next = newBook;

cout<<"Book inserted at the end."<<endl;

void deleteAtBeginning() {

if(head == nullptr) {

cout<<"Empty List. nothing to delete;"<<endl;

return;

BookInfo* temp = head;

head = head->next;

delete temp;

cout<<"Book deleted at the beginning."<<endl;

void deleteAtMiddle(int position) {

if(position < 1) {

cout<<"Invalid position. Try again."<<endl;

return;

if(head == nullptr) {

cout<<"Empty list. nothing to delete."<<endl;

return;
}

if(position == 1) {

deleteAtBeginning();

BookInfo* current = head;

int i = 1;

while (i < position - 1 && current->next!= nullptr) {

current = current->next;

i++;

if (current == nullptr || current->next == nullptr) {

cout<<"Invalid Position.";

BookInfo* temp = current->next;

current->next = current->next->next;

delete temp;

cout<<"Book deleted at "<< position<<endl;

void deleteAtEnd() {

if(head == nullptr) {

cout<<"Empty list. nothing to delete."<<endl;

return;

BookInfo* current = head;

while(current->next->next!= nullptr) {
current = current->next;

delete current->next;

current->next= nullptr;

void searchBookByISBN(int isbn) {

if(head == nullptr){

cout<<"Empty List"<<endl;

return;

BookInfo* current = head;

bool found = false;

while(current!=nullptr) {

if(current->ISBN == isbn) {

cout<<"Title: "<< current->title<<endl;

cout<<"Author: "<< current->author<<endl;

cout<<"ISBN: "<< current->ISBN<<endl;

cout<<"Publisher: "<< current->publisher<<endl;

cout<<"Price: "<< current->price<<endl;

cout<<"Number of Pages: "<< current->numberOfPages<<endl;

cout<<"Borrow Status: "<< current->borrowStatus<<endl;

found = true;

break;

}
current = current->next;

if(!found) {

cout<<"the book does not exit in the list."<<endl;

return;

void displayAllBooks() {

if(head == nullptr){

cout<<"Empty List"<<endl;

return;

BookInfo* current = head;

while(current!=nullptr) {

cout<<"Book Detail Information:"<<endl;

cout<<"Title: "<< current->title<<endl;

cout<<"Author: "<< current->author<<endl;

cout<<"ISBN: "<< current->ISBN<<endl;

cout<<"Publisher: "<< current->publisher<<endl;

cout<<"Price: "<< current->price<<endl;

cout<<"Number of Pages: "<< current->numberOfPages<<endl;

cout<<"Borrow Status: "<< current->borrowStatus<<endl;

current = current->next;

}
int main()

int chioce, position, isbn;

BookInfo book;

while(true) {

cout<<"Book Operations:"<<endl;

cout<<": -----------------------------------: "<<endl;

cout<<"1. Insert Book at the beginning: "<<endl;

cout<<"2. Insert Book at the middle: "<<endl;

cout<<"3. Insert Book at the end: "<<endl;

cout<<"4. Delete Book at the beginning: "<<endl;

cout<<"5. Delete Book at the middle: "<<endl;

cout<<"6. Delete Book at the end: "<<endl;

cout<<"7. Search Book By ISBN: "<<endl;

cout<<"8. Display All Books: "<<endl;

cout<<"9. Exit or Close: "<<endl;

cout<<": -----------------------------------: "<<endl;

cout<<"Enter your choice: ";

cin>>chioce;

switch(chioce) {

case 1:

cout<<"Enter Book Details: "<<endl;

cout<<"Title: ";

cin>>book.title;

cout<<"Author: ";
cin>>book.author;

cout<<"ISBN: ";

cin>>book.ISBN;

cout<<"Publisher: ";

cin>>book.publisher;

cout<<"Price: ";

cin>>book.price;

cout<<"Number of Pages: ";

cin>>book.numberOfPages;

cout<<"Borrow Status: ";

cin>>book.borrowStatus;

insertAtbeginning(book);

break;

case 2:

cout<<"Enter Book Details: "<<endl;

cout<<"Title: ";

cin>>book.title;

cout<<"Author: ";

cin>>book.author;

cout<<"ISBN: ";

cin>>book.ISBN;

cout<<"Publisher: ";

cin>>book.publisher;

cout<<"Price: ";

cin>>book.price;
cout<<"Number of Pages: ";

cin>>book.numberOfPages;

cout<<"Borrow Status: ";

cin>>book.borrowStatus;

cout<<"Enter Position: ";

cin>>position;

insertAtMiddle(book, position);

break;

case 3:

cout<<"Enter Book Details: "<<endl;

cout<<"Title: ";

cin>>book.title;

cout<<"Author: ";

cin>>book.author;

cout<<"ISBN: ";

cin>>book.ISBN;

cout<<"Publisher: ";

cin>>book.publisher;

cout<<"Price: ";

cin>>book.price;

cout<<"Number of Pages: ";

cin>>book.numberOfPages;

cout<<"Borrow Status: ";

cin>>book.borrowStatus;

insertAtEnd(book);
break;

case 4:

deleteAtBeginning();

break;

case 5:

cout<<"Enter Position: ";

cin>>position;

deleteAtMiddle(position);

break;

case 6:

deleteAtEnd();

break;

case 7:

cout<<"Enter ISBN :";

cin>>isbn;

searchBookByISBN(isbn);

break;

case 8:

displayAllBooks();

break;

case 9:

return 0;

return 0;
}

You might also like