0% found this document useful (0 votes)
69 views

LAB 2 Practice Tasks

The document describes a C++ program to implement a linked list to store books with various functions like insertion, deletion, displaying the list etc. It defines a Book struct with title, author, price, id etc and a BookList class with functions to insert books sequentially based on id, delete books sequentially or at a specific index, display the list and get the length. It provides sample code in main() to insert some books, display the list, remove a book, insert at an index and remove at an index.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views

LAB 2 Practice Tasks

The document describes a C++ program to implement a linked list to store books with various functions like insertion, deletion, displaying the list etc. It defines a Book struct with title, author, price, id etc and a BookList class with functions to insert books sequentially based on id, delete books sequentially or at a specific index, display the list and get the length. It provides sample code in main() to insert some books, display the list, remove a book, insert at an index and remove at an index.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Create a list of at least 10 books (Title, Author, Price, Book_id) with the following

functions:
1) Sequential insert in the list on the basis of Book_id
2) Sequential delete in the list and then adjust the list afterwards
3) Insert a value at a specific index and adjust remaining list afterwards
4) Delete a value at a specific index and adjust remaining list afterwards
5) Show all values present in the list
6) Show the length of the list

#include <iostream>
#include <string>

using namespace std;

struct Book {
string title;
string author;
float price;
int book_id;
Book* next;
};

class BookList {
private:
Book* head;
public:
BookList() {
head = nullptr;
}

void insert(Book* new_book) {


if (head == nullptr || head->book_id > new_book->book_id) {
new_book->next = head;
head = new_book;
} else {
Book* current = head;
while (current->next != nullptr && current->next->book_id < new_book->book_id) {
current = current->next;
}
new_book->next = current->next;
current->next = new_book;
}
}

void remove(int book_id) {


Book* current = head;
Book* prev = nullptr;

while (current != nullptr && current->book_id != book_id) {


prev = current;
current = current->next;
}

if (current == nullptr) {
cout << "Book not found." << endl;
return;
}

if (prev == nullptr) {
head = current->next;
} else {
prev->next = current->next;
}

delete current;
}

void insertAtIndex(Book* new_book, int index) {


if (index == 0 || head == nullptr) {
new_book->next = head;
head = new_book;
} else {
Book* current = head;
for (int i = 0; i < index - 1 && current->next != nullptr; ++i) {
current = current->next;
}
new_book->next = current->next;
current->next = new_book;
}
}
void removeAtIndex(int index) {
if (head == nullptr) {
cout << "List is empty." << endl;
return;
}

if (index == 0) {
Book* temp = head;
head = head->next;
delete temp;
} else {
Book* current = head;
for (int i = 0; i < index - 1 && current->next != nullptr; ++i) {
current = current->next;
}
if (current->next == nullptr) {
cout << "Index out of range." << endl;
return;
}
Book* temp = current->next;
current->next = current->next->next;
delete temp;
}
}

void display() {
Book* current = head;
while (current != nullptr) {
cout << "Book ID: " << current->book_id << ", Title: " << current->title << ", Author: "
<< current->author << ", Price: " << current->price << endl;
current = current->next;
}
}

int length() {
int count = 0;
Book* current = head;
while (current != nullptr) {
count++;
current = current->next;
}
return count;
}
};

int main() {
BookList books;

// Sample books insertion


Book book1 = {"Book1", "Author1", 10.5, 101, nullptr};
Book book2 = {"Book2", "Author2", 20.75, 102, nullptr};
Book book3 = {"Book3", "Author3", 15.2, 103, nullptr};
Book book4 = {"Book4", "Author4", 30.0, 104, nullptr};
books.insert(&book1);
books.insert(&book2);
books.insert(&book3);
books.insert(&book4);

cout << "Length of list: " << books.length() << endl;


cout << "Books in the list:" << endl;
books.display();

cout << "\nRemoving book with ID 102..." << endl;


books.remove(102);
cout << "Length of list after removal: " << books.length() << endl;
cout << "Books in the list after removal:" << endl;
books.display();

cout << "\nInserting book at index 1..." << endl;


Book new_book = {"NewBook", "NewAuthor", 25.5, 105, nullptr};
books.insertAtIndex(&new_book, 1);
cout << "Length of list after insertion: " << books.length() << endl;
cout << "Books in the list after insertion:" << endl;
books.display();

cout << "\nRemoving book at index 2..." << endl;


books.removeAtIndex(2);
cout << "Length of list after removal: " << books.length() << endl;
cout << "Books in the list after removal:" << endl;
books.display();
return 0;
}
7.3 Practice Task 3 [Expected time = 25mins]
Create a list of Employees (List can be as long as user wants). Program should save the
following information for each Employee: Name, Emp. No., Experience, Designation and
Salary. Now perform the following tasks:
a) Your program should save the information of only those employees whose experience is
at least 2 years.
b) Display only those Employees whose Salary is greater than 50,000
c) Display the Employees alphabetically

#include <iostream>
#include <string>

using namespace std;

struct Employee {
string name;
int empNo;
float experience;
string designation;
float salary;
};

void addEmployee(Employee employees[], int& numEmployees) {


cout << "Enter employee name: ";
cin >> employees[numEmployees].name;
cout << "Enter employee number: ";
cin >> employees[numEmployees].empNo;
cout << "Enter employee experience (in years): ";
cin >> employees[numEmployees].experience;
cout << "Enter employee designation: ";
cin >> employees[numEmployees].designation;
cout << "Enter employee salary: ";
cin >> employees[numEmployees].salary;

numEmployees++;
}

void displayEmployees(Employee employees[], int numEmployees) {


cout << "Employees with salary greater than 50,000:\n";
for (int i = 0; i < numEmployees; ++i) {
if (employees[i].salary > 50000) {
cout << "Name: " << employees[i].name << ", Emp. No.: " << employees[i].empNo <<
", Experience: " << employees[i].experience << " years, Designation: " <<
employees[i].designation << ", Salary: " << employees[i].salary << endl;
}
}
}

void sortEmployees(Employee employees[], int numEmployees) {


// Bubble sort algorithm for simplicity
for (int i = 0; i < numEmployees - 1; ++i) {
for (int j = 0; j < numEmployees - i - 1; ++j) {
if (employees[j].name > employees[j + 1].name) {
// Swap
Employee temp = employees[j];
employees[j] = employees[j + 1];
employees[j + 1] = temp;
}
}
}

cout << "\nEmployees sorted alphabetically:\n";


for (int i = 0; i < numEmployees; ++i) {
cout << "Name: " << employees[i].name << ", Emp. No.: " << employees[i].empNo << ",
Experience: " << employees[i].experience << " years, Designation: " <<
employees[i].designation << ", Salary: " << employees[i].salary << endl;
}
}

int main() {
const int MAX_EMPLOYEES = 100;
Employee employees[MAX_EMPLOYEES];
int numEmployees = 0;
char choice;

do {
addEmployee(employees, numEmployees);
cout << "Do you want to add another employee? (y/n): ";
cin >> choice;
} while (choice == 'y' || choice == 'Y');
cout << endl;
displayEmployees(employees, numEmployees);

sortEmployees(employees, numEmployees);

return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy