KEMBAR78
C++ Student LinkedList Demo | PDF | Computer Programming | Software Engineering
0% found this document useful (0 votes)
18 views5 pages

C++ Student LinkedList Demo

The document contains C++ code for a linked list implementation that manages a list of students, including functionalities to append, prepend, remove, and display students. It defines classes for Student and Node, and includes methods for managing the linked list and its memory. The main function demonstrates the usage of the LinkedList class by adding and removing students, and displaying the list's contents.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views5 pages

C++ Student LinkedList Demo

The document contains C++ code for a linked list implementation that manages a list of students, including functionalities to append, prepend, remove, and display students. It defines classes for Student and Node, and includes methods for managing the linked list and its memory. The main function demonstrates the usage of the LinkedList class by adding and removing students, and displaying the list's contents.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

CHETAN JAISWAL

0901CS211041

#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("chetan", 17, 3.1);

append("aayush", 22, 2.8);

append("kishan", 18, 1.7);

prepend("rohit", 21, 3.5);

display();

remove("chetan");

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; }

OUTPUT :

You might also like