Skip to content

Instantly share code, notes, and snippets.

@jamesdev4you
Created March 16, 2026 13:14
Show Gist options
  • Select an option

  • Save jamesdev4you/2dfa6ee8560d0c1edc76b2fc72151040 to your computer and use it in GitHub Desktop.

Select an option

Save jamesdev4you/2dfa6ee8560d0c1edc76b2fc72151040 to your computer and use it in GitHub Desktop.
#include <iostream>
// Only way I could use string as an arbitrary data type.
#include <string>
using namespace std;
// Class made to be passed into my linked lists.
// Uses an OPERATOR! Which was recommended by ChatGPT.
// I read more on the topic in order to build it myself - but without the recommendation
// I couldn't figure out how to place a class into my linked list. This default class is GPT based.
class Anime {
public:
string title;
int rating;
string tier;
Anime(string t, int r, string tr) {
title = t;
rating = r;
tier = tr;
}
void Print() {
cout << title << " | Rating: " << rating << " | Tier: " << tier << endl;
}
// Building up from the operator, this one is a boolean operator. Much more familiar since
// bool is a data type that is known, where ostream& from iostream is crazy.
// Const anime& is the same as before, a referenced copy that is constant so that it doesn't get
// edited. other, is just anything comparing with Anime&.
// The operator then compares title - which is easily accessible since the function is in the class.
// and other.title gets compared. This compares the two TITLES. Which is all the is needed for
// remove node to occur.
bool operator==(const Anime& other) const {
return title == other.title;
}
};
// Operators are important for repackaging this for my other classes.
// Since each data is analyzed under two conditions:
// "<<" in cout.
// "==" in remove nodes.
// First for the print operator.
// It must return ostream& since it is apart of an output stream.
// The operator used is: <<, so operator<<
// Then we take the stream it is in (in tandem with the << operator): "ostream& os"
// and a reference to the orginal object (so that it is a copy), and make sure it is
// const so that it doesn't get modified "const Anime& a"
// The function is then made! So now it must have an output, which is just the os stream lined
// with the anime properties "a"
ostream& operator<<(ostream& os, const Anime& a) {
os << a.title << " | Rating: " << a.rating << " | Tier: " << a.tier;
return os;
}
// A singlle node for the linked list that has a typical data, next, structure.
template <typename T1>
class Node
{
public:
T1 data;
Node<T1>* next;
Node(T1 value) : data(value), next(nullptr) {}
};
// Linked list is the tier maker. Can make an groupings of anime tier rankings.
template <typename T1>
class LinkedList
{
public:
Node<T1>* start;
Node<T1>* head = nullptr;
LinkedList() {
start = nullptr;
head = nullptr;
}
// Adds similar arbitrary data type to linked list. Extends from start.
void AddANode(T1 data) {
Node<T1>* temp = new Node<T1>(data);
if(start == nullptr) {
start = temp;
head = temp;
} else {
head->next = temp;
head = temp;
}
}
void RemoveANode(T1 data) {
if(start == nullptr) return;
// Removing first node
if(start->data == data) {
Node<T1>* toDelete = start;
start = start->next;
if(start == nullptr) {
head = nullptr;
}
delete toDelete;
return;
}
Node<T1>* temp = start;
while(temp->next != nullptr) {
if(temp->next->data == data) {
Node<T1>* toDelete = temp->next;
// Update head if deleting last node
if(toDelete == head) {
head = temp;
}
temp->next = toDelete->next;
delete toDelete;
return;
}
temp = temp->next;
}
cout << "Node does not exist.\n";
}
// Prints out all values in linked list.
void PrintMe(){
if(start == nullptr){
return;
}
Node<T1>* temp = start;
while(temp != nullptr){
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
// Deconstructor to ensure no memory leaks.
~LinkedList() {
Node<T1>* current = start;
while(current != nullptr) {
Node<T1>* nextNode = current->next;
delete current;
current = nextNode;
}
}
};
int main() {
LinkedList<Anime> animeList;
int choice;
do {
cout << "\n--- Anime Tier List ---\n";
cout << "1. Add Anime\n";
cout << "2. Remove Anime\n";
cout << "3. Display List\n";
cout << "4. Exit\n";
cout << "Choice: ";
cin >> choice;
if(choice == 1) {
string title, tier;
int rating;
cout << "Title: ";
getline(cin >> ws, title);
cout << "Rating: ";
cin >> rating;
cout << "Tier: ";
cin >> tier;
animeList.AddANode(Anime(title, rating, tier));
}
else if(choice == 2) {
string title;
cout << "Enter title to remove: ";
cin >> title;
animeList.RemoveANode(Anime(title, 0, ""));
}
else if(choice == 3) {
animeList.PrintMe();
}
} while(choice != 4);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment