-
-
Save Usman032/67578c4221ce683ef069e856ebbdcaa3 to your computer and use it in GitHub Desktop.
Simple Single Linked List with Student Class Implementation in C++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <string> | |
| using namespace std; | |
| #define null 0 | |
| class Student { | |
| public: | |
| string name; | |
| int age; | |
| float gpa; | |
| Student(string name, int age, float gpa) { | |
| this->name = name; | |
| this->age = age; | |
| this->gpa = gpa; | |
| } | |
| }; | |
| class Node { | |
| public: | |
| Student* stu; | |
| Node* next; | |
| Node(Student* stu) { | |
| this->stu = stu; | |
| next = null; | |
| } | |
| }; | |
| class LinkedList { | |
| private: | |
| Node* head; | |
| Node* tail; | |
| int size; | |
| public: | |
| LinkedList() { | |
| head = null; | |
| tail = null; | |
| size = 0; | |
| } | |
| int getSize() { | |
| return size; | |
| } | |
| void append(string name, int age, float gpa) { | |
| Student *stud = new Student(name, age, gpa); | |
| Node *node = new Node(stud); | |
| if(head == null) { | |
| head = node; | |
| tail = node; | |
| } else { | |
| tail->next = node; | |
| tail = node; | |
| } | |
| size++; | |
| } | |
| void prepend(string name, int age, float gpa) { | |
| Student *stud = new Student(name, age, gpa); | |
| Node *node = new Node(stud); | |
| if(head == null) { | |
| head = node; | |
| tail = node; | |
| } else { | |
| node->next = head; | |
| head = node; | |
| } | |
| size++; | |
| } | |
| void removeStu(string name) { | |
| if(head != null) { | |
| Node *current = head; | |
| if(current->stu->name == name) { | |
| delete current; | |
| } else { | |
| while(current != null) { | |
| if(current->next->stu->name == name) { | |
| current->next = current->next->next; | |
| break; | |
| } else { | |
| current = current->next; | |
| } | |
| } | |
| } | |
| size--; | |
| } | |
| } | |
| void removeHead() { | |
| if(head != null) { | |
| Node *current = head; | |
| head = head->next; | |
| delete current; | |
| size--; | |
| } | |
| } | |
| void display() { | |
| Node *current = head; | |
| while(current != null) { | |
| cout << current->stu->name << ", " << current->stu->age << ", " << current->stu->gpa << endl; | |
| current = current->next; | |
| } | |
| cout << "*" << getSize() << endl; | |
| cout << endl; | |
| } | |
| void run() { | |
| append("Koko", 17, 3.1); | |
| append("Omar", 22, 2.8); | |
| append("Karim", 18, 1.7); | |
| prepend("Muhammad", 21, 3.5); | |
| display(); | |
| remove("Koko"); | |
| display(); | |
| } | |
| void destroy() { | |
| Node* current; | |
| while(head != null) { | |
| current = head->next; | |
| delete head; | |
| head = current; | |
| } | |
| head = null; | |
| tail = null; | |
| size = 0; | |
| } | |
| ~LinkedList() { | |
| destroy(); | |
| } | |
| }; | |
| int main() { | |
| LinkedList list; | |
| list.run(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment