Dynamic structure: each student is a node in a singly linked list.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
struct Student {
int roll;
string name;
double marks;
Student* next;
Student(int r, const string& n, double m) : roll(r), name(n), marks(m), next(nullptr) {}
};
class StudentList {
Student* head;
public:
StudentList() : head(nullptr) {}
~StudentList() {
while (head) {
Student* tmp = head;
head = head->next;
delete tmp;
}
}
void addStudent(int roll, const string& name, double marks) {
Student* node = new Student(roll, name, marks);
if (!head) { head = node; return; }
Student* cur = head;
while (cur->next) cur = cur->next;
cur->next = node;
}
void displayAll() const {
if (!head) { cout << "No students.\n"; return; }
cout << left << setw(8) << "Roll" << setw(20) << "Name" << setw(8) << "Marks" << '\n';
for (Student* cur = head; cur; cur = cur->next)
cout << left << setw(8) << cur->roll << setw(20) << cur->name << setw(8) << cur->marks << '\n';
}
Student* find(int roll) const {
for (Student* cur = head; cur; cur = cur->next)
if (cur->roll == roll) return cur;
return nullptr;
}
bool update(int roll, const string& name, double marks) {
Student* s = find(roll);
if (!s) return false;
s->name = name; s->marks = marks;
return true;
}
bool remove(int roll) {
if (!head) return false;
if (head->roll == roll) {
Student* tmp = head; head = head->next; delete tmp; return true;
}
Student* prev = head; Student* cur = head->next;
while (cur) {
if (cur->roll == roll) { prev->next = cur->next; delete cur; return true; }
prev = cur; cur = cur->next;
}
return false;
}
};
int main() {
StudentList db;
db.addStudent(1, "Alice", 85.5);
db.addStudent(2, "Bob", 90.0);
db.addStudent(3, "Charlie", 78.2);
cout << "Linked List Database:\n";
db.displayAll();
db.update(2, "Bob Updated", 95.0);
cout << "\nAfter update:\n"; db.displayAll();
db.remove(1);
cout << "\nAfter deletion:\n"; db.displayAll();
}
Formula‑Based Representation
Direct indexing: roll number maps to array index using a formula.
#include <iostream>
#include <string>
#include <vector>
#include <optional>
#include <iomanip>
using namespace std;
struct Student {
int roll;
string name;
double marks;
};
class StudentDB {
int baseRoll, maxRoll;
vector<optional<Student>> table;
public:
StudentDB(int base, int maxR) : baseRoll(base), maxRoll(maxR) {
table.resize(maxRoll - baseRoll + 1);
}
bool add(int roll, const string& name, double marks) {
if (roll < baseRoll || roll > maxRoll) return false;
int idx = roll - baseRoll;
if (table[idx].has_value()) return false;
table[idx] = Student{roll, name, marks};
return true;
}
bool update(int roll, const string& name, double marks) {
if (roll < baseRoll || roll > maxRoll) return false;
int idx = roll - baseRoll;
if (!table[idx].has_value()) return false;
table[idx]->name = name; table[idx]->marks = marks;
return true;
}
bool remove(int roll) {
if (roll < baseRoll || roll > maxRoll) return false;
int idx = roll - baseRoll;
if (!table[idx].has_value()) return false;
table[idx].reset(); return true;
}
void displayAll() const {
cout << left << setw(8) << "Roll" << setw(20) << "Name" << setw(8) << "Marks" << '\n';
for (int i = 0; i < (int)table.size(); ++i)
if (table[i].has_value())
cout << left << setw(8) << table[i]->roll << setw(20) << table[i]->name << setw(8) << table[i]->marks << '\n';
}
};
int main() {
StudentDB db(1, 100); // roll numbers 1–100
db.add(1, "Alice", 85.5);
db.add(2, "Bob", 90.0);
db.add(3, "Charlie", 78.2);
cout << "Formula-Based Database:\n";
db.displayAll();
db.update(2, "Bob Updated", 95.0);
cout << "\nAfter update:\n"; db.displayAll();
db.remove(1);
cout << "\nAfter deletion:\n"; db.displayAll();
}
⚖️ Comparison
Linked list: flexible, no fixed size, but slower search (O(n)).
Formula array: instant lookup (O(1)), but requires known roll range and wastes memory if sparse.
You can compile either with:
g++ -std=c++17 linked_list_students.cpp -o linked_list_students
./linked_list_students
OR
g++ -std=c++17 formula_array_students.cpp -o formula_array_students
./formula_array_students