線性表之單鏈表
一、頭文件:LinkedList.h
1 //單鏈表是用一組任意的存儲單元存放線性表的元素,這組單元可以是連續的也可以是不連續的,甚至可以是零散分布在內存中的任意位置。 2 //單鏈表頭文件 3 #include<iostream> 4 using namespace std; 5 //定義單鏈表結點-結構體類型 6 template<class DataType> 7 struct Node 8 { 9 //數據域,存放該結點的數據 10 DataType data; 11 //指針域,指向下一個結點 12 Node<DataType> *next; 13 }; 14 15 template<class DataType> 16 class LinkedList{ 17 public: 18 //單鏈表無參構造器 19 LinkedList(); 20 //單鏈表有參構造器 21 LinkedList(DataType array[], int n); 22 LinkedList(int n, DataType array[]); 23 //單鏈表析構函數 24 ~LinkedList(); 25 //獲取單鏈表的長度 26 int GetLength(); 27 //查找單鏈表指定元素的序號 28 int GetLocal(DataType x); 29 //獲取單鏈表指序號的元素 30 DataType GetElement(int index); 31 //單鏈表中在指定位置插入指定的元素 32 void Insert(int index, DataType x); 33 //在單鏈表中刪除指定位置的元素 34 DataType Delete(int index); 35 //按序號輸出單鏈表中的元素 36 void PrintLinkedList(); 37 38 private : 39 //聲明單鏈表的頭指針 40 Node<DataType> *first; 41 }; 42 43 //實現單鏈表的無參構造函數 44 template<class DataType> 45 LinkedList<DataType>::LinkedList() 46 { 47 first = new Node<DataType>; 48 first->next = NULL; 49 } 50 51 //頭插法建立單鏈表 52 template<class DataType> 53 LinkedList<DataType>::LinkedList(int n,DataType array[]) 54 { 55 //初始化一個空鏈表 56 first = new Node<DataType>; 57 first->next = NULL; 58 for (int i = 0; i < n; i++) 59 { 60 //為每一個數組元素都申請新結點 61 Node<DataType> *s = new Node<DataType>; 62 //數組元素賦值給結點數據域 63 s->data = array[i]; 64 //將結點插入到頭結點之前 65 s->next = first->next; 66 first->next = s; 67 68 } 69 } 70 71 //尾插法建立單鏈表 72 template<class DataType> 73 LinkedList<DataType>::LinkedList(DataType array[], int n) 74 { 75 //生成頭結點 76 first = new Node<DataType>; 77 //定義尾結點 78 Node<DataType> *r = first; 79 for (int i = 0; i < n; i++) 80 { 81 //為每一個數組元素申請一個結點 82 Node<DataType> *s = new Node<DataType>; 83 //把數組元素賦值給結點的數據域 84 s->data = array[i]; 85 //將每一個結點追加到終端結點之后 86 r->next = s; 87 r = s; 88 } 89 //尾結點尾NULL 90 r->next = NULL; 91 } 92 93 //實現單鏈表的析構函數 94 template<class DataType> 95 LinkedList<DataType>::~LinkedList() 96 { 97 //聲明工作指針 98 Node<DataType> *q; 99 while (first != NULL) 100 { 101 //暫存被釋放的結點 102 q = first; 103 //讓頭指針指向要釋放結點的下一個結點 104 first = first->next; 105 delete q; 106 } 107 } 108 109 //實現單鏈表插入:在指定的位置插入指定的元素 110 template<class DataType> 111 void LinkedList<DataType>::Insert(int index, DataType x) 112 { 113 //定義工作指針 114 Node<DataType> *p = first->next; 115 //定義計數器,初始值為0 116 int count = 0; 117 while (p != NULL &&count < index - 1) 118 { 119 //工作指針后移 120 p = p->next; 121 count ++; 122 } 123 //找到 index-1 的位置 124 if (p == NULL) 125 { 126 throw "插入的位置有誤"; 127 } 128 else 129 { 130 //申請一個新結點 131 Node<DataType> *s; 132 s= new Node<DataType>; 133 //其數據域為 x 134 s->data = x; 135 //在新結點的指針域存放工作指針p的指針域 136 s->next = p->next; 137 //將結點s插入到p結點之后 138 p->next = s; 139 } 140 } 141 142 //實現單鏈表的按值查找,返回指定元素在單鏈表中的序號(如不存在,則返回0) 143 template<class DataType> 144 int LinkedList<DataType>::GetLocal(DataType x) 145 { 146 //定義工作指針 147 Node<DataType> *p = first->next; 148 //定義計數器,初始值是1 149 int count = 1; 150 //查找序號所對應的位置 151 while (p != NULL) 152 { 153 if (p->data == x) 154 { 155 return count; 156 } 157 //工作指針后移 158 p = p->next; 159 //計數器加1 160 count++; 161 } 162 //如果找不到該元素,則返回0 163 return 0; 164 } 165 166 //實現單鏈表按位查找,返回指定位置的元素 167 template<class DataType> 168 DataType LinkedList<DataType>::GetElement(int index) 169 { 170 //定義工作指針 171 Node<DataType> *p = first->next; 172 //定義計數器,初始值是1 173 int count = 1; 174 //查找序號所對應的位置 175 while (p != NULL&&count < index) 176 { 177 //工作指針后移 178 p = p->next; 179 //計數器加1 180 count++; 181 } 182 //如果找到單鏈表的末尾,還找不到指定的位置,則拋出異常 183 if (p == NULL) 184 { 185 throw "查找的位置有誤"; 186 } 187 else 188 { 189 //當找到合適的位置時,返回該位置上的元素 190 return p->data; 191 } 192 193 } 194 195 //實現獲取單鏈表的長度 196 template<class DataType> 197 int LinkedList<DataType>::GetLength() 198 { 199 //定義計數器,用來計算單鏈表的長度 200 int count = 0; 201 //定義工作指針 202 Node<DataType> *p = first->next; 203 while (p != NULL) 204 { 205 p = p->next; 206 count++; 207 } 208 return count; 209 210 } 211 212 //實現單鏈表的按序號輸出元素 213 template<class DataType> 214 void LinkedList<DataType>::PrintLinkedList() 215 { 216 //聲明工作指針 217 Node<DataType> *p; 218 //初始化工作指針 219 p = first->next; 220 while(p != NULL) 221 { 222 cout << p->data << " "; 223 //工作指針向后移動 224 p = p->next; 225 } 226 cout << endl; 227 } 228 229 //實現單鏈表的刪除 230 template<class DataType> 231 DataType LinkedList<DataType>::Delete(int index) 232 { 233 Node<DataType> *p = first->next; 234 int count = 0; 235 //查找第 index-1 位置結點 236 while (p != NULL&&count < index - 1) 237 { 238 p = p->next; 239 count++; 240 } 241 //如果能找到 242 if (p == NULL || p->next == NULL) 243 { 244 throw "刪除的位置有誤"; 245 } 246 else 247 { 248 Node<DataType> *q = p->next; 249 DataType x = q->data; 250 p->next = q->next; 251 delete q; 252 return x; 253 } 254 }
二、測試線性表之單鏈表的源文件:TestLinkedList.cpp
1 #include<iostream> 2 #include "LinkedList.h" 3 using namespace std; 4 void show() 5 { 6 cout << "---------------------------------------" << endl; 7 } 8 int main() 9 { 10 int array[] = { 1, 3, 5, 2, 7, 6, 9, 8, 10, 4}; 11 //聲明單鏈表 12 LinkedList<int> linkedList = LinkedList<int>(10,array); 13 cout << "輸出單鏈表:" << endl; 14 linkedList.PrintLinkedList(); 15 show(); 16 cout << "單鏈表的長度:" << linkedList.GetLength() << endl; 17 cout << "單鏈表中第5個元素是:" << linkedList.GetElement(5) << endl; 18 cout << "單鏈表中元素5的位置是:" << linkedList.GetLocal(5) << endl; 19 show(); 20 cout << "在單鏈表的第5個位置上插入元素22" << endl; 21 linkedList.Insert(5, 22); 22 cout << "輸出單鏈表:" << endl; 23 linkedList.PrintLinkedList(); 24 cout << "單鏈表的長度:" << linkedList.GetLength() << endl; 25 show(); 26 cout << "刪除第5位置的元素" << endl; 27 linkedList.Delete(5); 28 cout << "輸出單鏈表:" << endl; 29 linkedList.PrintLinkedList(); 30 cout << "單鏈表的長度:" << linkedList.GetLength() << endl; 31 show(); 32 return 0; 33 }
三、運行示例結果

