c语言中链表的实现

来源:互联网 发布:行车记录仪支架淘宝 编辑:程序博客网 时间:2024/05/17 17:38

linklist.h

[cpp] view plain copy
  1. <span style="font-size:18px;">#ifndef __LINKLIST_H__  
  2. #define __LINKLIST_H__  
  3. #include<stdio.h>  
  4. #include<stdlib.h>  
  5. #include<assert.h>  
  6. typedef int DataType;  
  7. typedef struct LinkNode  
  8. {  
  9. DataType data;  
  10. struct LinkNode* next;  
  11. }LinkNode,*pLinkNode;  
  12. typedef struct LinkList  
  13. {  
  14. LinkNode* pHead;  
  15. }LinkList,*pLinkList;  
  16. void InitLinkList(pLinkList list);    
  17. void DestoryList(pLinkList list);    
  18. void PushBack(pLinkList list , DataType x);    
  19. void PopBack(pLinkList list);    
  20. void PushFront(pLinkList list , DataType x);    
  21. void PopFront(pLinkList list);    
  22. void PrintList(pLinkList list);    
  23. pLinkNode Find(pLinkList list,DataType x);    
  24. void Insert(pLinkList list, pLinkNode pos, DataType x);    
  25. void Remove(pLinkList list, DataType x);    
  26. void RemoveAll(pLinkList list, DataType x);    
  27. void Erase(pLinkList list,pLinkNode pos);    
  28. void BubbleSort(pLinkList list);    
  29. void SelectSort(pLinkList list);    
  30. void InsertSort(pLinkList list);    
  31. #endif    //__LINKLIST_H__</span>  
Linklist.c

