-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4_5_remove_student_record.cpp
More file actions
131 lines (104 loc) · 2.51 KB
/
4_5_remove_student_record.cpp
File metadata and controls
131 lines (104 loc) · 2.51 KB
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
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
struct listNode {
int studentNum;
int grade;
listNode * next;
};
typedef listNode * studentCollection;
void printNodes(studentCollection sc);
void addRecord(studentCollection& sc, int studNum, int gr);
void removeRecord(studentCollection& sc, int studNum);
double averageRecord(studentCollection sc);
void removeLinkedList(studentCollection &head);
int main()
{
studentCollection sc = nullptr;
addRecord(sc, 1001, 78);
addRecord(sc, 1012, 93);
addRecord(sc, 1076, 85);
cout << "The nodes: " << endl;
printNodes(sc);
cout << "The nodes after removing student nr. 1012: " << endl;
removeRecord(sc, 1012);
printNodes(sc);
cout << "The average record is: " << averageRecord(sc) << endl;
removeLinkedList(sc);
cin.get();
return 0;
}
void addRecord(studentCollection& sc, int studNum, int gr)
{
listNode * newNode = new listNode;
newNode->studentNum = studNum;
newNode->grade = gr;
newNode->next = sc;
sc = newNode;
}
void removeRecord(studentCollection& sc, int studNum)
{
if (sc == nullptr) return;
listNode * loopPtr = sc;
if (loopPtr->studentNum == studNum)
{
sc = loopPtr->next;
delete loopPtr;
}
else
{
while (loopPtr->next != nullptr)
{
if (loopPtr->next->studentNum == studNum)
{
listNode * deleteNode = loopPtr->next;
loopPtr->next = loopPtr->next->next;
delete deleteNode;
break;
}
else
{
loopPtr = loopPtr->next;
}
}
}
return;
}
void printNodes(studentCollection sc)
{
listNode * loopPtr = sc;
while (loopPtr != nullptr)
{
printf("Student nr: %d grade: %d \n", loopPtr->studentNum, loopPtr->grade);
loopPtr = loopPtr->next;
}
}
double averageRecord(studentCollection sc)
{
if (sc == nullptr) return 0;
int count = 0;
double sum = 0;
listNode * loopPtr = sc;
while (loopPtr != nullptr)
{
sum += loopPtr->grade;
count++;
loopPtr = loopPtr->next;
}
double average = sum / count;
return average;
}
void removeLinkedList(studentCollection &head)
{
listNode * loopPtr = head;
listNode * deleteNode;
while (loopPtr != nullptr)
{
deleteNode = loopPtr;
loopPtr = loopPtr->next;
delete deleteNode;
}
head = nullptr;
return;
}