线性表之链表

来源:互联网 发布:mac 进度管理软件 编辑:程序博客网 时间:2024/05/17 03:46

    数据结构草草学过,不过没有认真运用过。 虽然知道一些最为基本的抽象类型及一些常用操作,不过叫我把这些基本的算法写出来我也是写不出来的。因为常说数据结构+算法是一个程序员最基本的素质,所以这次认真加以复习。在复习的同时我尽量将自己学习的其他的一些基本知识比如C++中的面向对象思想也引入进来,同时也会将在复习中碰到其他的一些问题提出来,能解决的便解决,不能解决的可以试着解决。
    To be a programmer .

----------------------------------------------------------------------------------------------------------------

    之前的有关线性表的基本操作内容只是针对线性表的顺序存储结构的,本章复习其对应的链式存储。主要熟悉链式操作, 很简单。
    同样,文章的组织结构分为三部分:开头是简单的介绍,之后是数据结构基本运算的代码,最后是在复习中遇到的一些问题。
    以下代码实现了一些线性表最基本的运算,链栈和链队列只是链表的一种特殊形式而已,所以就不写出来了。值得注意的是链栈和链队列
的方向要使得插入删除操作尽可能方便。两点变化:
    1) 在定义数据结构的时候用到了友员:
    2) 注释并没有按照之前的那样,因为觉得函数很简单,有时候并不需要写太长的注释。
    实现代码分三个部分:linklist.h定义了顺序表模版类,main.cpp是用于基本的测试。

----------------------------------------------------------------------------------------------------------------

Code:
  1. // linklist.h   
  2.   
  3. /*  
  4.  * include files  
  5.  *-------------  
  6.  */  
  7. #include <iostream>   
  8.   
  9. /*  
  10.  * constants  
  11.  * ---------  
  12.  */  
  13. #define HEAD 1   
  14. #define TAIL 0   
  15.   
  16. /*  
  17.  * definition for linklist node.  
  18.  * ----------------------------  
  19.  */  
  20. template <class T>   
  21. class Linklist;   
  22.   
  23. template <class T>   
  24. class LinklistNode   
  25. {   
  26. friend class Linklist<T>;   
  27. private:   
  28.     T data;   
  29.     LinklistNode<T> *next;   
  30. };   
  31.   
  32. /*  
  33.  * definition for linklist  
  34.  * -----------------------  
  35.  */  
  36. template <class T>   
  37. class Linklist   
  38. {   
  39.     public:   
  40.         Linklist();   
  41.         ~Linklist();   
  42.         void create(int OPTION);   
  43.         LinklistNode<T> *get(int i);   
  44.         LinklistNode<T> *locate(T key);   
  45.         bool insert(int i, T data);   
  46.         bool delet(int i, T&);   
  47.         void print();   
  48.         int length();   
  49.     private:   
  50.         LinklistNode<T> *head;   
  51. };   
  52.   
  53. /*  
  54.  * implementation for linklist  
  55.  * ---------------------------  
  56.  */  
  57.   
  58. /* constructor */  
  59. template <class T>   
  60. Linklist<T>::Linklist()   
  61. {   
  62.     head = new LinklistNode<T>;   
  63.     head->next = NULL;   
  64. //  head->data = 0;   
  65. }   
  66.   
  67. /* destructor */  
  68. template <class T>   
  69. Linklist<T>::~Linklist()   
  70. {   
  71.   
  72.     LinklistNode<T> *ptrPre;   
  73.     LinklistNode<T> *ptrNex;   
  74.   
  75.     ptrPre = head;   
  76.     ptrNex = ptrPre->next;   
  77.     while (ptrNex != NULL)   
  78.     {   
  79.         delete ptrPre;   
  80.         ptrPre = ptrNex;   
  81.         ptrNex = ptrPre->next;   
  82.     }   
  83.     delete ptrPre;   
  84.   
  85.     ptrPre = NULL;   
  86.     ptrNex = NULL;   
  87. }   
  88.   
  89. /* create one linklist */  
  90. template <class T>   
  91. void Linklist<T>::create(int OPTION )   
  92. {   
  93.     if (OPTION == HEAD)   
  94.     {   
  95.         for (int i=0; i<5; i++)   
  96.         {   
  97.             LinklistNode<T> *newNode = new LinklistNode<T>;   
  98.             newNode->data = i;   
  99.             newNode->next = head->next;   
  100.             head->next = newNode;   
  101.             newNode = NULL;   
  102.         }   
  103.     }   
  104.     else  
  105.     {   
  106.         LinklistNode<T> *tail = head->next;   
  107.         while (tail->next != NULL)   
  108.             tail = tail->next;   
  109.         for (int i=0; i<5; i++)   
  110.         {   
  111.             LinklistNode<T> *newNode = new LinklistNode<T>;   
  112.             newNode->data = i;   
  113.             newNode->next = NULL;   
  114.             tail->next = newNode;   
  115.             tail = newNode;   
  116.             newNode = NULL;   
  117.         }   
  118.   
  119.     }   
  120. }   
  121.   
  122. /* get the node whose index is 'i' */  
  123. template <class T>   
  124. LinklistNode<T>* Linklist<T>::get(int i)   
  125. {   
  126.     LinklistNode<T> *ptrSearch = head->next;   
  127.   
  128.     while (ptrSearch!=NULL && (--i)>0)   
  129.     {   
  130.         ptrSearch = ptrSearch->next;   
  131.     }   
  132.   
  133.     return (ptrSearch == NULL ? NULL : ptrSearch);   
  134. }   
  135.   
  136. /* locate the node by the giving key data */  
  137. template <class T>   
  138. LinklistNode<T>* Linklist<T>::locate(T key)   
  139. {   
  140.     LinklistNode<T> *ptrSearch = head->next;   
  141.   
  142.     while (ptrSearch!=NULL && ptrSearch->data!=key)   
  143.         ptrSearch = ptrSearch->next;   
  144.   
  145.     return (ptrSearch->data == key ? ptrSearch:NULL);   
  146. }   
  147.   
  148. /* insert one elem into the Linklist according the given data and index */  
  149. template <class T>   
  150.  bool Linklist<T>::insert(int i, T key)   
  151. {   
  152.     LinklistNode<T> *ptrNewNode = new LinklistNode<T>;   
  153.     ptrNewNode->data = key;   
  154.     ptrNewNode->next = NULL;   
  155.   
  156.     LinklistNode<T> *ptrSearch = head;   
  157.     while (ptrSearch!=NULL && (--i)>0)   
  158.         ptrSearch = ptrSearch->next;   
  159.     if (i==0)   
  160.     {   
  161.         ptrNewNode->next = ptrSearch->next;   
  162.         ptrSearch->next = ptrNewNode;   
  163.         return true;   
  164.     }   
  165.     else  
  166.         return false;   
  167.   
  168. }      
  169.   
  170. /* delete the node whost index is 'i' */  
  171. template <class T>   
  172. bool  Linklist<T>::delet(int i, T& deletedData)   
  173. {   
  174.     LinklistNode<T> *ptrSearch = head;   
  175.   
  176.     while (ptrSearch->next != NULL && (--i)>0)   
  177.         ptrSearch = ptrSearch->next;   
  178.     if (ptrSearch->next != NULL)   
  179.     {   
  180.         LinklistNode<T> *deletedNode = ptrSearch->next;   
  181.         ptrSearch->next = deletedNode->next;   
  182.         deletedData = deletedNode->data;   
  183.         delete deletedNode;   
  184.         deletedNode = NULL;   
  185.   
  186.         return true;   
  187.     }   
  188.     else  
  189.         return false;   
  190. }   
  191.   
  192. /* count the length of the Linklist */  
  193. template <class T>   
  194. int Linklist<T>::length()   
  195. {   
  196.     int cnt = 0;   
  197.     LinklistNode<T> *ptrSearch = head->next;   
  198.   
  199.     while (ptrSearch!=NULL)   
  200.     {   
  201.         ++ cnt;   
  202.         ptrSearch = ptrSearch->next;   
  203.     }   
  204.     return cnt;   
  205. }   
  206.   
  207. /* print all the elements of the Linklist */  
  208. template <class T>   
  209. void Linklist<T>::print()   
  210. {   
  211.     LinklistNode<T> *ptrSearch = head->next;   
  212.   
  213.     std::cout <<" The elements: /n";   
  214.     while(ptrSearch != NULL)   
  215.     {   
  216.         std::cout <<ptrSearch->data <<" ";   
  217.         ptrSearch = ptrSearch->next;   
  218.     }   
  219.     std::cout <<std::endl;   
  220. }   
  221.   