[cpp] view plain copy
  1. <strong><span style="font-size:18px;">#include"linklist.h"    
  2.      
  3. void CreateNode(pLinkNode *newNode, DataType x)  //创建节点  
  4. {    
  5.          
  6.     *newNode = (pLinkNode)malloc(sizeof(LinkNode));    
  7.     if (NULL == *newNode)    
  8.     {    
  9.         printf("out of memory\n");    
  10.         exit(EXIT_FAILURE);    
  11.     }    
  12.     (*newNode)->data = x;    
  13.     (*newNode)->next = NULL;    
  14. }    
  15. void InitLinkList(pLinkList list)  //初始化  
  16. {    
  17. list->pHead = NULL;   
  18.     assert(list);     
  19. }    
  20.      
  21. void DestoryList(pLinkList list)    
  22. {    
  23.     assert(list);    
  24.     if (NULL==list->pHead)            //链表为空直接返回    
  25.     {    
  26.         return;    
  27.     }    
  28.     else    
  29.     {    
  30.         pLinkNode cur = list->pHead;     //cur指向第一个结点    
  31.         while (cur != NULL)             
  32.         {    
  33.             list->pHead = cur->next;    //pHead指向cur的下一个结点,当cur是最后一个结点时,pHead指向空    
  34.             free(cur);    
  35.             cur = list->pHead;          //cur指向当前第一个结点,当链表为空时,cur指向空    
  36.         }    
  37.     }    
  38.      
  39. }    
  40.      
  41. void PushBack(pLinkList list, DataType x)    
  42. {    
  43. pLinkNode newNode = NULL;  
  44.     assert(list);      
  45.     CreateNode(&newNode,x);    
  46.     if (NULL == (list->pHead))             //如果是空链表,直接插入头指针之后    
  47.     {    
  48.         list->pHead = newNode;    
  49.     }    
  50.     else    
  51.     {    
  52.         pLinkNode cur = list->pHead;    
  53.         while (NULL != (cur->next))            //找到最后一个结点cur    
  54.         {    
  55.             cur = cur->next;    
  56.         }    
  57.         cur->next = newNode;    
  58.     }    
  59. }    
  60.      
  61. void PopBack(pLinkList list)    
  62. {    
  63.     assert(list);    
  64.     if (NULL == list->pHead)    
  65.     {    
  66.         return;    
  67.     }    
  68.     else    
  69.     {    
  70.         pLinkNode cur = list->pHead;    
  71.         if (NULL == cur->next)                 //如果只有一个结点    
  72.         {    
  73.             free(cur);    
  74.             list->pHead= NULL;    
  75.         }    
  76.         else    
  77.         {    
  78.             while (NULL != cur->next->next)         //大于一个结点,先找到倒数第二个结点    
  79.             {    
  80.                 cur = cur->next;    
  81.             }    
  82.             free(cur->next);    
  83.             cur->next= NULL;    
  84.         }    
  85.     }    
  86. }    
  87.      
  88. void PushFront(pLinkList list, DataType x)            
  89. {    
  90. pLinkNode newNode = NULL;   
  91.     assert(list);    
  92.     CreateNode(&newNode, x);    
  93.     newNode->next =list->pHead;           //newNode的指针域先指向第一个结点    
  94.     list->pHead= newNode;                 //头指针指向newNode    
  95. }    
  96.      
  97. void PopFront(pLinkList list)    
  98. {   
  99. pLinkNode cur = list->pHead;             //cur指向第一个结点  
  100.     assert(list);    
  101.     if (NULL == list->pHead)              //空链表    
  102.     {    
  103.         return;    
  104.     }    
  105.     list->pHead = cur->next;             //指向第一个结点的指针域    
  106.     free(cur);    
  107.     cur = NULL;    
  108. }    
  109.      
  110. void PrintList(pLinkList list)    
  111. {    
  112. pLinkNode cur = list->pHead;  
  113.     assert(list);     
  114.     while (NULL != cur)    
  115.     {    
  116.         printf("%d->", cur->data);    
  117.         cur = cur->next;    
  118.     }    
  119.     printf("over\n");    
  120. }    
  121.      
  122. pLinkNode Find(pLinkList list, DataType x)    
  123. {    
  124. pLinkNode cur = list->pHead;  
  125.     assert(list);    
  126.     while (NULL != cur)    
  127.     {    
  128.         if (cur->data == x)    
  129.         {    
  130.             break;    
  131.         }    
  132.         cur = cur->next;    
  133.     }    
  134.     return cur;                  
  135. }    
  136.      
  137. void Insert(pLinkList list, pLinkNode pos, DataType x)  //在pos后面插入元素    
  138. {    
  139. pLinkNode cur = list->pHead;  
  140. pLinkNode newNode = NULL;  
  141.     assert(list);    
  142.     CreateNode(&newNode, x);    
  143.     while (NULL != cur)                                  //先找到这个位置    
  144.     {    
  145.         if (cur == pos)    
  146.         {    
  147.             break;    
  148.         }    
  149.         cur = cur->next;    
  150.     }    
  151.     if (NULL != cur)    
  152.     {    
  153.         newNode->next=cur->next;    
  154.         cur->next = newNode;    
  155.     }    
  156.     else    
  157.     {    
  158.         printf("没有这个结点\n");    
  159.     }    
  160.          
  161. }    
  162.      
  163. void Remove(pLinkList list, DataType x)    
  164. {    
  165. pLinkNode cur = list->pHead;    
  166.     pLinkNode p = list->pHead;   
  167.     assert(list);    
  168.     if (NULL == list->pHead)              //空链表直接返回    
  169.     {    
  170.         return;    
  171.     }    
  172.     if (NULL == cur->next)                 //如果只有一个结点    
  173.     {    
  174.         if (cur->data == x)    
  175.         {    
  176.             list->pHead = cur->next;    
  177.             free(cur);    
  178.             return;    
  179.         }    
  180.     }    
  181.     else    
  182.     {    
  183.         if (cur->data == x)                   //先判断第一个结点是不是要删除的结点    
  184.         {    
  185.             list->pHead = cur->next;    
  186.             free(cur);    
  187.             return;    
  188.         }    
  189.         cur = cur->next;    
  190.         while (NULL != cur)    
  191.         {    
  192.             if (cur->data == x)    
  193.             {    
  194.                 p->next = cur->next;   //p结点的指针域指向要删除结点的指针域    
  195.                 free(cur);    
  196.                 return;    
  197.             }    
  198.             p = cur;    
  199.             cur = cur->next;    
  200.         }    
  201.     }    
  202. }    
  203.      
  204. void RemoveAll(pLinkList list, DataType x)    
  205. {    
  206. pLinkNode cur = list->pHead;    
  207.     pLinkNode p = NULL;    
  208.     assert(list);    
  209.     if (NULL == list->pHead)    
  210.     {    
  211.         return;    
  212.     }    
  213.     while (NULL != cur)    
  214.     {    
  215.         if (NULL == list->pHead->next)          //如果要只有一个结点    
  216.         {    
  217.             if (cur->data == x)                 //如果是则删除    
  218.             {    
  219.                 list->pHead = cur->next;    
  220.                 free(cur);    
  221.                 return;    
  222.             }    
  223.         }    
  224.         else if (list->pHead->data == x)         //判断是不是第一个结点,是则删除,继续判断    
  225.         {    
  226.                 list->pHead = cur->next;    
  227.                 free(cur);    
  228.                 cur = list->pHead;    
  229.         }    
  230.         else    
  231.         {    
  232.             break;    
  233.         }    
  234.     }    
  235.     //要删除的结点在第一个结点之后    
  236.     cur = cur->next;    
  237.     p = list->pHead ;    
  238.     while (NULL != cur)    
  239.     {    
  240.         if (cur->data == x)    
  241.         {    
  242.             p->next = cur->next;           //p结点的指针域指向要删除结点的指针域    
  243.             free(cur);    
  244.             cur = p;    
  245.         }    
  246.         p = cur;    
  247.         cur = cur->next;    
  248.     }    
  249. }    
  250.      
  251. void Erase(pLinkList list, pLinkNode pos)   //删除pos后面的结点    
  252. {    
  253. pLinkNode cur = list->pHead;    
  254.     pLinkNode p = list->pHead;  
  255.     assert(list);     
  256.     if (NULL == cur)    
  257.     {    
  258.         return;    
  259.     }    
  260.     if (NULL == cur->next)                  //如果只有一个结点    
  261.     {    
  262.         if (cur == pos)    
  263.         {    
  264.             free(cur);    
  265.             list->pHead = NULL;    
  266.         }    
  267.     }    
  268.     else    
  269.     {    
  270.         if (cur == pos)                        //如果是第一个结点    
  271.         {    
  272.             list->pHead = cur->next;    
  273.             free(cur);    
  274.             return;    
  275.         }    
  276.         cur = cur->next;    
  277.         while (NULL != cur)    
  278.         {    
  279.             if (cur == pos)    
  280.             {    
  281.                 p->next = cur->next;    
  282.                 free(cur);    
  283.                 return;    
  284.             }    
  285.             p= cur;    
  286.             cur = cur->next;    
  287.         }    
  288.     }    
  289. }    
  290.      
  291. void BubbleSort(pLinkList list)    
  292. {    
  293. pLinkNode cur = list->pHead;    
  294.     pLinkNode p1= list->pHead;    
  295.     int flag = 0;    
  296.     DataType tmp=0;  
  297. pLinkNode p2 = list->pHead->next;      
  298.     assert(list);    
  299.     if (NULL == list->pHead)    
  300.     {    
  301.         return;    
  302.     }    
  303.     if (NULL == cur->next)    
  304.     {    
  305.         return;    
  306.     }    
  307.     cur = cur->next;    
  308.     while (NULL!=cur)    
  309.     {    
  310.         flag = 1;    
  311.         while (NULL != p2)    
  312.         {    
  313.             if (p1->data > p2->data)    
  314.             {    
  315.                 tmp = p1->data;    
  316.                 p1->data = p2->data;    
  317.                 p2->data = tmp;    
  318.                 flag = 0;    
  319.             }    
  320.             p2 = p2->next;    
  321.             p1 = p1->next;    
  322.         }    
  323.         if (flag)    
  324.         {    
  325.             break;    
  326.         }    
  327.         p1 = list->pHead;    
  328.         p2 = list->pHead->next;    
  329.         cur = cur->next;    
  330.     }    
  331. }</span></strong>  

