-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedList.h
77 lines (66 loc) · 1.6 KB
/
LinkedList.h
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
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <iostream>
using namespace std;
template <typename T>
class Node {
public:
T data;
Node* next;
Node(T val) : data(val), next(NULL) {}
};
template <typename T>
class LinkedList {
private:
Node<T>* head;
public:
LinkedList() : head(NULL) {}
// Insert a new node at the end of the linked list
void insert(T data) {
Node<T>* newNode = new Node<T>(data);
if (!head) {
head = newNode;
} else {
Node<T>* temp = head;
while (temp->next) {
temp = temp->next;
}
temp->next = newNode;
}
}
// Accessor method to get the head of the linked list
Node<T>* getHead() const {
return head;
}
// Find a student by ID
T* findById(const string& id) {
Node<T>* temp = head;
while (temp) {
if (temp->data.getId() == id) {
return &(temp->data);
}
temp = temp->next;
}
return NULL;
}
// Print all student data
void printAll() const {
Node<T>* temp = head;
cout << "ID CW Marks FE Marks Total Grade\n";
cout << "----------------------------------------------\n";
while (temp) {
temp->data.print();
temp = temp->next;
}
}
// Destructor to clean up memory
~LinkedList() {
Node<T>* temp = head;
while (temp) {
Node<T>* next = temp->next;
delete temp;
temp = next;
}
}
};
#endif