Files
BasicsOfComputerSoftwareEng…/OOP/12/Optional01/node.h
2023-05-17 09:48:55 +08:00

67 lines
1.6 KiB
C++
Executable File

#pragma once
#include <iostream>
template <class E> class Node {
private:
E content;
Node *nextNode;
public:
Node();
Node(E _content, Node<E> *_nextNode = NULL);
~Node();
// Returns a reference to the content.
E &getContent();
// Returns a reference to the next Node. May return NULL if this Node is the
// last one.
Node<E> *getNextPtr();
// Set the next Node _nextNode. Returns THIS Node.
Node<E> &setNext(Node<E> *_nextNode);
// Returns a reference to the Tail.
Node<E> &getTail();
// Returns a reference to the Node of given index.
Node<E> &getByIndex(const int index);
void destruct();
};
template <class E> Node<E>::Node() : content(0), nextNode(NULL){};
template <class E>
Node<E>::Node(E _content, Node *_nextNode)
: content(_content), nextNode(_nextNode){};
template <class E> Node<E>::~Node(){};
template <class E> E &Node<E>::getContent() {
return this->content;
}
template <class E> Node<E> *Node<E>::getNextPtr() {
return this->nextNode;
}
template <class E> Node<E> &Node<E>::setNext(Node<E> *_nextNode) {
this->nextNode = _nextNode;
return *this;
}
template <class E> Node<E> &Node<E>::getTail() {
if (this->nextNode == NULL) {
return *this;
}
return this->nextNode->getTail();
}
template <class E> Node<E> &Node<E>::getByIndex(const int index) {
if (index == 0) {
return *this;
}
return this->nextNode->getByIndex(index - 1);
}
template <class E> void Node<E>::destruct() {
if (this->nextNode != NULL) {
this->nextNode->destruct();
}
delete this;
}