1. 程式人生 > >資料結構線性表之鏈式儲存結構單鏈表(C++)

資料結構線性表之鏈式儲存結構單鏈表(C++)

一. 標頭檔案—linkedlist.h

  1 #ifndef _LIKEDLIST_H_
  2 #define _LIKEDLIST_H_
  3 
  4 #include <iostream>
  5 
  6 template <class T>
  7 struct Node
  8 {
  9     T data;
 10     Node<T> *next;
 11 };
 12 
 13 template <class T>
 14 class linkedlist
 15 {
 16 public:
 17     linkedlist(); /*
無參建構函式*/ 18 linkedlist(T array[], int n); /*頭插法建構函式*/ 19 linkedlist(int n, T array[]); /*尾插法建構函式*/ 20 ~linkedlist(); /*解構函式*/ 21 22 int GetLength(); /*返回線性表長度*/ 23 T GetElement(int i); /*返回第i個位置的元素值*/ 24 void InsertElement(int i, T x); /*在i位置處插入新元素x*/ 25 void DeleteElement(int
i); /*刪除i位置處的元素*/ 26 27 void Printlinkedlist(); /*列印連結串列元素*/ 28 private: 29 Node<T> *first; /*頭指標*/ 30 }; 31 32 /*無參建構函式*/ 33 template <class T> 34 linkedlist<T>::linkedlist() 35 { 36 /*新建頭指標*/ 37 first = new Node<T>; 38 /*指向空*/ 39 first->next = NULL;
40 } 41 42 /*前插法建構函式*/ 43 template <class T> 44 linkedlist<T>::linkedlist(T array[], int n) 45 { 46 /*新建頭指標*/ 47 first = new Node<T>; 48 first->next = NULL; 49 /*線性表元素賦值*/ 50 for(int i = 0; i < n; i++) 51 { 52 Node<T> *s = new Node<T>; 53 s->data = array[i]; 54 s->next = first->next; 55 first->next = s; 56 } 57 } 58 59 /*後插法建構函式*/ 60 template <class T> 61 linkedlist<T>::linkedlist(int n, T array[]) 62 { 63 /*新建頭指標*/ 64 first = new Node<T>; 65 first->next = NULL; 66 Node<T> *r = first; 67 /*線性表元素賦值*/ 68 for(int i = 0; i < n; i++) 69 { 70 Node<T> *s = new Node<T>; 71 s->data = array[i]; 72 r->next = s; 73 r = s; 74 } 75 r->next = NULL; 76 } 77 78 /*解構函式*/ 79 template <class T> 80 linkedlist<T>::~linkedlist() 81 { 82 /*判斷是否為空表*/ 83 if (first != NULL) 84 { 85 /*暫存刪除節點*/ 86 Node<T> *s = first; 87 /*更新頭節點*/ 88 first = first->next; 89 /*刪除暫存節點*/ 90 delete s; 91 } 92 } 93 94 /*列印線性表元素*/ 95 template <class T> 96 int linkedlist<T>::GetLength() 97 { 98 /*新建一個指向第一個節點的指標*/ 99 Node<T> *s = new Node<T>; 100 s = first->next; 101 int j = 0; 102 while (s != NULL) 103 { 104 j++; 105 s = s->next; 106 } 107 return j; 108 } 109 110 /*返回第i個位置的元素值*/ 111 template <class T> 112 T linkedlist<T>::GetElement(int i) 113 { 114 Node<T> *s = new Node<T>; 115 s = first->next; 116 int j = 1; 117 /*判斷位置是否合理*/ 118 while (s != NULL) 119 { 120 if (j == i) 121 return s->data; 122 s = s->next; 123 j++; 124 } 125 if (j < i) 126 { 127 throw"不存在位置i"; 128 cout<<"不存在位置i"<<endl; 129 return -1; 130 } 131 } 132 133 /*在i位置處插入新元素x*/ 134 template <class T> 135 void linkedlist<T>::InsertElement(int i, T x) 136 { 137 /*新建一個指向頭節點的指標*/ 138 Node<T> *s = new Node<T>; 139 s = first; 140 int j = 0; 141 /*判斷位置是否合理*/ 142 while (s != NULL) 143 { 144 /*尋找第i-1位置*/ 145 if (j == i-1) 146 { 147 Node<T> *r = new Node<T>; 148 r->data = x; 149 r->next = s->next; 150 s->next = r; 151 break; 152 } 153 else 154 { 155 s = s->next; 156 j++; 157 } 158 } 159 if (j < i-1) 160 { 161 throw "不存在位置i"; 162 cout<<"不存在位置i"<<endl; 163 } 164 } 165 166 /*刪除i位置處的元素*/ 167 template <class T> 168 void linkedlist<T>::DeleteElement(int i) 169 { 170 /*新建一個指向頭節點的指標*/ 171 Node<T> *s = new Node<T>; 172 s = first; 173 int j = 0; 174 /*判斷位置是否合理*/ 175 while (s != NULL) 176 { 177 /*尋找第i-1位置*/ 178 if (j == i-1) 179 { 180 s->next = s->next->next; 181 break; 182 } 183 else 184 { 185 s = s->next; 186 j++; 187 } 188 } 189 if (j < i-1) 190 { 191 throw "不存在位置i"; 192 cout<<"不存在位置i"<<endl; 193 } 194 } 195 196 /*列印線性表元素*/ 197 template <class T> 198 void linkedlist<T>::Printlinkedlist() 199 { 200 /*新建一個指向第一個節點的指標*/ 201 Node<T> *s = new Node<T>; 202 s = first->next; 203 while (s != NULL) 204 { 205 cout<<s->data<<" "; 206 s = s->next; 207 } 208 cout << endl; 209 } 210 #endif

二. 測試檔案—test.cpp

 1 #include "linkedlist.h"
 2 #include <iostream>
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     int array[5] = {1,2,3,4,5};
 8     int len = 5;
 9     /*頭插法連結串列list1*/
10     linkedlist<int> list1 = linkedlist<int>(array,len);
11     /*頭插法連結串列list2*/
12     linkedlist<int> list2 = linkedlist<int>(len,array);
13     cout<<"頭插法連結串列list1:";
14     list1.Printlinkedlist();
15     cout<<"尾插法連結串列list2:";
16     list2.Printlinkedlist();
17     cout<<"在list1連結串列的第2個位置插入6"<<endl;
18     int a = 6;
19     list1.InsertElement(2,a);
20     cout<<"list1連結串列為:";
21     list1.Printlinkedlist();
22     a = list1.GetElement(2);
23     cout<<"list1連結串列第2個元素為:"<<a<<endl;    
24     int b = list1.GetLength();
25     cout<<"list1的長度為"<<b<<endl;
26     cout<<"刪除list1連結串列的第4個位置的元素"<<endl;
27     list1.DeleteElement(4);
28     cout<<"list1連結串列為:";
29     list1.Printlinkedlist();
30     return 0;
31 }

三. 測試結果