Implement a single linklist

来源:互联网 发布:男士通勤包推荐知乎 编辑:程序博客网 时间:2024/06/05 05:32
  1. //LinkList.h
  2. #include<iostream>
  3. using namespace std;
  4. template<typename T>
  5. //Node for LinkList
  6. struct Node
  7. {
  8.     T data;
  9.     Node<T>* next;        //the link for next node
  10.     Node(Node<T>* ptr=NULL)
  11.     {
  12.         next = ptr;
  13.     }
  14.     Node(const T& item,Node<T>* ptr=NULL)
  15.     {
  16.         data = item,next = ptr;
  17.     }
  18. };
  19. template<typename T>
  20. class LinkList
  21. {
  22.     Node<T>* head;
  23. public:
  24.     LinkList();
  25.     LinkList(const T& item);
  26.     LinkList(LinkList<T>& );
  27.     LinkList<T>& operator=(LinkList<T>& L);
  28.     ~LinkList();
  29.     ////////////////////////////////
  30.     int Length() const//calculate the length of LinkList
  31.     T GetData(int i);    //get the value of ith element
  32.     void SetHead(Node<T>* );
  33.     void CreateList();    //create list by insert back method
  34.     void MakeEmpty();
  35.     void Display();
  36.     Node<T>* GetHead()const;
  37.     Node<T>* Search(T x);   //find address of 'x'
  38.     Node<T>* Locate(int i);  //find address of ith element
  39.     bool Insert(int i,T& x);  //insert value 'x' after ith element
  40.     bool Delete(int i,T& x);  //remove the ith element,and return it's value by 'x'
  41.     bool IsEmpty()const;
  42.     bool IsFull()const;
  43. };
  44. /////////////////////////////////////////////
  45. //LinkList.cpp
  46. #include "LinkList.h"
  47. template<typename T>
  48. LinkList<T>::LinkList()
  49. {
  50.     head = new Node<T>;
  51. }
  52. template<typename T>
  53. LinkList<T>::LinkList(const T& x)
  54. {
  55.     head = new Node<T> (x);
  56. }
  57. template<typename T>
  58. LinkList<T>::LinkList(LinkList<T>& L)
  59. {
  60.     T value;
  61.     Node<T> *p = L.GetHead();
  62.     Node<T> *q = head = new Node<T>;
  63.     while (p->next)
  64.     {
  65.         value = p->next->data;
  66.         q ->next = new Node<T> (value);  //copy constructor
  67.         q = q->next,p = p->next;
  68.     }
  69.     q->next = NULL;   //
  70. }
  71. template<typename T>
  72. LinkList<T>& LinkList<T>::operator=(LinkList<T>& L)  //overload operator '='
  73. {
  74.     if (this == &L) return this;
  75.     T value;
  76.     Node<T>* p = L.GetHead();
  77.     Node<T>* q = head = new Node<T>;
  78.     while (p->next)
  79.     {
  80.         value = p->next->data;
  81.         q->next = new Node<T> (value);
  82.         p = p->next,q = q->next;
  83.     }
  84.     q->next = NULL;
  85.     return *this;
  86. }
  87. template<typename T>
  88. LinkList<T>::~LinkList()
  89. {
  90.     MakeEmpty();
  91. }
  92. ///////////////////////////////////////////////////////////////////////////
  93. template<typename T>
  94. int LinkList<T>::Length()const
  95. {
  96.     Node<T>* p = head->next;
  97.     int count = 0;
  98.     while (p)
  99.     {
  100.         count++,p = p->next;
  101.     }
  102.     return count;
  103. }
  104. template<typename T>
  105. T LinkList<T>::GetData(int i)
  106. {
  107.     if (i<0) return NULL;
  108.     Node<T>* p = Locate(i);
  109.     if (p == NULL) return -1;   //i overflow...
  110.     return p->data;
  111. }
  112. template<typename T>
  113. inline void LinkList<T>::SetHead(Node<T>* p)
  114. {
  115.     head = p;
  116. }
  117. template<typename T>
  118. void LinkList<T>::CreateList()   //insert back
  119. {
  120.     Node<T> *newNode,*last;
  121.     head = new Node<T>;
  122.     if (head == NULL) exit(1);   //allocation memroy error..
  123.     cout<<"create list now,please input the node's value"<<endl;
  124.     cout<<"terminate by push 'Ctrl+Z'"<<endl;
  125.     T val;
  126.     last = head;
  127.     while (cin>>val)
  128.     {
  129.         newNode = new Node<T> (val);
  130.         if (newNode == NULL) exit(1);   //allocation error
  131.         last->next = newNode;
  132.         last = newNode;
  133.     }
  134.     last->next = NULL;
  135. }
  136. template<typename T>
  137. void LinkList<T>::MakeEmpty()
  138. {
  139.     Node<T>* p;
  140.     while (head->next)
  141.     {
  142.         p = head->next, head->next = p->next;
  143.         delete p;
  144.     }
  145. }
  146. template<typename T>
  147. void LinkList<T>::Display()
  148. {
  149.     Node<T>* p = head->next;
  150.     while (p)
  151.     {
  152.         cout<<p->data<<"  ";
  153.         p = p->next;
  154.     }
  155.     cout<<endl;
  156. }
  157. template<typename T>
  158. Node<T>* LinkList<T>::Locate(int i)
  159. {
  160.     if (i<0) return NULL;
  161.     Node<T>* p = head;
  162.     int k = 0;
  163.     while (p && k<i)
  164.     {
  165.         p = p->next,k++;
  166.     }
  167.     return p;
  168. }
  169. template<typename T>
  170. inline Node<T>* LinkList<T>::GetHead()const
  171. {
  172.     return head;
  173. }
  174. template<typename T>
  175. Node<T>* LinkList<T>::Search(T x)
  176. {
  177.     Node<T>* p = head->next;
  178.     while (p)
  179.     {
  180.         if (p->data == x) break;   //
  181.         p = p->next;
  182.     }
  183.     return p;
  184. }
  185. template<typename T>
  186. bool LinkList<T>::Insert(int i,T& x)
  187. {
  188.     Node<T>* p = Locate(i);
  189.     if (p == NULL) return false;
  190.     Node<T>* newNode = new Node<T> (x);
  191.     if (newNode == NULL) exit(1);   //allocation error
  192.     newNode->next = p->next;
  193.     p->next = newNode;
  194.     return true;
  195. }
  196. template<typename T>
  197. bool LinkList<T>::Delete(int i,T& x)
  198. {
  199.     Node<T>* p = Locate(i-1);
  200.     if (p==NULL || p->next==NULL) return false;
  201.     Node<T>* del = p->next;
  202.     p->next = del->next, x = del->data;
  203.     delete del;
  204.     return true;
  205. }
  206. template<typename T>
  207. bool LinkList<T>::IsEmpty()const
  208. {
  209.     return (head->next == NULL) ? true : false;
  210. }
  211. template<typename T>
  212. bool LinkList<T>::IsFull()const
  213. {
  214.     return false;
  215. }
  216. /////////////////////////////////////////////////////////
  217. //main.cpp
  218. #include "LinkList.cpp"
  219. int main(void)              //a simple test
  220. {
  221.     LinkList<int> List_Obj;
  222.     List_Obj.CreateList();  //litter test ......
  223.     cout<<"the length of LinkList is "<<List_Obj.Length()<<endl;
  224.     List_Obj.Display();
  225.     int a = 100;
  226.     List_Obj.Insert(2,a);
  227.     List_Obj.Display();
  228.     List_Obj.Delete(5,a);
  229.     if (List_Obj.Delete(5,a)) cout<<"remove succeed...yeah"<<endl;
  230.     else cout<<"fail to remove"<<endl;
  231.     List_Obj.Display();
  232.     return 0;
  233. }