线性表的链式存储结构

        之前实现了线性表的顺序存储方式发现顺序存储方式是存在缺陷的,就是插入和删除时需要移动大量的元素,显而易见这种顺序存储的方式需要耗费大量的时间,那仫我们有没有办法解决它呢?这就是我今天要实现的线性表的链式存储结构...

       我们知道在顺序表中它的物理存储位置是连续的所以当我们以顺序表的形式实现线性表的插入和删除时我们需要移动大量的元素; 假设线性表中的每个数据存储的位置存在间隙我们是不是就可以不用移动大量的元素了呢?此时我们只需要让这个元素知道它的下一个元素的位置就可以了. 这就是链式表的存储思路.

      要理解链式表首先我们需要理解头指针和头结点 

      头指针:我们通常把链表中第一个节点的存储位置叫头指针, 整个链表的存取就是从头指针开始的.

      头结点: 通常我们在单链表的第一个节点前附设一个结点叫头结点, 头结点的数据域可以不存储任何信息指针域用来存储指向第一个节点的指针.

      

        因此我们实现链式表是可以有两种方式:一种是通过头结点的方式访问后续链表,一种是通过头指针的方式访问后续链表在这里我只实现用头指针的方式.下面就让我们来看链式表的存储结构

        

[cpp]  view plain  copy
 print ?
  1. typedef int DataType;  
  2.   
  3. typedef struct LinkNode  
  4. {  
  5.     DataType data;  
  6.     struct LinkNode *next;  
  7. }LinkNode,*pLinkNode;      
  8.   
  9. typedef struct LinkList  
  10. {  
  11.     LinkNode *phead;  
  12. }LinkList,*pLinkList;   
    

          从链式表的存储结构可知链表中的一个结点包括数据域和指针域,而定义结构LinkList则是可以在函数传参时不需要再添加指针的标志符号'*'而已本身并没有什仫实际的意义。

         好了说了这仫多也该进入我们的正题了-实现线性表的链式存储方式,先让我们来看看这个链式结构有什仫功能吧!

         

[cpp]  view plain  copy
 print ?
  1. void Init_LinkList(pLinkList plist);          //初始化  
  2. void Free_LinkList(pLinkList plist);          //释放空间  
  3. void Push_back(pLinkList plist,DataType x);   //尾插  
  4. void Pop_back(pLinkList plist);               //尾删  
  5. void Push_Front(pLinkList plist,DataType x);  //头插  
  6. void Pop_Front(pLinkList plist);              //头删  
  7. void Print_LinkList(pLinkList plist);         //打印结点  
  8. pLinkNode Find_NUM(pLinkList plist,DataType x); //找结点  
  9. void Insert_Back(pLinkList plist,pLinkNode pos,DataType x);   //指定位置pos后插入  
  10. void Insert_Front(pLinkList plist,pLinkNode pos,DataType x);  //指定位置pos前插入  
  11. void Remove_LinkList(pLinkList plist,DataType x);             //删除指定元素  
  12. void RemoveAll_LinkList(pLinkList plist,DataType x);          //删除所有指定元素  
  13. void Bubble_Sort(pLinkList plist);                            //对链表排序  
  14. void Erase_LinkList(pLinkList plist,pLinkNode pos);  

        <尾插和尾删>

         @尾插

         此时我们需要考虑两种情况的链式表

         (1).链表为空,此时只需要将新开辟的结点赋给头指针所指向的下一个结点就可以了;

         (2).存在元素,此时我们需要找到最后一个元素并把最后一个元素的指针域修改为指向新开辟的结点就可以了;

        @尾删

          此时我们需要考虑的有三种情况

          (1).链表为空,直接返回;

          (2).链表只有一个结点,只需要free第一个节点就可以了;

          (3).链表存在不止一个结点,在这里提供两种思路一种是:在找最后一个结点的时候将它的前一个结点也保存下来,因为我们需要将倒数第二个结点的指针域置为空;另一种思路就是不需要记录它的前一个结点,我们只需要让cur指针找到链表的倒数第二个结点就可以了,我们可以通过倒数第二个结点的指针域找到最后一个结点;

          尾插和尾删的代码实现如下:

          

