single link list lab task 3
single link list lab task 3
Reg # 2312101
Section # BSCS-3A
Subject: DSA LAB
Date: 30/09/2024
Lab task-1:
#include <iostream>
using namespace std;
class MyList {
private:
int *items;
int size;
int pos;
public:
MyList() {
size = 0;
cout << "Enter size for array: ";
cin >> size;
items = new int[size];
pos = 0;
}
void add(int val) {
if (pos < size) {
items[pos++] = val;
} else {
cout << "List is full. Cannot add more items." << endl;
}
}
int retrieve(int loc) {
if (loc >= 0 && loc < pos) {
return items[loc];
} else {
cout << "Invalid location." << endl;
return -1;
}
}
void insertAnywhere(int val, int loc) {
if (loc >= 0 && loc < pos && pos < size) {
for (int i = pos; i > loc; i--) {
items[i] = items[i - 1];
}
items[loc] = val;
pos++;
} else if (loc == pos && pos < size) {
add(val);
} else {
cout << "Location not correct or list is full." << endl;
}
}
void deleteAnywhere(int loc) {
if (loc >= 0 && loc < pos) {
for (int i = loc; i < pos - 1; i++) {
items[i] = items[i + 1];
}
pos--;
} else {
cout << "Invalid location." << endl;
}
}
void display() {
if (pos == 0) {
cout << "List is empty." << endl;
return;
}
cout << "List contents: ";
for (int i = 0; i < pos; i++) {
cout << items[i] << " ";
}
cout << endl;
}
int length() {
return pos;
}
};
int main() {
MyList list;
int val = 0, loc = 0;
int ch = 0;
do {
cout << "Press 1 for Add value in sequence" << endl;
cout << "Press 2 for Retrieve value" << endl;
cout << "Press 3 for Insert value Anywhere" << endl;
cout << "Press 4 for Display" << endl;
cout << "Press 5 for Delete in sequence" << endl;
cout << "Press 6 for Delete Anywhere" << endl;
cout << "Press 7 for Length of the list" << endl;
cout << "Press 8 for Quit" << endl;
cin >> ch;
switch (ch) {
case 1:
cout << "Enter value to add: ";
cin >> val;
list.add(val);
break;
case 2:
cout << "Enter location to retrieve: ";
cin >> loc;
val = list.retrieve(loc);
if (val != -1) {
cout << "Value at location " << loc << ": " << val << endl;
}
break;
case 3:
cout << "Enter value to insert: ";
cin >> val;
cout << "Enter location for insertion: ";
cin >> loc;
list.insertAnywhere(val, loc);
break;
case 4:
list.display();
break;
case 5:
cout << "Enter location to delete: ";
cin >> loc;
list.deleteAnywhere(loc);
break;
case 6:
cout << "Enter location for sequential delete: ";
cin >> loc;
list.deleteAnywhere(loc);
break;
case 7:
cout << "Length of the list: " << list.length() << endl;
break;
case 8:
exit(0);
default:
cout << "Invalid choice" << endl;
}
} while (true);
return 0;
}
OutPut #
Code-2:
#include <iostream>
using namespace std;
struct Student {
string name;
string regNo;
float cgpa;
int semester;
};
class StudentList {
private:
Student students[10];
int count;
public:
StudentList() : count(0) {}
void displayHighCGPA() {
cout << "Students with CGPA greater than 3.0:\n";
for (int i = 0; i < count; i++) {
if (students[i].cgpa > 3.0) {
cout << "Name: " << students[i].name << ", Reg. No.: " <<
students[i].regNo
<< ", CGPA: " << students[i].cgpa << ", Semester: " <<
students[i].semester << endl;
}
}
}
void displayThirdAndFourthSemester() {
cout << "Students in 3rd and 4th semesters:\n";
for (int i = 0; i < count; i++) {
if (students[i].semester == 3 || students[i].semester == 4) {
cout << "Name: " << students[i].name << ", Reg. No.: " <<
students[i].regNo
<< ", CGPA: " << students[i].cgpa << ", Semester: " <<
students[i].semester << endl;
}
}
}
void displayAlphabetically() {
for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count - i - 1; j++) {
if (students[j].name > students[j + 1].name) {
Student temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
cout << "Students sorted alphabetically:\n";
for (int i = 0; i < count; i++) {
cout << "Name: " << students[i].name << ", Reg. No.: " <<
students[i].regNo
<< ", CGPA: " << students[i].cgpa << ", Semester: " <<
students[i].semester << endl;
}
}
};
int main() {
StudentList studentList;
studentList.addStudent("Arif", "REG 2312154", 4.0, 3);
studentList.addStudent("Shair Zaman", "REG 2312134", 3.9, 3);
studentList.addStudent("Rashid Ali", "REG 696969", 2.5, 3);
studentList.addStudent("Wahab", "REG 2312101", 2.8, 4);
studentList.addStudent("Fatima", "REG 2312102", 3.2, 4);
studentList.displayHighCGPA();
studentList.displayThirdAndFourthSemester();
studentList.displayAlphabetically();
return 0;
}