Assignment
Assignment
Lab 4
Name: Hamza Iqbal
Section : V18
Scenario: A linked list of student records, where each node represents a student with their
unique attributes.
Operations:
· - Add Node at Rear: Add a new student to the end of the list.
· - Add Node at Front: Add a new student at the start of the list.
· - Delete from Front/Rear: Remove the first or last student from the list.
· - Delete All Nodes: Clear all student records from the list.
CODE:
#include <iostream>
#include <string>
using namespace std;
class StudentNode {
public:
int studentID;
string name;
int age;
float GPA;
StudentNode* next;
class StudentList {
private:
StudentNode* head;
public:
StudentList() {
head = nullptr;
}
void addStudentAtPosition(int id, string name, int age, float gpa, int position) {
if (position <= 0) {
addStudentAtFront(id, name, age, gpa);
return;
}
if (temp == nullptr) {
addStudentAtRear(id, name, age, gpa);
} else {
newStudent->next = temp->next;
temp->next = newStudent;
}
}
void deleteFromFront() {
if (head == nullptr) return;
StudentNode* temp = head;
head = head->next;
delete temp;
}
void deleteFromRear() {
if (head == nullptr) return;
if (head->next == nullptr) {
delete head;
head = nullptr;
return;
}
StudentNode* temp = head;
while (temp->next->next != nullptr) {
temp = temp->next;
}
delete temp->next;
temp->next = nullptr;
}
void deleteAllNodes() {
while (head != nullptr) {
deleteFromFront();
}
}
void displayStudents() {
StudentNode* temp = head;
while (temp != nullptr) {
cout << "StudentID: " << temp->studentID
<< ", Name: " << temp->name
<< ", Age: " << temp->age
<< ", GPA: " << temp->GPA << endl;
temp = temp->next;
}
}
~StudentList() {
deleteAllNodes();
}
};
int main() {
StudentList studentList;
return 0;
}
OUTPUT :
Scenario: A linked list representing an inventory system, where each node is an item in stock.
Operations:
· - Add Node at Rear: Add a new item at the end of the list.
· - Add Node at Specific Position: Insert an item at a specific position in the inventory list.
· - Delete from Front/Rear: Remove the first or last item in the list.
CODE:
#include <iostream>
#include <string>
using namespace std;
class ItemNode {
public:
int itemID;
string itemName;
int quantity;
float price;
ItemNode* next;
class InventoryList {
private:
ItemNode* head;
public:
InventoryList() {
head = nullptr;
}
void addItemAtPosition(int id, string name, int qty, float pr, int position) {
if (position <= 0) {
addItemAtFront(id, name, qty, pr);
return;
}
void deleteFromFront() {
if (head == nullptr) return;
ItemNode* temp = head;
head = head->next;
delete temp;
}
void deleteFromRear() {
if (head == nullptr) return;
if (head->next == nullptr) {
delete head;
head = nullptr;
return;
}
ItemNode* temp = head;
while (temp->next->next != nullptr) {
temp = temp->next;
}
delete temp->next;
temp->next = nullptr;
}
void deleteAllNodes() {
while (head != nullptr) {
deleteFromFront();
}
}
void displayItems() {
ItemNode* temp = head;
while (temp != nullptr) {
cout << "ItemID: " << temp->itemID
<< ", Name: " << temp->itemName
<< ", Quantity: " << temp->quantity
<< ", Price: " << temp->price << endl;
temp = temp->next;
}
}
~InventoryList() {
deleteAllNodes();
}
};
int main() {
InventoryList inventory;
return 0;
}
OUTPUT :
Operations:
· - Add Node at Rear: Add a new book at the end of the list.
· - Add Node at Front: Add a new book at the start of the list.
· - Add Node at Specific Position: Insert a book at a specified position in the list.
· - Delete from Front/Rear: Remove the first or last book from the list.
· - Delete All Nodes: Clear all book records from the list.
· - Display: Show all book records in the list.
CODE:
#include <iostream>
#include <string>
using namespace std;
class BookNode {
public:
int bookID;
string title;
string author;
int yearPublished;
BookNode* next;
class Library {
private:
BookNode* head;
public:
Library() {
head = nullptr;
}
void addBookAtPosition(int id, string title, string author, int year, int position) {
if (position <= 0) {
addBookAtFront(id, title, author, year);
return;
}
if (temp == nullptr) {
addBookAtRear(id, title, author, year);
} else {
newBook->next = temp->next;
temp->next = newBook;
}
}
void deleteFromFront() {
if (head == nullptr) return;
BookNode* temp = head;
head = head->next;
delete temp;
}
void deleteFromRear() {
if (head == nullptr) return;
if (head->next == nullptr) {
delete head;
head = nullptr;
return;
}
BookNode* temp = head;
while (temp->next->next != nullptr) {
temp = temp->next;
}
delete temp->next;
temp->next = nullptr;
}
void deleteAllNodes() {
while (head != nullptr) {
deleteFromFront();
}
}
void displayBooks() {
BookNode* temp = head;
while (temp != nullptr) {
cout << "BookID: " << temp->bookID
<< ", Title: " << temp->title
<< ", Author: " << temp->author
<< ", Year Published: " << temp->yearPublished << endl;
temp = temp->next;
}
}
~Library() {
deleteAllNodes();
}
};
int main() {
Library library;
return 0;
OUTPUT :
4. Employee Directory Linked List
Node Attributes: int employeeID, string name, string department, float salary
Operations:
· - Add Node at Front: Add a new employee at the beginning of the directory.
· - Delete from Front/Rear: Remove the first or last employee in the list.
· - Delete All Nodes: Clear all employee records from the list.
CODE:
#include <iostream>
#include <string>
using namespace std;
class EmployeeNode {
public:
int employeeID;
string name;
string department;
float salary;
EmployeeNode* next;
class EmployeeDirectory {
private:
EmployeeNode* head;
public:
EmployeeDirectory() {
head = nullptr;
}
void addEmployeeAtPosition(int id, string name, string department, float salary, int position) {
if (position <= 0) {
addEmployeeAtFront(id, name, department, salary);
return;
}
void deleteFromFront() {
if (head == nullptr) return;
EmployeeNode* temp = head;
head = head->next;
delete temp;
}
void deleteFromRear() {
if (head == nullptr) return;
if (head->next == nullptr) {
delete head;
head = nullptr;
return;
}
EmployeeNode* temp = head;
while (temp->next->next != nullptr) {
temp = temp->next;
}
delete temp->next;
temp->next = nullptr;
}
void deleteAllNodes() {
while (head != nullptr) {
deleteFromFront();
}
}
void displayEmployees() {
EmployeeNode* temp = head;
while (temp != nullptr) {
cout << "EmployeeID: " << temp->employeeID
<< ", Name: " << temp->name
<< ", Department: " << temp->department
<< ", Salary: " << temp->salary << endl;
temp = temp->next;
}
}
~EmployeeDirectory() {
deleteAllNodes();
}
};
int main() {
EmployeeDirectory directory;
return 0;
}
OUTPUT :
5. Order Tracking Linked List
Node Attributes: int orderID, string customerName, string orderDate, float orderAmount
Operations:
· - Add Node at Rear: Add a new order to the end of the list.
· - Add Node at Specific Position: Insert an order at a specific position in the list.
· - Delete from Front/Rear: Remove the first or last order from the list.
CODE:
#include <iostream>
#include <string>
using namespace std;
class OrderNode {
public:
int orderID;
string customerName;
string orderDate;
float orderAmount;
OrderNode* next;
class OrderTracking {
private:
OrderNode* head;
public:
OrderTracking() {
head = nullptr;
}
void addOrderAtPosition(int id, string name, string date, float amount, int position) {
if (position <= 0) {
addOrderAtFront(id, name, date, amount);
return;
}
if (temp == nullptr) {
addOrderAtRear(id, name, date, amount);
} else {
newOrder->next = temp->next;
temp->next = newOrder;
}
}
void deleteFromFront() {
if (head == nullptr) return;
OrderNode* temp = head;
head = head->next;
delete temp;
}
void deleteFromRear() {
if (head == nullptr) return;
if (head->next == nullptr) {
delete head;
head = nullptr;
return;
}
OrderNode* temp = head;
while (temp->next->next != nullptr) {
temp = temp->next;
}
delete temp->next;
temp->next = nullptr;
}
void deleteAllNodes() {
while (head != nullptr) {
deleteFromFront();
}
}
void displayOrders() {
OrderNode* temp = head;
while (temp != nullptr) {
cout << "OrderID: " << temp->orderID
<< ", Customer Name: " << temp->customerName
<< ", Order Date: " << temp->orderDate
<< ", Order Amount: " << temp->orderAmount << endl;
temp = temp->next;
}
}
~OrderTracking() {
deleteAllNodes();
}
};
int main() {
OrderTracking orders;
cout << "\nOrder Tracking List after deleting all:" << endl;
orders.displayOrders();
return 0;
}
OUTPUT :