-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedList.cpp
72 lines (65 loc) · 1.5 KB
/
LinkedList.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
#include "main.h"
/**
* insertNode - insert a new node with collected information for a subject
* @head: head of the subject linked list
* @score: score for a particular subject
* @creditHours: credit hours for a particular subject
* @gradepoint: grade point calculated based on subject score
* @subjectName: Name of the subject
*
* Return: Nothing
*/
void insertNode(SubjectNode*& head, double score, int creditHours, const std::string& letterGrade, double gradePoint, const std::string& subjectName)
{
SubjectNode* newNode = new SubjectNode(subjectName, score, creditHours, gradePoint, letterGrade);
if (newNode == nullptr)
{
//cleanupSubjects(newNode);
return;
}
if (head == nullptr)
{
head = newNode;
}
else
{
SubjectNode* current = head;
while (current->next != nullptr)
{
current = current->next;
}
current->next = newNode;
}
}
/**
* cleanMemory - clean the memory allocated for student Linked list.
* @head: head of student Linked list
*
* Return: nothing
*/
void cleanMemory(StudentNode* head)
{
while (head != nullptr)
{
StudentNode* temp = head;
head = head->next;
cleanupSubjects(temp->subjects); // Delete subjects separately
delete temp;
}
}
/**
* cleanupSubjects - free memory allocated for Subjects linked list
* @head: subjects Linked list head.
*
* Return: Nothing
*/
void cleanupSubjects(SubjectNode* head)
{
SubjectNode* current = head;
while (current != nullptr)
{
SubjectNode* next = current->next;
delete current;
current = next;
}
}