test.c

[cpp] view plain copy
  1. <strong><span style="font-size:18px;">#include"linklist.h"    
  2. void Menu()    
  3. {    
  4.     printf("**********************************\n");    
  5.     printf("*0.Quit           1.InitLinkList *\n");    
  6.     printf("*2.PushBack       3.PopBack      *\n");    
  7.     printf("*4.PushFront      5.PopFront     *\n");    
  8.     printf("*6.PrintList      7.Find         *\n");    
  9.     printf("*8.Insert         9.Remove       *\n");    
  10.     printf("*10.RemoveAll     11.Erase       *\n");    
  11.     printf("*12.BubbleSort    \n");    
  12.     printf("**********************************\n\n");    
  13.     printf("请选择: ");    
  14. }    
  15.      
  16. void test()    
  17. {    
  18.     LinkList list;    
  19.     DataType x = 0;    
  20.     pLinkNode pos = NULL;    
  21.     int n = -1;    
  22.     while (1)    
  23.     {    
  24.         Menu();    
  25.         scanf("%d", &n);    
  26.         switch (n)    
  27.         {    
  28.         case 0:    
  29.             DestoryList(&list);    
  30.             exit(1);    
  31.             break;    
  32.         case 1:    
  33.             InitLinkList(&list);    
  34.             break;    
  35.         case 2:    
  36.             printf("请输入:");    
  37.             scanf("%d",&x);    
  38.             PushBack(&list,x);    
  39.             break;    
  40.         case 3:    
  41.             PopBack(&list);    
  42.             break;    
  43.         case 4:    
  44.             printf("请输入:");    
  45.             scanf("%d", &x);    
  46.             PushFront(&list,x);    
  47.             break;    
  48.         case 5:    
  49.             PopFront(&list);    
  50.             break;    
  51.         case 6:    
  52.             PrintList(&list);    
  53.             break;    
  54.         case 7:    
  55.             printf("请输入:");    
  56.             scanf("%d", &x);    
  57.             pos=Find(&list,x);    
  58.             printf("查找成功\n");    
  59.             break;    
  60.         case 8:    
  61.             printf("请输入元素:");    
  62.             scanf("%d", &x);    
  63.             Insert(&list,pos,x);    
  64.             break;    
  65.         case 9:    
  66.             printf("请输入:");    
  67.             scanf("%d", &x);    
  68.             Remove(&list,x);    
  69.             break;    
  70.         case 10:    
  71.             printf("请输入:");    
  72.             scanf("%d", &x);    
  73.             RemoveAll(&list,x);    
  74.             break;    
  75.         case 11:    
  76.             Erase(&list,pos);    
  77.             break;    
  78.         case 12:    
  79.             BubbleSort(&list);    
  80.             break;    
  81.         default:    
  82.             printf("选择无效,请重新选择\n");    
  83.             break;    
  84.         }    
  85.     }    
  86. }    
  87.      
  88. int main()    
  89. {    
  90.     test();    
  91.     system("pause");    
  92.     return 0;    
  93. }</span></strong>  
原创粉丝点击