数据结构之单链表——C++模板类实现

来源:互联网 发布:linux 调试动态库 编辑:程序博客网 时间:2024/06/06 18:04

转自:http://blog.csdn.net/Mrx_Nh/article/details/60471647


单链表定义

[cpp] view plain copy
  1. #ifndef SinglyLinkedListEDLIST_H_INCLUDED  
  2. #define SinglyLinkedListEDLIST_H_INCLUDED  
  3. #include <bits/stdc++.h>  
  4. using namespace std;  
  5.   
  6. template<class T>  
  7. class LinkNode {  
  8. public:  
  9.         LinkNode(LinkNode<T>* ptr = NULL) {cout << "Constructing a LinkNode by default way!" << endl; link = ptr;}  
  10.         LinkNode(const T& item, LinkNode<T>* ptr = NULL) {cout << "Constructing a LinkNode by defined way!" << endl; data = item, link = ptr;}  
  11.         ~LinkNode() {cout << "Desctructing a LineNode!" << endl; delete link;}  
  12.         T data;                    // Data Scope  
  13.         LinkNode<T> *link;         // Point Scope, Point to next Node  
  14. };  
  15.   
  16. // SinglyLinkedListList with head node  
  17. template <class T>  
  18. class SinglyLinkedList : public LinkNode<T> {  
  19. public:  
  20.         SinglyLinkedList() {cout << "Constructing a SinglyLinkedListList by default way" << endl; first = new LinkNode<T>;}  
  21.         SinglyLinkedList(const T&x);  
  22.         SinglyLinkedList(SinglyLinkedList<T>& L);       // Deep copy constructor function  
  23.         ~SinglyLinkedList() {cout << "Destructing a SinglyLinkedListNode by default way" << endl; delete first;}  
  24.         void makeEmpty();           // Delete all nodes in the SinglyLinkedList  
  25.         int Length() const;         // Get the length of the SinlyLinkedList  
  26.         LinkNode<T> *GetHead() const {return first;} // Return first  
  27.         LinkNode<T> *Search(T x);   // x can only be a variable while T& x  
  28.         LinkNode<T> *Locate(int i); // Get the i-th node's address  
  29.         bool GetData(int i, T& x);        // Get the data in i-th node and let x = data  
  30.         void SetData(int i, T& x);              // Make the data in i-th node equals x  
  31.         bool Insert(int i, T& x);               // Insert a node in i-th with its' data be x  
  32.         bool Remove(int i, T& x);               // Delete the i-th node and make x = node[i]->data  
  33.         bool IsEmpty() const {return first->link == NULL ? true : false;}  
  34.         bool IsFull() const {return false;}  
  35.         void Sort();                            // Sort all datas in the SinglyLinkedList by non-decreasing way  
  36.         void Input();                           // Input all elements  
  37.         void Output();                          // Out all elements  
  38.         SinglyLinkedList<T>& operator=(SinglyLinkedList<T>& L); // Overloading operator =  
  39. protected:  
  40.         LinkNode<T> *first;     // Point to Head node  
  41. };  
  42.   
  43. #endif // SinglyLinkedListEDLIST_H_INCLUDED  

单链表实现