[cpp]  view plain  copy
 print ?
  1. void Push_back(pLinkList plist,DataType x)    //尾插  
  2. {  
  3.     //1.是空链表 2.有结点  
  4.     pLinkNode cur=NULL;  
  5.     pLinkNode newNode=NULL;  
  6.     assert(plist);  
  7.     cur=plist->phead;  
  8.     newNode=Create_Node(x);  
  9.     if(NULL == cur)           //是空链表  
  10.     {  
  11.         plist->phead=newNode;  
  12.     }  
  13.     else                      //有结点  
  14.     {  
  15.         while(cur->next != NULL)    //找到最后一个结点  
  16.         {  
  17.             cur=cur->next;  
  18.         }  
  19.         cur->next=newNode;  
  20.     }  
  21. }  
  22.   
  23. void Pop_back(pLinkList plist)          //尾删  
  24. {  
  25.     //1.空链表 2.只有一个结点 3.大于1个结点  
  26.     pLinkNode tmp=NULL;  
  27.     pLinkNode cur=plist->phead;  
  28.     assert(plist);  
  29.     if(NULL == plist->phead)         //空链表  
  30.     {     
  31.         return ;  
  32.     }  
  33.     else if(NULL == plist->phead->next)   //只有一个结点  
  34.     {  
  35.         free(plist->phead);  
  36.         plist->phead=NULL;  
  37.     }  
  38.     else  
  39.     {  
  40.         while(cur->next != NULL)         //方法二:存储最后一个结点的前一个结点  
  41.         {  
  42.             tmp=cur;  
  43.             cur=cur->next;  
  44.         }  
  45.         free(cur);  
  46.         cur=NULL;  
  47.         tmp->next=NULL;  
  48.         //while(cur->next->next != NULL)    方法一  
  49.         //{  
  50.         //  cur=cur->next;  
  51.         //}  
  52.         //free(cur->next);  
  53.         //cur->next=NULL;  
  54.     }  
  55. }  

          

       <头插和头删>

       @头插

       @头删

       这两个功能都是需要考虑空链表和有结点的情况,但是在头插中我们可以先将新开辟结点的指针域指向原链表的第一个元素也就是首元结点,再将头指针指向新开辟的结点就可以简化代码了;因为当是空链表时上述解决思路可以将空指针赋给新开辟的结点满足题意,头插和头删的实现代码如下:

      

[cpp]  view plain  copy
 print ?
  1. void Push_Front(pLinkList plist,DataType x)  //头插  
  2. {  
  3.     //1.空链表 2.有结点  
  4.     pLinkNode newNode=NULL;  
  5.     assert(plist);  
  6.     newNode=Create_Node(x);  
  7.     newNode->next=plist->phead;  
  8.     plist->phead=newNode;  
  9. }  
  10.   
  11. void Pop_Front(pLinkList plist)              //头删  
  12. {  
  13.     //1.空链表 2.有元素的结点  
  14.     pLinkNode tmp=NULL;  
  15.     if(NULL == plist->phead)         //空链表  
  16.     {     
  17.         return ;  
  18.     }  
  19.     else           //有元素的结点  
  20.     {  
  21.         tmp=plist->phead;  
  22.         plist->phead=tmp->next;  
  23.         free(tmp);  
  24.         tmp=NULL;  
  25.     }  
  26. }  

       

       

       <指定位置pos前插入和指定位置后插入>

       这两种情况都需要考虑两种情况,空链表则直接调用头插函数,如果有结点且在指定位置后插入我们需要记录该指定位置然后利用该位置的指针域访问下一个结点修改其指针指向关系;如果在指定位置前插入我们可以不需要找到该pos结点只需要找到该pos结点的前一个结点,可以通过前一个结点访问该pos结点并修改其指针指向关系;

       @指定位置pos前插入

       @指定位置后插入

       

