Node.h
#ifndef __NODE_H__ #define __NODE_H__ template <class T> struct Node { T data; Node<T> * next; }; #endif
LinkedList.h
#ifndef __LINKEDLIST_H__ #define __LINKEDLIST_H__ #include "Node.h" template <class T> class LinkedList { public: LinkedList(); LinkedList(T *_nodes, int _length); virtual ~LinkedList(); public: void Insert(int _pos, T _node); T Delete(int _pos); void setNode(int _pos, T _node); int getLength(); T getNode(int _pos); int LocateNode(T _node); void Traversal(); private: Node<T> *firstNode; }; #endif
LinkedList.cpp
#include "LinkedList.h" template <class T> LinkedList<T>::LinkedList() { firstNode = new Node(); firstNode->next = nullptr; } template <class T> LinkedList<T>::LinkedList(T *_nodes, int _length) { firstNode = new Node<T>; Node<T> *tail = firstNode; for (int i = 0; i < _length; i++) { Node<T> *tempNode = new Node<T>; tempNode->data = *(_nodes + i); tail->next = tempNode; tail = tail->next; } tail->next = nullptr; } template <class T> LinkedList<T>::~LinkedList() { while (firstNode != nullptr) { Node<T> *deleteNode = firstNode; firstNode = firstNode->next; delete deleteNode; } } template <class T> void LinkedList<T>::Insert(int _pos, T _node) { Node<T> *tempNode = firstNode; Node<T> *insertNode = new Node<T>; for (int i = 1; i <= _pos - 1; i++) tempNode = tempNode->next; insertNode->data = _node; insertNode->next = tempNode->next; tempNode->next = insertNode; } template <class T> T LinkedList<T>::Delete(int _pos) { T tempdata; Node<T> *delNode = new Node<T>; Node<T> *tempNode = firstNode; for (int i = 1; i <= _pos - 1; i++) { tempNode = tempNode->next; delNode = tempNode->next; } tempNode->next = tempNode->next->next; tempdata = delNode->data; delete delNode; return tempdata; } template <class T> void LinkedList<T>::setNode(int _pos, T _node) { Node<T> *setNode = firstNode; for (int i = 1; i <= _pos; i++) setNode = setNode->next; setNode->data = _node; } template <class T> int LinkedList<T>::getLength() { int count = 0; Node<T> *countNode = firstNode; countNode = countNode->next; while (countNode != nullptr) { countNode = countNode->next; count += 1; } return count; } template <class T> T LinkedList<T>::getNode(int _pos) { T getData; Node<T> *getNode = firstNode; for (int i = 1; i <= _pos; i++) getNode = getNode->next; getData = getNode->data; return getData; } template <class T> int LinkedList<T>::LocateNode(T _Node) { int locate = 0; Node<T> *locNode = firstNode; for (int i = 1; i <= this->getLength(); i++) { locate += 1; locNode = locNode->next; if (locNode->data == _Node) break; } return locate; } template <class T> void LinkedList<T>::Traversal() { Node<T> * traNode = firstNode->next; while (traNode != nullptr) { cout << traNode->data << " "; traNode = traNode->next; } cout << endl; }
SinglyLinkedList.cpp
#include <iostream> #include "Node.h" #include "LinkedList.h" #include "LinkedList.cpp" using namespace std; int main(void) { int nodes[4] = {11, 23, 76, 45}; LinkedList<int> *list = new LinkedList<int>(nodes, 4); list->Traversal(); list->Insert(4, 0); list->Traversal(); cout << list->Delete(4) << endl; list->Traversal(); list->s