Code:
  1. // main.cpp   
  2.   
  3. #include "linklist.h"   
  4.   
  5. int main()   
  6. {   
  7.     Linklist<int> linklist;   
  8.     linklist.create(HEAD);   
  9.     linklist.print();   
  10.   
  11.     linklist.create(TAIL);   
  12.     linklist.print();   
  13.   
  14.     linklist.insert(1, 100);   
  15.     linklist.print();   
  16.   
  17.     int d;   
  18.     linklist.delet(2, d);   
  19.     linklist.print();   
  20.        
  21.     std::cout <<"the location of the third element:/n";   
  22.     std::cout <<linklist.get(3);   
  23.     std::cout <<std::endl;   
  24.   
  25.     std::cout <<"the location of the element 100:/n";   
  26.     std::cout <<linklist.locate(100);   
  27.     std::cout <<std::endl;   
  28.   
  29.     std::cout <<"the location of the element 22:/n";   
  30.     std::cout <<linklist.locate(22);   
  31.     std::cout <<std::endl;   
  32.   
  33.     return 0;   
  34. }   

----------------------------------------------------------------------------------------------------------------

    把例子程序跑起来没想到断断续续的花了3天时间。26号,第一天,遇到了一些bug,后来发现主要是由于对类模版的一些用法不是很熟悉,还有就是一些错字等。28号,因为熟悉linux,所以有时候会有一些疑问。比如,linux的目录结构,vim设置命令,如何可以通过设置使得能够很好的查看源代码(这个功能还没有弄懂!)等等。

原创粉丝点击