Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

#include <iostream>

#include <string>

using namespace std;

// Define the structure for each node in the linked list


struct Student {
int IDno;
string name;
int age;
Student* next;
};

// Function to display a student's information


void displayStudent(const Student* student) {
cout << "ID: " << student->IDno << "\tName: " << student->name << "\tAge: " <<
student->age << endl;
}

// Function to insert a new student at the end of the linked list


void insertStudent(Student*& head, int IDno, const string& name, int age) {
Student* newStudent = new Student{IDno, name, age, nullptr};
if (head == nullptr) {
head = newStudent;
} else {
Student* current = head;
while (current->next != nullptr) {
current = current->next;
}
current->next = newStudent;
}
}

// Function to search for a student by ID in the linked list


Student* searchStudent(const Student* head, int IDno) {
const Student* current = head;
while (current != nullptr) {
if (current->IDno == IDno) {
return const_cast<Student*>(current); // Return a non-const pointer
for potential modifications
}
current = current->next;
}
return nullptr; // Student not found
}

// Function to perform bubble sort on the linked list based on student IDs
void bubbleSort(Student* head) {
bool swapped;
Student* ptr1;
Student* lptr = nullptr;

do {
swapped = false;
ptr1 = head;

while (ptr1->next != lptr) {


if (ptr1->IDno > ptr1->next->IDno) {
// Swap the students
swap(ptr1->IDno, ptr1->next->IDno);
swap(ptr1->name, ptr1->next->name);
swap(ptr1->age, ptr1->next->age);
swapped = true;
}
ptr1 = ptr1->next;
}
lptr = ptr1;
} while (swapped);
}

// Function to delete a student with a given ID from the linked list


void deleteStudent(Student*& head, int IDno) {
if (head == nullptr) {
return;
}

if (head->IDno == IDno) {
Student* temp = head;
head = head->next;
delete temp;
return;
}

Student* current = head;


while (current->next != nullptr && current->next->IDno != IDno) {
current = current->next;
}

if (current->next == nullptr) {
return; // Student not found
}

Student* temp = current->next;


current->next = current->next->next;
delete temp;
}

// Function to insert a new student at a specific position in the linked list


void insertAtPosition(Student*& head, int position, int IDno, const string& name,
int age) {
if (position < 1) {
cout << "Invalid position." << endl;
return;
}

Student* newStudent = new Student{IDno, name, age, nullptr};

if (position == 1) {
newStudent->next = head;
head = newStudent;
return;
}

Student* current = head;


for (int i = 1; i < position - 1 && current != nullptr; ++i) {
current = current->next;
}
if (current == nullptr) {
cout << "Invalid position." << endl;
return;
}

newStudent->next = current->next;
current->next = newStudent;
}

// Function to display all students in the linked list


void displayAllStudents(const Student* head) {
const Student* current = head;
while (current != nullptr) {
displayStudent(current);
current = current->next;
}
}

int main() {
Student* head = nullptr;

// 1. Create linked list of 10 nodes


for (int i = 1; i <= 10; ++i) {
insertStudent(head, i, "Student" + to_string(i), 20 + i);
}

// 2. Display each student on the screen


cout << "All Students:" << endl;
displayAllStudents(head);

// 3. Search a student from the list


int searchID = 5;
Student* foundStudent = searchStudent(head, searchID);
if (foundStudent != nullptr) {
cout << "\nStudent with ID " << searchID << " found:" << endl;
displayStudent(foundStudent);
} else {
cout << "\nStudent with ID " << searchID << " not found." << endl;
}

// 4. Sort (bubble) the elements of the list


cout << "\nSorting students by ID:" << endl;
bubbleSort(head);
displayAllStudents(head);

// 5. Delete one member of the list


int deleteID = 3;
deleteStudent(head, deleteID);
cout << "\nStudent with ID " << deleteID << " deleted:" << endl;
displayAllStudents(head);

// 6. Insert an element (new student) at the 7th position


int insertPosition = 7;
insertAtPosition(head, insertPosition, 99, "New Student", 25);
cout << "\nInserted a new student at position " << insertPosition << ":" <<
endl;
displayAllStudents(head);

// Clean up memory by deleting all nodes


Student* current = head;
while (current != nullptr) {
Student* next = current->next;
delete current;
current = next;
}

return 0;
}

You might also like