#include <iostream>
#pragma once
template <typename T>
class Node
{
const char * const _key;
Node *_next;
T _value;
public:
Node(const char * const key, const T &value) : _key(key), _value(value), _next(nullptr) {}
const char * const getKey()
{
return _key;
}
Node *getNext()
{
return _next;
}
void setNext(Node *next)
{
_next = next;
}
T &getValue()
{
return _value;
}
void print()
{
cout << _key << ", " << _value;
}
};
#include <iostream>
#include "Node.h"
#pragma once
template <typename T>
class LinkedList
{
int _count;
Node<T> *_first;
public:
LinkedList() : _count(0), _first(nullptr) {}
Node<T> *add(const char * const key, const T &value)
{
Node<T> *first = new Node<T>(key, value);
first->setNext(_first);
_first = first;
_count++;
return _first;
}
void remove(const char * const key)
{
Node<T> *next = _first;
Node<T> *previous = nullptr;
while (next)
{
if (next->getKey() == key)
{
if (next == _first)
_first = next->getNext();
else
previous->setNext(next->getNext());
_count--;
return;
}
previous = next;
next = next->getNext();
}
}
int getCount()
{
return _count;
}
Node<T> * const getFirst()
{
return _first;
}
void print()
{
Node<T> *next = _first;
while (next)
{
next->print();
cout << "\n";
next = next->getNext();
}
}
};