采用C++模板类实现
1 #ifndef _List_H_ 2 #define _List_H_ 3 #include <iostream> 4 #include "stdlib.h" 5 template <class T> 6 class CNode 7 { 8 public: 9 T data; 10 CNode<T> *next; 11 }; 12 template <class T> 13 class CLink 14 { 15 public: 16 CLink(); 17 CLink(const CNode<T> &init_data); 18 ~CLink(); 19 void InsertHead(const T &data); //从头插入节点 20 bool Insert(const int pos,const T &data ); //在pos位置之前插入data; 21 bool Delete(const int pos); //删除第Pos个元素 22 int Get_Length()const; //获得链表长度 23 bool Find_Data(const T& data)const; //返回链表中是否存在data. 24 bool IsEmpty() const; //判断链表是否为空 25 bool SetAt(const int pos,const T& data); //将位置pos的值设为data 26 void Remove(); //清空链表 27 void Print()const; //打印链表中的内容 28 protected: 29 CNode<T> *head; 30 int Count; //记录链表中元素个数 31 32 }; 33 34 35 template <class T> 36 CLink<T>::CLink() 37 { 38 head = new CNode<T>; 39 head->next=NULL; 40 Count = 0; 41 } 42 template <class T> 43 CLink<T>::~CLink() 44 { 45 if(head->next!=NULL) 46 { 47 CNode<T>* p = head; 48 CNode<T>* q; 49 while (p->next!=NULL) 50 { 51 q = p->next; 52 p->next = q->next; 53 delete q; 54 } 55 delete head; 56 } 57 template <class T> 58 CLink<T>::CLink(const CNode<T> &initdata) 59 { 60 head = new CNode<T>; 61 head->next = NULL; 62 head->data = initdata; 63 Count = 0; 64 } 65 template <class T> 66 void CLink<T>::InsertHead(const T& data) 67 { 68 CNode<T> *newnode = new CNode<T>; 69 newnode->data = data; 70 newnode->next = head->next; 71 head->next = newnode; 72 ++Count; 73 } 74 template <class T> 75 void CLink<T>::Print()const 76 { 77 CNode<T>* p = new CNode<T>; 78 p = head; 79 while(p->next) 80 { 81 p = p->next; 82 std::cout<<p->data<<" "; 83 } 84 } 85 template <class T> 86 bool CLink<T>::Insert(const int pos,const T& data) 87 { 88 int i=1; 89 if (pos>Count+1||pos<1) 90 { 91 std::cout<<"插入位置不存在"<<std::endl; 92 return false; 93 } 94 else 95 { 96 CNode<T>* newNode = new CNode<T>; 97 CNode<T>* p = head->next; 98 newNode->data = data; 99 while(i<=Count&&i!=pos-1) 100 { 101 i++; 102 p = p->next; 103 } 104 newNode->next = p->next; 105 p->next = newNode; 106 ++Count; 107 return true; 108 } 109 } 110 template <class T> 111 bool CLink<T>::Delete(const int pos) 112 { 113 int i = 0; //应该从0开始,注意删除首元节点 114 if (pos<1||pos>Count) 115 { 116 std::cout<<"删除元素不存在"<<std::endl; 117 return false; 118 } 119 else 120 { 121 CNode<T>* p = head; 122 CNode<T>* q; 123 while(i<=Count-1&&i!=pos-1) 124 { 125 i++; 126 p = p->next; 127 } 128 q = p->next; 129 p->next = q->next; 130 delete q; 131 --Count; 132 return true; 133 } 134 } 135 template<class T> 136 bool CLink<T>::Find_Data(const T& data)const 137 { 138 if(!Count||!(head->next)) 139 { 140 return false; 141 } 142 else 143 { 144 CNode<T>* p =head->next; 145 while (p->data !=data&&p->next!=NULL) 146 { 147 p = p->next; 148 } 149 if(!p->next) 150 { 151 std::cout<<"链表中不存在数据data"<<std::endl; 152 return false; 153 } 154 else 155 { 156 std::cout<<"链表中存在数据data"<<std::endl; 157 return true; 158 } 159 } 160 } 161 template <class T> 162 bool CLink<T>::IsEmpty() const 163 { 164 if(!head||0==Count) 165 { 166 return true; 167 } 168 else 169 { 170 return false; 171 } 172 } 173 template <class T> 174 bool CLink<T>::SetAt(const int pos,const T& data) 175 { 176 int i = 0; 177 if(!head||pos<1||pos>Count) 178 { 179 return false; 180 } 181 else 182 { 183 CNode<T>* p = head; 184 while(i<=Count&&i!=pos) 185 { 186 p = p->next; 187 i++; 188 } 189 p->data = data; 190 return true; 191 } 192 193 } 194 template <class T> 195 void CLink<T>::Remove() 196 { 197 if(!head->next) 198 { 199 std::cout<<"链表为空"<<std::endl; 200 } 201 else 202 { 203 CNode<T>* p = head; 204 CNode<T>* q; 205 while (p->next!=NULL) 206 { 207 q = p->next; 208 p->next = q->next; 209 delete q; 210 } 211 std::cout<<"链表清除成功"<<std::endl; 212 } 213 } 214 template <class T> 215 int CLink<T>::Get_Length()const 216 { 217 return Count; 218 } 219 220 #endif//_List_H_