Assignment No.
01
Total Marks: 20
SEMESTER Fall 2024
Due Date: 09-Nov, 2024
CS301P- Data Structures (Practical)
Instructions
Please read the following instructions carefully before solving & submitting assignment:
It should be clear that your assignment will not get any credit (zero marks) if:
o The assignment is submitted after the due date.
o The submitted code does NOT compile.
o The submitted assignment is other than .CPP file.
o The submitted assignment does NOT open or file is corrupted.
o The assignment is copied (from other student or ditto copy from handouts or internet).
Uploading instructions
For clarity and simplicity, you are required to Upload/Submit only ONE .cpp template file after completing its
code.
Note:
1. Use ONLY Dev-C++ IDE.
2. Only add the code were commented (// Write your code here) in given .cpp template file. Don’t change
any other code otherwise you will lose your marks.
3. Only implement these functions addPart(), deletePart(), updateQuantity(), findPart().
Objective
The objective of this assignment is
o To make you familiar of Programming with Linked List Data Structure.
o Develop and implement essential operations on linked lists using class-based objects to manage an
inventory of computer parts effectively.
o Enhance your skills in object-oriented programming and gain practical experience with dynamic data
structures.
For any query about the assignment, contact at [email protected]
GOOD LUCK
Marks: 20
You are tasked with developing the "Computer Parts Management System," designed to manage the inventory of
a computer parts store using Linked List data structure. The system should store details about each computer part,
including its ID, name, and quantity in stock. It must support adding new parts, removing parts, updating the
quantity of parts, and finding parts in the inventory.
Tasks:
1. Understand the provided code:
Read and understand the given .cpp file which is uploaded on VULMS with your assignment file. This
file outlining the class structure for managing the computer parts inventory.
2. Implement the addPart() function:
Complete the method to add a new part to the inventory linked list.
3. Implement the deletePart() function:
Complete the method to remove a part from the inventory based on the part ID.
4. Implement the updateQuantity() function:
Complete the method to update the stock quantity for an existing part.
5. Implement the findPart() function:
Complete the method to find and display a part by its part ID.
6. Mention your own student id instead of BCxxxxxxxxx as given in the output screenshot.
Note:
1. Especially handle head, current and previous pointers properly in all four functions using liked list.
2. After completing your code your output will be like the given output screenshot.
Sample Output Screenshot:
Lectures Covered: This assignment covers Lab # 1. (First three lectures of CS301).
Deadline: Your assignment must be uploaded/submitted on or before, Nov 09, 2024.
Solution
#include <iostream>
#include <string>
using namespace std;
// Part class to represent each part in the inventory
class Part {
public:
int id;
string name;
int quantity;
Part* next;
Part(int partId, string partName, int partQuantity) {
id = partId;
name = partName;
quantity = partQuantity;
next = nullptr;
}
};
// Inventory class to manage the linked list of parts
class Inventory {
private:
Part* head;
public:
Inventory() : head(nullptr) {}
// Function to add a new part to the inventory
void addPart(int partId, string partName, int partQuantity) {
Part* newPart = new Part(partId, partName, partQuantity);
newPart->next = head;
head = newPart;
cout << "Part added: ID " << partId << ", Name " << partName << ", Quantity " <<
partQuantity << endl;
}
// Function to delete a part from the inventory by part ID
void deletePart(int partId) {
Part* current = head;
Part* previous = nullptr;
while (current != nullptr && current->id != partId) {
previous = current;
current = current->next;
}
if (current == nullptr) {
cout << "Part ID " << partId << " not found." << endl;
return;
}
if (previous == nullptr) { // Deleting the head node
head = current->next;
} else { // Deleting a node other than head
previous->next = current->next;
}
delete current;
cout << "Part ID " << partId << " deleted." << endl;
}
// Function to update the quantity of an existing part by part ID
void updateQuantity(int partId, int newQuantity) {
Part* current = head;
while (current != nullptr) {
if (current->id == partId) {
current->quantity = newQuantity;
cout << "Updated quantity of Part ID " << partId << " to " << newQuantity << endl;
return;
}
current = current->next;
}
cout << "Part ID " << partId << " not found." << endl;
}
// Function to find and display a part by part ID
void findPart(int partId) {
Part* current = head;
while (current != nullptr) {
if (current->id == partId) {
cout << "Found Part: ID " << current->id << ", Name " << current->name << ", Quantity
" << current->quantity << endl;
return;
}
current = current->next;
}
cout << "Part ID " << partId << " not found." << endl;
}
// Function to display all parts in the inventory
void displayInventory() {
Part* current = head;
cout << "--------------------------------------------" << endl;
cout << "Part ID\tPart Name\tQuantity" << endl;
cout << "--------------------------------------------" << endl;
while (current != nullptr) {
cout << current->id << "\t" << current->name << "\t\t" << current->quantity << endl;
current = current->next;
}
cout << "--------------------------------------------" << endl;
}
};
int main() {
Inventory inventory;
// Adding parts to inventory
inventory.addPart(101, "SSD", 25);
inventory.addPart(102, "RAM", 40);
inventory.addPart(103, "HDD", 15);
cout << "\nInventory Added." << endl;
inventory.displayInventory();
// Finding a part by ID
cout << "\nFinding Part ID 101:" << endl;
inventory.findPart(101);
// Deleting a part and updating quantity
cout << "\nAfter deleting SSD and updating RAM Quantity..." << endl;
inventory.deletePart(101);
inventory.updateQuantity(102, 50);
// Displaying updated inventory
inventory.displayInventory();
return 0;
}