数据结构—单链表(模板类)

  链表概念--链表是一种线性表,但是并不是顺序存储,而是每个节点里面存储着下一个节点的指针,把存储数据元素的数据串链起来

单链表的实现

 

  1 #include<iostream>
  2 #include<assert.h>
  3 using namespace std;
  4 
  5 template <class T>
  6 struct Node
  7 {
  8  Node(const T& x)
  9  :_data(x)
 10  , _pNext(NULL)
 11  { }
 12 
 13  Node<T> *_pNext;
 14  T _data;
 15 };
 16 template <class T>
 17 class SList
 18 {
 19 public:
 20  SList()
 21  :_pHead(NULL)
 22  , _size(0)
 23  { }
 24 
 25  SList(const SList<T>& l)
 26  {
 27  Node<T>*pNode = l._pHead;
 28 
 29  while (pNode)
 30  {
 31  PushBack(pNode->_data);
 32  pNode = pNode->_pNext;
 33  }
 34  }
 35 
 36  ~SList()
 37  {
 38  Clear();
 39  }
 40 
 41  void Clear()
 42  {
 43  Node<T>*pNode = _pHead;
 44 
 45  while (pNode)
 46  {
 47  Node<T>* pDel = pNode;
 48  pNode = pNode->_pNext;
 49  delete pDel;
 50  }
 51  _pHead = NULL;
 52  _size = 0;
 53  }
 54 
 55  SList& operator = (const SList& other)
 56  {
 57  if (this != &other)
 58  {
 59 
 60  Node<T> *pNode = other._pHead;
 61  while (NULL != pNode)
 62  {
 63  PushBack(pNode->_data);
 64  pNode = pNode->_pNext;
 65  }
 66  }
 67 
 68  return *this;
 69  }
 70 
 71  //尾插
 72  void PushBack(const T& x)
 73  {
 74  Node<T>*pNode = _BuyNode(x);
 75  Node<T>*cur = _pHead;
 76  if (Empty())
 77  {
 78  _pHead = pNode;
 79  }
 80  else
 81  {
 82  while (cur->_pNext)
 83  {
 84  cur = cur->_pNext;
 85  }
 86  cur->_pNext = pNode;
 87  }
 88  _size++;
 89  }
 90  //尾删
 91  void PopBack()
 92  {
 93  if (Empty())
 94  {
 95  return;
 96  }
 97  else if (_size == 1)
 98  {
 99  delete _pHead;
100  _pHead = NULL;
101  _size = 0;
102  }
103  else
104  {
105  Node<T>*pNode = _pHead;
106  Node<T>*pPreNode = NULL;
107  while (pNode->_pNext)
108  {
109  pPreNode = pNode;
110  pNode = pNode->_pNext;
111  }
112  pPreNode->_pNext = NULL;
113  delete pNode;
114  _size--;
115  }
116 
117  }
118  //头插
119  void PushFront(const T&x)
120  {
121  Node<T>*pNode = _BuyNode(x);
122  if (Empty())
123  {
124  _pHead = pNode;
125  }
126  else
127  {
128  pNode->_pNext = _pHead;
129  _pHead = pNode;
130  }
131  _size++;
132  }
133  //头删
134  void PopFront()
135  {
136  if (Empty())
137  {
138  return;
139  }
140  else if (_size == 1)
141  {
142  delete _pHead;
143  _pHead = NULL;
144  _size = 0;
145  }
146  else
147  {
148  Node<T>*pNewNode = _pHead;
149  _pHead = _pHead->_pNext;
150  delete pNewNode;
151  pNewNode = NULL;
152  _size--;
153  }
154  }
155  //显示
156  void Print()
157  {
158  Node<T>*cur = _pHead;
159  while (cur)
160  {
161  cout << cur->_data << "->";
162 cur = cur->_pNext;
163  }
164  cout << "NULL";
165  }
166  //查找
167  Node<T>* Serach(const T&x)
168  {
169  if (Empty())
170  {
171  return 0;
172  }
173  Node<T>*cur = _pHead;
174  while (cur)
175  {
176  if (cur->_data == x)
177  {
178  return cur;
179  }
180  cur = cur->_pNext;
181  }
182  return NULL;
183  }
184  //删除
185  void Erase(Node<T>*pos)
186  {
187 
188  if (Empty())
189  {
190  return;
191  }
192  if (pos == _pHead)
193  {
194  _pHead = pos->_pNext;
195  delete pos;
196  }
197  else
198  {
199  Node<T>*cur = _pHead;
200  while (cur)
201  {
202  if (cur->_pNext == pos)
203  {
204  cur->_pNext = pos->_pNext;
205  delete pos;
206  }
207  cur = cur->_pNext;
208  }
209  }
210  }
211 
212  //排序(冒泡法)
213  void BubbleSort()
214  {
215  Node<T>* ptail = NULL;
216  Node<T>*pNode = NULL;
217  Node<T>* ppreNode = NULL;
218 
219  if (_pHead == NULL || _pHead->_pNext == NULL)
220  {
221  return;
222  }
223  while (_pHead != ptail)
224  {
225  int exchange = 0;
226  ppreNode = _pHead;
227  pNode = _pHead->_pNext;
228  while (pNode != ptail)
229  {
230  if (ppreNode->_data > pNode->_data)
231  {
232  T temp;
233  temp = ppreNode->_data;
234  ppreNode->_data = pNode->_data;
235  pNode->_data = temp;
236  exchange = 1;
237  }
238  ppreNode = pNode;
239  pNode = pNode->_pNext;
240  }
241  if (exchange == 0)
242  {
243  return;
244  }
245  ptail = ppreNode;
246  }
247  }
248 
249  //逆置(前插)
250  void Reverse()
251  {
252  Node<T>* pPreNode = NULL;
253  Node<T>* pNewNode = NULL;
254  Node<T>* pNode = _pHead;
255  if (_pHead == NULL || (_pHead)->_pNext == NULL)
256  {
257  return;
258  }
259  while (pNode)
260  {
261  pPreNode = pNode;
262  pNode = pNode->_pNext;
263  pPreNode->_pNext = pNewNode;
264  pNewNode = pPreNode;
265  }
266  _pHead = pNewNode;
267  }
268 
269  bool Empty()
270  {
271  return _size == 0;
272  }
273 
274  T & operator[](size_t index)
275  {
276  if (index >= _size)
277  {
278  cout << "index error" << endl;
279  abort();
280  }
281  Node<T>* pNode = _pHead;
282  while (index--)
283  {
284 pNode = pNode->_pNext;
285  }
286  return pNode->_data;
287  }
288 
289  const T & operator[](size_t index) const
290  {
291  if (index >= _size)
292  {
293  cout << "index error" << endl;
294  abort();
295  }
296  Node* pNode = _pHead;
297  while (index--)
298  {
299 pNode = pNode->_pNext;
300  }
301  return pNode->_data;
302  }
303 
304  friend ostream& operator << (ostream& _cout, const SList<T>&l)
305  {
306  Node<T> *pNode = l._pHead;
307 
308  while (pNode)
309  {
310  _cout << pNode->_data << "->";
311 pNode = pNode->_pNext;
312  }
313  _cout << "NULL";
314  return _cout;
315  }
316 
317  Node<T>*FindMidNode();
318 
319 private:
320  Node<T>* _BuyNode(const T& data)
321  {
322  return new Node<T>(data);
323  }
324 
325 private:
326  Node<T> *_pHead;
327  size_t _size;
328 };
329 //查找单链表的中间节点
330 template <typename T>
331 Node<T>*SList<T>::FindMidNode()
332 {
333  Node<T>*pFast = _pHead;
334  Node<T>*pSlow = _pHead;
335  while (pFast&&pFast->_pNext)
336  {
337  pFast = pFast->_pNext->_pNext;
338  pSlow = pSlow->_pNext;
339  }
340  return pSlow;
341 }
342 
343 void main()
344 {
345  SList<int> s;
346 
347  s.PushBack(6);
348  s.PushBack(3);
349  s.PushBack(7);
350  cout << s[0] << endl;
351  cout << s << endl;
352 
353  SList<int> s1(s);
354 
355  cout << s1 << endl;
356 
357  s.Print();
358 
359 }

 

posted @ 2016-04-19 13:25  A_carat_tear  阅读(407)  评论(0编辑  收藏  举报