Single Linked List Example 2
Single Linked List Example 2
#include<string>
struct BookInfo{
string title;
string author;
int ISBN;
string publisher;
float price;
int numberOfPages;
bool borrowStatus;
BookInfo* next;
};
*newBook = book;
newBook->next = head;
head = newBook;
if(position < 1) {
return;
}
*newBook = book;
if(position == 1) {
newBook->next = head;
head = newBook;
current = current->next;
if(current == nullptr) {
cout<<"Invalid position";
newBook->next = current->next;
current->next = newBook;
*newBook = book;
newBook->next = nullptr;
if(head == nullptr) {
head = newBook;
}
BookInfo* current = head;
while(current->next!= nullptr) {
current = current->next;
current->next = newBook;
void deleteAtBeginning() {
if(head == nullptr) {
return;
head = head->next;
delete temp;
if(position < 1) {
return;
if(head == nullptr) {
return;
}
if(position == 1) {
deleteAtBeginning();
int i = 1;
current = current->next;
i++;
cout<<"Invalid Position.";
current->next = current->next->next;
delete temp;
void deleteAtEnd() {
if(head == nullptr) {
return;
while(current->next->next!= nullptr) {
current = current->next;
delete current->next;
current->next= nullptr;
if(head == nullptr){
cout<<"Empty List"<<endl;
return;
while(current!=nullptr) {
if(current->ISBN == isbn) {
found = true;
break;
}
current = current->next;
if(!found) {
return;
void displayAllBooks() {
if(head == nullptr){
cout<<"Empty List"<<endl;
return;
while(current!=nullptr) {
current = current->next;
}
int main()
BookInfo book;
while(true) {
cout<<"Book Operations:"<<endl;
cin>>chioce;
switch(chioce) {
case 1:
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;
cin>>book.numberOfPages;
cin>>book.borrowStatus;
insertAtbeginning(book);
break;
case 2:
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;
cin>>book.borrowStatus;
cin>>position;
insertAtMiddle(book, position);
break;
case 3:
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;
cin>>book.numberOfPages;
cin>>book.borrowStatus;
insertAtEnd(book);
break;
case 4:
deleteAtBeginning();
break;
case 5:
cin>>position;
deleteAtMiddle(position);
break;
case 6:
deleteAtEnd();
break;
case 7:
cin>>isbn;
searchBookByISBN(isbn);
break;
case 8:
displayAllBooks();
break;
case 9:
return 0;
return 0;
}