[cpp] view plain copy
  1. #include <bits/stdc++.h>  
  2. #include "SinglyLinkedList.h"  
  3.   
  4. using namespace std;  
  5.   
  6. template<class T>  
  7. SinglyLinkedList<T>::SinglyLinkedList(const T&x) {  
  8.         first = new LinkNode<T>;  
  9.         if(first == NULL) {  
  10.                 cerr << "Invalid allocation " << endl;  
  11.         }  
  12.         first->link->data = x;  
  13. }  
  14.   
  15. template<class T>  
  16. SinglyLinkedList<T>::SinglyLinkedList(SinglyLinkedList<T>& L) {        // Deep copy constructor function  
  17.         LinkNode<T> *destptr = first = new LinkNode<T>;  
  18.         LinkNode<T> *srcptr = L.GetHead();  
  19.         while(srcptr->link != NULL){  
  20.                 destptr->link = srcptr->link;  
  21.                 destptr->link->data = srcptr->link->data;  
  22.                 destptr = destptr->link;  
  23.                 srcptr = srcptr->link;  
  24.         }  
  25. }  
  26.   
  27. template<class T>  
  28. void SinglyLinkedList<T>::makeEmpty() {  
  29.         // Cannot delete first due to is the head in order to make some operation like insert more easy  
  30.         LinkNode<T> *current;  
  31.         while(first->link != NULL) {  
  32.                current = first->link;  
  33.                first->link = current->link;  
  34.                delete current;  
  35.         }  
  36. }  
  37.   
  38. template<class T>  
  39. int SinglyLinkedList<T>::Length() const {  
  40.         int cnt = 0;  
  41.         LinkNode<T> *current = first;  
  42.         while(current->link != NULL) {  
  43.                 cnt++;  
  44.                 current = current->link;  
  45.         }  
  46.         return cnt;  
  47. }  
  48.   
  49. template<class T>  
  50. LinkNode<T>* SinglyLinkedList<T>::Search(T x) {      // x can only be a variable while T& x  
  51.         LinkNode<T> *current = first;  
  52.         while(current->link != NULL) {  
  53.                 if(current->link->data == x) {  
  54.                         return current;  
  55.                 }  
  56.                 current = current->link;  
  57.         }  
  58.         return NULL;    // Not found  
  59. }  
  60.   
  61. template<class T>  
  62. LinkNode<T>* SinglyLinkedList<T>::Locate(int i) {  
  63.         // Return the address of i-th node  
  64.         if(i < 0) {  
  65.                 cerr << "Invalid Location of " << i << endl;  
  66.                 return NULL;  
  67.         }  
  68.         LinkNode<T> *current = first;  
  69.         int k = 0;  
  70.         while(current != NULL && k < i) {  
  71.                 current = current->link;  
  72.                 k++;  
  73.         }  
  74.         return current;         // Return NULL while i is too large  
  75. }  
  76.   
  77. template<class T>  
  78. bool SinglyLinkedList<T>::GetData(int i, T& x) {  
  79.         if(i <= 0) {  
  80.                 cerr << "Invalid index of " << i << endl;  
  81.                 return false;  
  82.         }  
  83.         LinkNode<T> *current = Locate(i);  
  84.         if(current == NULL) {  
  85.                 return false;  
  86.         }  
  87.         x = current->data;  
  88.         return true;  
  89. }  
  90.   
  91. template<class T>  
  92. void SinglyLinkedList<T>::SetData(int i, T& x) {  
  93.         if(i <= 0) {  
  94.                 cerr << "Invalid index of " << i << endl;  
  95.                 return ;  
  96.         }  
  97.         LinkNode<T> *current = Locate(i);  
  98.         if(current == NULL) return ;  
  99.         current->data = x;  
  100. }  
  101.   
  102. template<class T>  
  103. bool SinglyLinkedList<T>::Insert(int i, T& x) {  
  104.         // Insert a node behind i-th node  
  105.         if(i <= 0) {  
  106.                 cerr << "Invalid index of " << i << endl;  
  107.                 return false;  
  108.         }  
  109.         LinkNode<T> *current = Locate(i-1);  
  110.         if(current == NULL) {  
  111.                 return false;  
  112.         }  
  113.         LinkNode<T> *newLinkNode = new LinkNode<T>(x);  
  114.         if(newLinkNode == NULL) {  
  115.                 cerr << "Invalid allocation " << endl;  
  116.                 return false;  
  117.         }  
  118.         newLinkNode->link = current->link;  
  119.         current->link = newLinkNode;  
  120.         return true;  
  121. }  
  122.   
  123. template<class T>  
  124. bool SinglyLinkedList<T>::Remove(int i, T& x) {  
  125.         // Remove the i-th node is the SinglyLinkedList  
  126.         if(i <= 0) {  
  127.                 cerr << "Invalid position " << endl;  
  128.         }  
  129.         LinkNode<T> *current = Locate(i-1);  
  130.         if(current == NULL || current->link == NULL) {  
  131.                 return false;   // The SinglyLinkedList is too short  
  132.         }  
  133.         LinkNode<T> *del = current->link;  
  134.         x = del->data;  
  135.         current->link = del->link;  
  136.         delete del;  
  137.         return true;  
  138. }  
  139.   
  140. template<class T>  
  141. void SinglyLinkedList<T>::Sort() {  
  142.         // Sort all datas in the SinglyLinkedList by non-decreasing way  
  143.         LinkNode<T> *p = first->link;  
  144.         LinkNode<T> *q;  
  145.         T value;  
  146.         while(p->link != NULL) {  
  147.                 q = p->link;  
  148.                 while(q != NULL) {  
  149.                         if(q->data < p->data) {  
  150.                                 value = p->data;  
  151.                                 p->data = q->data;  
  152.                                 q->data = value;  
  153.                         }  
  154.                         q = q->link;  
  155.                 }  
  156.                 p = p->link;  
  157.         }  
  158. }  
  159.   
  160. template<class T>  
  161. void SinglyLinkedList<T>::Input() {  
  162.         int cnt;  
  163.         cout << "Please enter the total nodes of the SinglyLinkedListedList" << endl;  
  164.         while(true) {  
  165.                 cin >> cnt;  
  166.                 if(cnt <= 0) {  
  167.                         cerr << "Invalid length of SinglyLinkedListedList, the length must be a positive interger " << endl;  
  168.                 }  
  169.                 else break;  
  170.         }  
  171.         LinkNode<T> *current = first;  
  172.         T val;  
  173.         for(int i = 1; i <= cnt; ++i) {  
  174.                 cin >> val;  
  175.                 current->link = new LinkNode<T>(val);  
  176.                 current = current->link;  
  177.         }  
  178. }  
  179.   
  180. template<class T>  
  181. void SinglyLinkedList<T>::Output() {  
  182.         LinkNode<T> *current = first;  
  183.         int cnt = 1;  
  184.         while(current->link != NULL) {  
  185.                 cout << "#No. " << cnt++ << "  "  << current->link->data << endl;  
  186.                 current = current->link;  
  187.         }  
  188. }  
  189.   
  190. template<class T>  
  191. SinglyLinkedList<T>& SinglyLinkedList<T>::operator=(SinglyLinkedList<T>& L) {  
  192.         LinkNode<T> *srcptr, *destptr;  
  193.         destptr = first = new LinkNode<T>;  
  194.         srcptr = L.GetHead();  
  195.         while(srcptr->link != NULL) {  
  196.                 destptr->link = srcptr->link;  
  197.                 destptr->link->data = srcptr->link->data;  
  198.                 destptr = destptr->link;  
  199.                 srcptr = srcptr->link;  
  200.         }  
  201.         destptr->link = NULL;  
  202.         return *this;  
  203. }  
  204. int main()  
  205. {  
  206.         int a = 10;  
  207.         SinglyLinkedList<int> SinglyLinkedListList;  
  208.         if(SinglyLinkedListList.IsEmpty()) {  
  209.                 cout << "Jesus Christ, is empty!" << endl;  
  210.         }  
  211.         SinglyLinkedListList.Input();  
  212.         cout << "Length: " << SinglyLinkedListList.Length() << endl;  
  213.         SinglyLinkedListList.Sort();  
  214.         SinglyLinkedListList.Output();  
  215.         int num = 284;  
  216.         SinglyLinkedListList.Insert(1, num);  
  217.         SinglyLinkedListList.Insert(2, num);  
  218.         SinglyLinkedListList.Insert(3, num);  
  219.         SinglyLinkedListList.Output();  
  220.         cout << "\n----------------------------\n";  
  221.         SinglyLinkedListList.Sort();  
  222.         SinglyLinkedListList.Output();  
  223.         SinglyLinkedListList.GetData(1, a);  
  224.         cout << "a: " << a << endl;  
  225.         cout << "Length before remove the first element is:   " << SinglyLinkedListList.Length() << endl;  
  226.         SinglyLinkedListList.Remove(1, a);  
  227.         cout << "a: " << a << endl;  
  228.         cout << "Length after remove the first element is:   " << SinglyLinkedListList.Length() << endl;  
  229.         SinglyLinkedListList.Output();  
  230.         SinglyLinkedListList.makeEmpty();  
  231.         if(SinglyLinkedListList.IsEmpty()) {  
  232.                 cout << "Jesus Christ, is Empty!" << endl;  
  233.         }  
  234.   
  235.         return 0;  
  236. }  
原创粉丝点击