-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCourseList.cpp
170 lines (148 loc) · 5.15 KB
/
CourseList.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include "CourseList.h"
#include <iostream>
using namespace std;
// Constructor: Initialize the head to nullptr
CourseList::CourseList() {
head = nullptr;
}
// Destructor: Call the deleteAllNodes() function to free memory
CourseList::~CourseList() {
deleteAllNodes();
}
// Function to add a course in increasing order by course code
void CourseList::addCourse(int courseCode, int creditHours, char grade) {
CourseNode* newNode = new CourseNode(courseCode, creditHours, grade);
// Insert at the beginning if the list is empty or the new node is smaller than head
if (!head || head->courseCode > courseCode) {
newNode->next = head;
head = newNode;
}
else {
// Traverse to find the correct position to insert the node
CourseNode* nodeptr = head;
while (nodeptr->next && nodeptr->next->courseCode < courseCode) {
nodeptr = nodeptr->next;
}
newNode->next = nodeptr->next; // assign next node to new node pointer
nodeptr->next = newNode; // place new node at correct position
}
}
bool CourseList::isEmpty() {
if (!head) {
// List is empty
cout << "The course list is empty.\n\n";
return true;
}
else return false;
}
// Function to delete a course by course code
void CourseList::deleteCourse(int courseCode) {
// Special case: the course to delete is the head
if (head->courseCode == courseCode) {
CourseNode* temp = head;
head = head->next;
delete temp;
cout << "Course deleted successfully.\n\n";
return;
}
// Traverse to find the course to delete
CourseNode* nodeptr = head;
bool found = false;
while (nodeptr->next && nodeptr->next->courseCode != courseCode) {
nodeptr = nodeptr->next;
}
// Check if nodeptr->next is the node we want to delete
if (nodeptr->next && nodeptr->next->courseCode == courseCode) {
found = true;
CourseNode* temp = nodeptr->next;
nodeptr->next = nodeptr->next->next; // Re-link the next pointer
delete temp; // Delete the found node
cout << "Course deleted successfully.\n\n";
}
if (!found) {
cout << "Course not found.\n\n";
}
}
// Function to delete all nodes from the list
void CourseList::deleteAllNodes() {
while (head) {
CourseNode* temp = head;
head = head->next;
delete temp;
}
}
// Function to display all courses in the list
void CourseList::displayCourses() const {
CourseNode* nodeptr = head;
if (!nodeptr) {
cout << "No courses available.\n\n";
return;
}
while (nodeptr) {
cout << "\tCourse Code: " << nodeptr->courseCode
<< ", Credit Hours: " << nodeptr->creditHours
<< ", Grade: " << nodeptr->grade << endl;
nodeptr = nodeptr->next;
}
cout << "\n";
}
char validateGrade() {
char grade;
while (true) {
cout << "\tEnter Grade (single character): ";
cin >> grade;
if (cin.fail() || cin.get() != '\n') { // Check if input is a single character
cout << "\tInvalid input. Please enter a single character.\n";
cin.clear(); // Clear error flags
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Discard invalid input
}
else {
return grade;
}
}
}
int validateCreditHours() {
int creditHours;
while (true) {
cout << "\tEnter Credit Hours (1 to 5): ";
cin >> creditHours;
if (cin.fail() || creditHours < 1 || creditHours > 5) {
cout << "\tInvalid input. Please enter an integer between 1 and 5.\n";
cin.clear(); // Clear error flags
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Discard invalid input
}
else {
return creditHours;
}
}
}
int validateCourseCode() {
int courseCode;
while (true) {
cout << "\tEnter Course Code (3 digits): ";
cin >> courseCode;
if (cin.fail() || courseCode < 100 || courseCode > 999) {
cout << "\tInvalid input.Please enter a 3 - digit integer.\n";
cin.clear(); // Clear error flags
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Discard invalid input
}
else {
return courseCode;
}
}
}
void displayAndDeleteCourses(CourseList& courseList) {
// Display the full list of courses before deletion
cout << "\nDisplaying all courses before deletion:\n";
courseList.displayCourses();
// Delete two courses based on user input
for (int n = 0; n < 2; n++) {
int courseCode;
cout << "Enter the Course Code of the course to delete: ";
courseCode = validateCourseCode(); // Reuse the validation function for correct input
courseList.deleteCourse(courseCode);
}
// Display the full list of courses after deletion
cout << "\nDisplaying all courses after deletion:\n";
courseList.displayCourses();
}