[cpp]  view plain  copy
 print ?
  1. void Insert_Back(pLinkList plist,pLinkNode pos,DataType x)  //指定位置pos后插入  
  2. {  
  3.     //1.空链表 2.有结点的链表  
  4.     pLinkNode newNode=NULL;  
  5.     pLinkNode cur=plist->phead;  
  6.     assert(plist);  
  7.     newNode=Create_Node(x);  
  8.     if(NULL == plist->phead->next)   //空链表直接头插  
  9.     {  
  10.         Push_Front(plist,x);  
  11.     }  
  12.     else            //有结点的链表  
  13.     {  
  14.         while(cur != pos)  
  15.         {  
  16.             cur=cur->next;  
  17.         }  
  18.         newNode->next=cur->next;  
  19.         cur->next=newNode;  
  20.     }  
  21. }  
  22.   
  23. void Insert_Front(pLinkList plist,pLinkNode pos,DataType x)  //指定位置pos前插入  
  24. {  
  25.     pLinkNode newNode=NULL;  
  26.     pLinkNode cur=plist->phead;  
  27.     assert(plist);  
  28.     newNode=Create_Node(x);  
  29.     if(NULL == plist->phead->next)   //空链表直接头插  
  30.     {  
  31.         Push_Front(plist,x);  
  32.     }  
  33.     else  
  34.     {  
  35.         while(cur->next != pos)  
  36.         {  
  37.             cur=cur->next;  
  38.         }  
  39.         newNode->next=cur->next;  
  40.         cur->next=newNode;  
  41.     }  
  42. }  

        

        在这里原链表有三个结点,他们的数据域分别为1,2,3,通过上述两个函数将结点数据域为5的结点插入到数据域为2的结点的前面和后面,结果如上图.

        <删除指定元素和删除所有指定元素>

        @删除指定元素

        @删除所有指定元素

        两个函数的实现方法基本类似,在删除指定元素时我们只删除了第一个找到的指定元素的结点,在这里我们需要将该删除结点的前一个结点保存下来,之后将它的指针域指向该删除结点的下一个结点;在删除所有指定元素时我们可以仿照删除指定元素的方法先将第一个找到的指定元素删除再从该位置往后继续查找指定元素的结点找到并删除;

       

[cpp]  view plain  copy
 print ?
  1. void Remove_LinkList(pLinkList plist,DataType x)   //删除指定元素  
  2. {  
  3.     pLinkNode ret=NULL;  
  4.     pLinkNode cur=plist->phead;  
  5.     assert(plist);  
  6.     if(NULL == plist->phead)     //空链表  
  7.     {  
  8.         return ;  
  9.     }  
  10.     else  
  11.     {  
  12.         ret=Find_NUM(plist,x);  
  13.         while(cur->next != ret)  //cur保存的是要删除结点的前一个结点  
  14.         {  
  15.             cur=cur->next;  
  16.         }  
  17.         cur->next=ret->next;  
  18.         free(ret);  
  19.         ret=NULL;  
  20.     }  
  21. }  
  22.   
  23. void RemoveAll_LinkList(pLinkList plist,DataType x)//删除所有指定元素  
  24. {  
  25.     pLinkNode ret=NULL;  
  26.     pLinkNode cur=plist->phead;  
  27.     assert(plist);  
  28.     if(NULL == plist->phead)  
  29.     {  
  30.         return ;  
  31.     }  
  32.     else  
  33.     {  
  34.         while(cur->next != NULL)  
  35.         {  
  36.             ret=Find_NUM(plist,x);  
  37.             while(cur->next != ret)  
  38.             {  
  39.                 cur=cur++;  
  40.             }  
  41.             cur->next=ret->next;  
  42.             free(ret);  
  43.             ret=NULL;  
  44.             cur=cur->next;  
  45.         }  
  46.     }  
  47. }  

         

        其他的函数就不在这里一一分析了,程序源代码如下:

        text.c

       

[cpp]  view plain  copy
 print ?
  1. #include"LinkList.h"  
  2.   
  3. void text1()    
  4. {  
  5.     LinkList plist;  
  6.     Init_LinkList(&plist);  
  7.     Push_back(&plist,1);  
  8.     Push_back(&plist,2);  
  9.     Push_back(&plist,3);  
  10.     Push_back(&plist,4);  
  11.     Push_back(&plist,5);  
  12.     Print_LinkList(&plist);   //1->2->3->4->5->over  
  13.     Pop_back(&plist);  
  14.     Print_LinkList(&plist);   //1->2->3->4->over  
  15.     Pop_back(&plist);  
  16.     Print_LinkList(&plist);   //1->2->3->over  
  17.     Pop_back(&plist);  
  18.     Print_LinkList(&plist);   //1->2->over  
  19.     Pop_back(&plist);  
  20.     Print_LinkList(&plist);   //1->over  
  21.     Pop_back(&plist);  
  22.     Print_LinkList(&plist);    //over  
  23.     Free_LinkList(&plist);  
  24. }  
  25.   
  26. void text2()  
  27. {  
  28.     LinkList plist;  
  29.     Init_LinkList(&plist);  
  30.     Push_Front(&plist,1);  
  31.     Push_Front(&plist,2);  
  32.     Push_Front(&plist,3);  
  33.     Push_Front(&plist,4);  
  34.     Push_Front(&plist,5);  
  35.     Print_LinkList(&plist);   //5->4->3->2->1->over  
  36.     Pop_Front(&plist);  
  37.     Print_LinkList(&plist);   //4->3->2->1->over  
  38.     Pop_Front(&plist);  
  39.     Print_LinkList(&plist);   //3->2->1->over  
  40.     Pop_Front(&plist);  
  41.     Print_LinkList(&plist);   //2->1->over  
  42.     Pop_Front(&plist);  
  43.     Print_LinkList(&plist);   //1->over  
  44.     Pop_Front(&plist);  
  45.     Print_LinkList(&plist);   //over  
  46.     Free_LinkList(&plist);  
  47. }  
  48.   
  49. void text3()  
  50. {  
  51.     LinkList plist;  
  52.     Init_LinkList(&plist);  
  53.     Push_back(&plist,1);  
  54.     Push_back(&plist,2);  
  55.     Push_back(&plist,3);  
  56.     Print_LinkList(&plist);   //1->2->3->over  
  57.     Insert_Back(&plist,Find_NUM(&plist,2),5);  
  58.     Print_LinkList(&plist);   //1->2->5->3->over  
  59.     Insert_Front(&plist,Find_NUM(&plist,2),5);  
  60.     Print_LinkList(&plist);  //1->5->2->5->3->over  
  61.     Remove_LinkList(&plist,5);  
  62.     Print_LinkList(&plist);  //1->5->5->3->over  
  63.     Free_LinkList(&plist);  
  64. }  
  65.   
  66. void text4()  
  67. {  
  68.     LinkList plist;  
  69.     Init_LinkList(&plist);  
  70.     Push_back(&plist,1);  
  71.     Push_back(&plist,2);  
  72.     Push_back(&plist,3);  
  73.     Print_LinkList(&plist);   //1->2->3->over  
  74.     Insert_Back(&plist,Find_NUM(&plist,2),5);  
  75.     Insert_Front(&plist,Find_NUM(&plist,2),5);  
  76.     Print_LinkList(&plist);   //1->5->2->5->3->over  
  77.     RemoveAll_LinkList(&plist,5);  
  78.     Print_LinkList(&plist);  //1->2->3->over  
  79.     Push_back(&plist,6);  
  80.     Push_back(&plist,5);  
  81.     Bubble_Sort(&plist);  
  82.     Print_LinkList(&plist);  //1->2->3->5->6->over   
  83.     Erase_LinkList(&plist,Find_NUM(&plist,3));   
  84.     Print_LinkList(&plist);    //1->2->5->6->over   
  85.     Free_LinkList(&plist);  
  86. }  
  87. int main()  
  88. {  
  89.     //text1();  
  90.     //text2();  
  91.     //text3();  
  92.     text4();  
  93.     system("pause");  
  94.     return 0;  
  95. }  

         LinkList.h

        

[cpp]  view plain  copy
 print ?
  1. #define _CRT_SECURE_NO_WARNINGS  
  2.   
  3. #ifndef __LINKLIST_H__  
  4. #define __LINKLIST_H__  
  5.   
  6. #include<stdio.h>  
  7. #include<stdlib.h>  
  8. #include<assert.h>  
  9.   
  10. typedef int DataType;  
  11.   
  12. typedef struct LinkNode  
  13. {  
  14.     DataType data;  
  15.     struct LinkNode *next;  
  16. }LinkNode,*pLinkNode;      
  17.   
  18. typedef struct LinkList  
  19. {  
  20.     LinkNode *phead;  
  21. }LinkList,*pLinkList;      
  22.   
  23. void Init_LinkList(pLinkList plist);          //初始化  
  24. void Free_LinkList(pLinkList plist);          //释放空间  
  25. void Push_back(pLinkList plist,DataType x);   //尾插  
  26. void Pop_back(pLinkList plist);               //尾删  
  27. void Push_Front(pLinkList plist,DataType x);  //头插  
  28. void Pop_Front(pLinkList plist);              //头删  
  29. void Print_LinkList(pLinkList plist);         //打印结点  
  30. pLinkNode Find_NUM(pLinkList plist,DataType x); //找结点  
  31. void Insert_Back(pLinkList plist,pLinkNode pos,DataType x);   //指定位置pos后插入  
  32. void Insert_Front(pLinkList plist,pLinkNode pos,DataType x);  //指定位置pos前插入  
  33. void Remove_LinkList(pLinkList plist,DataType x);             //删除指定元素  
  34. void RemoveAll_LinkList(pLinkList plist,DataType x);          //删除所有指定元素  
  35. void Bubble_Sort(pLinkList plist);                            //对链表排序  
  36. void Erase_LinkList(pLinkList plist,pLinkNode pos);  
  37.   
  38. #endif __LINKLIST_H__  

       LinkList.c

       

[cpp]  view plain  copy
 print ?
  1. #include"LinkList.h"  
  2.   
  3. void Init_LinkList(pLinkList plist)       //初始化  
  4. {  
  5.     assert(plist);  
  6.     plist->phead=NULL;  
  7. }  
  8.   
  9. pLinkNode Create_Node(DataType x)     //创建新结点  
  10. {  
  11.     pLinkNode newNode=(pLinkNode)malloc(sizeof(LinkNode));  
  12.     if(NULL == newNode)  
  13.     {  
  14.         printf("out of memory\n");  
  15.         exit(EXIT_FAILURE);  
  16.     }  
  17.     newNode->data=x;  
  18.     newNode->next=NULL;  
  19.     return newNode;  
  20. }  
  21.   
  22. void Free_LinkList(pLinkList plist)      //释放空间  
  23. {  
  24.     pLinkNode cur=plist->phead;  
  25.     assert(plist);  
  26.     while(cur != NULL)  
  27.     {  
  28.         pLinkNode tmp=cur;  
  29.         cur=cur->next;  
  30.         free(tmp);  
  31.         tmp=NULL;         
  32.     }  
  33.     plist->phead=NULL;  
  34. }  
  35.   
  36. void Push_back(pLinkList plist,DataType x)    //尾插  
  37. {  
  38.     //1.是空链表 2.有结点  
  39.     pLinkNode cur=NULL;  
  40.     pLinkNode newNode=NULL;  
  41.     assert(plist);  
  42.     cur=plist->phead;  
  43.     newNode=Create_Node(x);  
  44.     if(NULL == cur)           //是空链表  
  45.     {  
  46.         plist->phead=newNode;  
  47.     }  
  48.     else                      //有结点  
  49.     {  
  50.         while(cur->next != NULL)    //找到最后一个结点  
  51.         {  
  52.             cur=cur->next;  
  53.         }  
  54.         cur->next=newNode;  
  55.     }  
  56. }  
  57.   
  58. void Pop_back(pLinkList plist)          //尾删  
  59. {  
  60.     //1.空链表 2.只有一个结点 3.大于1个结点  
  61.     pLinkNode tmp=NULL;  
  62.     pLinkNode cur=plist->phead;  
  63.     assert(plist);  
  64.     if(NULL == plist->phead)         //空链表  
  65.     {     
  66.         return ;  
  67.     }  
  68.     else if(NULL == plist->phead->next)   //只有一个结点  
  69.     {  
  70.         free(plist->phead);  
  71.         plist->phead=NULL;  
  72.     }  
  73.     else  
  74.     {  
  75.         while(cur->next != NULL)         //方法二:存储最后一个结点的前一个结点  
  76.         {  
  77.             tmp=cur;  
  78.             cur=cur->next;  
  79.         }  
  80.         free(cur);  
  81.         cur=NULL;  
  82.         tmp->next=NULL;  
  83.         //while(cur->next->next != NULL)    方法一  
  84.         //{  
  85.         //  cur=cur->next;  
  86.         //}  
  87.         //free(cur->next);  
  88.         //cur->next=NULL;  
  89.     }  
  90. }  
  91.   
  92. void Push_Front(pLinkList plist,DataType x)  //头插  
  93. {  
  94.     //1.空链表 2.有结点  
  95.     pLinkNode newNode=NULL;  
  96.     assert(plist);  
  97.     newNode=Create_Node(x);  
  98.     newNode->next=plist->phead;  
  99.     plist->phead=newNode;  
  100. }  
  101.   
  102. void Pop_Front(pLinkList plist)              //头删  
  103. {  
  104.     //1.空链表 2.有元素的结点  
  105.     pLinkNode tmp=NULL;  
  106.     if(NULL == plist->phead)         //空链表  
  107.     {     
  108.         return ;  
  109.     }  
  110.     else           //有元素的结点  
  111.     {  
  112.         tmp=plist->phead;  
  113.         plist->phead=tmp->next;  
  114.         free(tmp);  
  115.         tmp=NULL;  
  116.     }  
  117. }  
  118.   
  119. void Print_LinkList(pLinkList plist)    //打印结点  
  120. {  
  121.     pLinkNode cur=plist->phead;  
  122.     while(cur != NULL)  
  123.     {  
  124.         printf("%d->",cur->data);  
  125.         cur=cur->next;  
  126.     }  
  127.     printf("over\n");  
  128. }  
  129.   
  130. pLinkNode Find_NUM(pLinkList plist,DataType x) //找结点  
  131. {  
  132.     pLinkNode cur=plist->phead;  
  133.     assert(plist);  
  134.     while(cur != NULL)  
  135.     {  
  136.         if(cur->data == x)  
  137.         {  
  138.             return cur;  
  139.         }  
  140.         cur=cur->next;  
  141.     }  
  142.     return NULL;  
  143. }  
  144.   
  145. void Insert_Back(pLinkList plist,pLinkNode pos,DataType x)  //指定位置pos后插入  
  146. {  
  147.     //1.空链表 2.有结点的链表  
  148.     pLinkNode newNode=NULL;  
  149.     pLinkNode cur=plist->phead;  
  150.     assert(plist);  
  151.     newNode=Create_Node(x);  
  152.     if(NULL == plist->phead->next)   //空链表直接头插  
  153.     {  
  154.         Push_Front(plist,x);  
  155.     }  
  156.     else            //有结点的链表  
  157.     {  
  158.         while(cur != pos)  
  159.         {  
  160.             cur=cur->next;  
  161.         }  
  162.         newNode->next=cur->next;  
  163.         cur->next=newNode;  
  164.     }  
  165. }  
  166.   
  167. void Insert_Front(pLinkList plist,pLinkNode pos,DataType x)  //指定位置pos前插入  
  168. {  
  169.     pLinkNode newNode=NULL;  
  170.     pLinkNode cur=plist->phead;  
  171.     assert(plist);  
  172.     newNode=Create_Node(x);  
  173.     if(NULL == plist->phead->next)   //空链表直接头插  
  174.     {  
  175.         Push_Front(plist,x);  
  176.     }  
  177.     else  
  178.     {  
  179.         while(cur->next != pos)  
  180.         {  
  181.             cur=cur->next;  
  182.         }  
  183.         newNode->next=cur->next;  
  184.         cur->next=newNode;  
  185.     }  
  186. }  
  187.   
  188. void Remove_LinkList(pLinkList plist,DataType x)   //删除指定元素  
  189. {  
  190.     pLinkNode ret=NULL;  
  191.     pLinkNode cur=plist->phead;  
  192.     assert(plist);  
  193.     if(NULL == plist->phead)     //空链表  
  194.     {  
  195.         return ;  
  196.     }  
  197.     else  
  198.     {  
  199.         ret=Find_NUM(plist,x);  
  200.         while(cur->next != ret)  //cur保存的是要删除结点的前一个结点  
  201.         {  
  202.             cur=cur->next;  
  203.         }  
  204.         cur->next=ret->next;  
  205.         free(ret);  
  206.         ret=NULL;  
  207.     }  
  208. }  
  209.   
  210. void RemoveAll_LinkList(pLinkList plist,DataType x)//删除所有指定元素  
  211. {  
  212.     pLinkNode ret=NULL;  
  213.     pLinkNode cur=plist->phead;  
  214.     assert(plist);  
  215.     if(NULL == plist->phead)  
  216.     {  
  217.         return ;  
  218.     }  
  219.     else  
  220.     {  
  221.         while(cur->next != NULL)  
  222.         {  
  223.             ret=Find_NUM(plist,x);  
  224.             while(cur->next != ret)  
  225.             {  
  226.                 cur=cur++;  
  227.             }  
  228.             cur->next=ret->next;  
  229.             free(ret);  
  230.             ret=NULL;  
  231.             cur=cur->next;  
  232.         }  
  233.     }  
  234. }  
  235.   
  236. void Bubble_Sort(pLinkList plist)       //对链表排序  
  237. {  
  238.     pLinkNode cur=plist->phead;  
  239.     pLinkNode cmp=plist->phead;  
  240.     DataType flag=0;  
  241.     while(cur->next != NULL)  
  242.     {  
  243.         flag=0;  
  244.         while(cmp->next != NULL)  
  245.         {  
  246.             if(cmp->data > cmp->next->data)   //升序排列  
  247.             {  
  248.                 DataType tmp=cmp->data;  
  249.                 cmp->data=cmp->next->data;  
  250.                 cmp->next->data=tmp;  
  251.                 flag=1;  
  252.             }  
  253.             cmp=cmp->next;  
  254.         }  
  255.         cur=cur->next;  
  256.         if(flag == 0)  
  257.             break;  
  258.     }  
  259. }  
  260.   
  261. void Erase_LinkList(pLinkList plist,pLinkNode pos)  
  262. {  
  263.     pLinkNode cur=plist->phead;                                              
  264.     assert(plist);  
  265.     while(cur->next != pos)  
  266.     {  
  267.         cur=cur->next;  
  268.     }  
  269.     cur->next=pos->next;  
  270.     free(pos);  
  271.     pos=NULL;  
  272. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值