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

SDP Assignment1 (1)

The document outlines an assignment for a course where the student, Chan Serey, is required to implement a linked list to manage student data. It includes C++ code for functions to add, display, delete, and search for students in the list, as well as a menu-driven interface for user interaction. The assignment specifies the use of a file containing student data and provides sample input and output scenarios.

Uploaded by

Da Vy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

SDP Assignment1 (1)

The document outlines an assignment for a course where the student, Chan Serey, is required to implement a linked list to manage student data. It includes C++ code for functions to add, display, delete, and search for students in the list, as well as a menu-driven interface for user interaction. The assignment specifies the use of a file containing student data and provides sample input and output scenarios.

Uploaded by

Da Vy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

SDP Assognment.

md 2025-03-31

Assignment SDP (Course)

Name : CHAN SEREY

ID: e20230409

Group: I2-GTR

Assignment 2's homework: Read student data of I2 AMS-GIC-GTR from file student list and store in Linked list.
Test functions: add end, add begin, display all, delete begin, search. Student list file is attached below.

.Input

#include <iostream>
#include <string>
using namespace std;

// Node structure for the linked list


struct Student {
int id;
string name;
Student* next;
};

// Linked list structure


struct List {
Student* head;
};

// Function to create an empty list


List* createEmptyList() {
List* list = new List;
list->head = nullptr;
return list;
}

// Add student at the beginning


void addBegin(List* list, int id, string name) {
Student* newStudent = new Student{id, name, list->head};
list->head = newStudent;
}

// Add student at the end


void addEnd(List* list, int id, string name) {
Student* newStudent = new Student{id, name, nullptr};
if (!list->head) {
list->head = newStudent;
return;
}
1/4
SDP Assognment.md 2025-03-31

Student* temp = list->head;


while (temp->next) temp = temp->next;
temp->next = newStudent;
}

// Display all students in a list


void displayMyList(List* list) {
Student* temp = list->head;
while (temp) {
cout << "ID: " << temp->id << ", Name: " << temp->name << endl;
temp = temp->next;
}
}

// Delete first student


void deleteBegin(List* list) {
if (!list->head) return;
Student* temp = list->head;
list->head = list->head->next;
delete temp;
}

// Search for a student by name


void search(List* list, string name) {
Student* temp = list->head;
while (temp) {
if (temp->name == name) {
cout << "Student Found: " << temp->name << " (ID: " << temp->id <<
")\n";
return;
}
temp = temp->next;
}
cout << "Student not found!\n";
}

int main() {
List* listAMS = createEmptyList();
List* listGIC = createEmptyList();
List* listGTR = createEmptyList();

int choice;
do {
cout << "\nMenu:\n";
cout << "1. Add Student\n";
cout << "2. Display Students\n";
cout << "3. Search Student\n";
cout << "4. Delete First Student\n";
cout << "5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;

if (choice == 1) {
int id;
2/4
SDP Assognment.md 2025-03-31

string name;
int listChoice;
cout << "Enter Student ID: ";
cin >> id;
cout << "Enter Student Name: ";
cin.ignore();
getline(cin, name);

cout << "Select List: 1. AMS 2. GIC 3. GTR: ";


cin >> listChoice;

if (listChoice == 1) addEnd(listAMS, id, name);


else if (listChoice == 2) addEnd(listGIC, id, name);
else if (listChoice == 3) addEnd(listGTR, id, name);
else cout << "Invalid choice!\n";
}
else if (choice == 2) {
int listChoice;
cout << "Select List to Display: 1. AMS 2. GIC 3. GTR: ";
cin >> listChoice;

if (listChoice == 1) displayMyList(listAMS);
else if (listChoice == 2) displayMyList(listGIC);
else if (listChoice == 3) displayMyList(listGTR);
else cout << "Invalid choice!\n";
}
else if (choice == 3) {
string name;
int listChoice;
cout << "Enter Student Name to Search: ";
cin.ignore();
getline(cin, name);

cout << "Select List: 1. AMS 2. GIC 3. GTR: ";


cin >> listChoice;

if (listChoice == 1) search(listAMS, name);


else if (listChoice == 2) search(listGIC, name);
else if (listChoice == 3) search(listGTR, name);
else cout << "Invalid choice!\n";
}
else if (choice == 4) {
int listChoice;
cout << "Select List to Delete First Student: 1. AMS 2. GIC 3. GTR: ";
cin >> listChoice;

if (listChoice == 1) deleteBegin(listAMS);
else if (listChoice == 2) deleteBegin(listGIC);
else if (listChoice == 3) deleteBegin(listGTR);
else cout << "Invalid choice!\n";
}
} while (choice != 5);

3/4
SDP Assognment.md 2025-03-31

return 0;
}

.Output

Menu:
1. Add Student
2. Display Students
3. Search Student
4. Delete First Student
5. Exit
Enter your choice: 1
Enter Student ID: 101
Enter Student Name: Jonh
Select List: 1. AMS 2. GIC 3. GTR: 3

Menu:
1. Add Student
2. Display Students
3. Search Student
4. Delete First Student
5. Exit
Enter your choice: 1
Enter Student ID: 102
Enter Student Name: Steven
Select List: 1. AMS 2. GIC 3. GTR: 3

Menu:
1. Add Student
2. Display Students
3. Search Student
4. Delete First Student
5. Exit
Enter your choice: 1
Enter Student ID: 103
Enter Student Name: Jack
Select List: 1. AMS 2. GIC 3. GTR: 3

Menu:
1. Add Student
2. Display Students
3. Search Student
4. Delete First Student
5. Exit

Enter your choice: 2


Select List to Display: 1. AMS 2. GIC 3. GTR: 3
ID: 101, Name: Jonh
ID: 102, Name: Steven
ID: 103, Name: Jack

4/4

You might also like