定义(LinkList.h ):
1 #pragma once 2 3 //节点结构体 4 template<class T> 5 struct Node 6 { 7 T value; 8 Node<T> *next; 9 }; 10 11 template<class T> 12 class LinkList 13 { 14 private: 15 unsigned count; //节点个数 16 Node<T> *head; //表头指针 17 Node<T> *tail; //表尾指针 18 public: 19 LinkList(); //构造函数 20 ~LinkList(); //析构函数 21 unsigned length(); //返回链表长度 22 T first(); //返回第一个节点值 23 T last(); //返回最后一个节点值 24 void addNode(T); //添加节点 25 void insert(T, unsigned); //在指定位置之后插入 26 void insert_before_head(T); //在头节点之前插入 27 void remove(unsigned); //移除指定位置的节点 28 void print_all(); //输出所有节点 29 };
实现(LinkList.cpp):
1 #include "LinkList.h" 2 #include <iostream> 3 4 //构造函数 5 template<class T> 6 LinkList<T>::LinkList() 7 { 8 head = NULL; 9 tail = NULL; 10 count = 0; 11 } 12 13 //析构函数 14 template<class T> 15 LinkList<T>::~LinkList() 16 { 17 Node<T> *cur; 18 while (head != NULL) 19 { 20 cur = head->next; 21 delete head; 22 head = cur; 23 } 24 count = 0; 25 } 26 27 //返回链表长度 28 template<class T> 29 unsigned LinkList<T>::length() 30 { 31 return count; 32 } 33 34 //返回第一个节点值 35 template<class T> 36 T LinkList<T>::first() 37 { 38 return head->value; 39 } 40 41 //返回最后一个节点值 42 template<class T> 43 T LinkList<T>::last() 44 { 45 return tail->value; 46 } 47 48 //添加节点 49 template<class T> 50 void LinkList<T>::addNode(T value) 51 { 52 Node<T> *node = new Node<T>; 53 node->value = value; 54 node->next = NULL; 55 if (count == 0) 56 { 57 head = node; 58 tail = node; 59 } 60 else 61 { 62 tail->next = node; 63 tail = node; 64 } 65 count++; 66 } 67 68 //在指定位置之后插入 69 template<class T> 70 void LinkList<T>::insert(T value, unsigned index) 71 { 72 if (index < 1 || index > count) 73 { 74 std::cout << "index error!\n"; 75 return; 76 } 77 Node<T> *node = new Node<T>; 78 node->value = value; 79 //如果是最后一个节点 80 if (index == count) 81 { 82 node->next = NULL; 83 tail->next = node; 84 tail = node; 85 } 86 else 87 { 88 Node<T> *cur = head; 89 while (--index) 90 { 91 cur = cur->next; 92 } 93 node->next = cur->next; 94 cur->next = node; 95 } 96 count++; 97 } 98 99 //在头节点之前插入 100 template<class T> 101 void LinkList<T>::insert_before_head(T value) 102 { 103 Node<T> *node = new Node<T>; 104 node->value = value; 105 node->next = head; 106 head = node; 107 count++; 108 } 109 110 //移除指定位置的节点 111 template<class T> 112 void LinkList<T>::remove(unsigned index) 113 { 114 if (index < 1 || index > count) 115 { 116 std::cout << "index error!\n"; 117 return; 118 } 119 Node<T> *cur = head; 120 if (index == 1) 121 { 122 head = cur->next; 123 delete cur; 124 } 125 else 126 { 127 int i = index; 128 while (--i > 1) 129 { 130 cur = cur->next; 131 } 132 if (index == count) 133 { 134 delete cur->next; 135 cur->next = NULL; 136 } 137 else 138 { 139 Node<T> *temp = cur->next->next; 140 delete cur->next; 141 cur->next = temp; 142 } 143 } 144 count--; 145 } 146 147 //输出所有节点 148 template<class T> 149 void LinkList<T>::print_all() 150 { 151 Node<T> *cur = head; 152 while (cur != NULL) 153 { 154 std::cout << cur->value << " "; 155 cur = cur->next; 156 } 157 }