Chinaunix首页 | 论坛 | 博客
  • 博客访问: 299672
  • 博文数量: 76
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 715
  • 用 户 组: 普通用户
  • 注册时间: 2015-05-20 20:38
文章分类
文章存档

2016年(20)

2015年(56)

分类: 嵌入式

2016-02-28 20:09:42



  1. 数据结构---线性表的链式表示和实现(二)
  2. 16年2月27日22:02:02
  3. 这一篇是双向链表的一些操作函数,与单向链表相比,并没有太大的区别。
  4. /**
  5.  * This code is for duplex linked_list.
  6.  */

  7. #include <stdio.h>
  8. #include <malloc.h>
  9. #include <stdlib.h>

  10. typedef struct DulNode{
  11.     int data;
  12.     struct DulNode *pNext, *pPrior;
  13. }DULNODE, *PDULNODE;

  14. /**
  15.  * Initialise the linked_list.
  16.  * @param pHead [the linked_list head]
  17.  * @return [return the address of the pHead]
  18.  */
  19. PDULNODE InitDulLink (PDULNODE pHead)
  20. {
  21.     pHead = (PDULNODE)malloc(sizeof(DULNODE));
  22.     if (!pHead)
  23.     {
  24.         printf("Cannot malloc memory for pHead.\n");
  25.         exit(-1);
  26.     }

  27.     pHead->pNext = pHead->pPrior = pHead;

  28.     return pHead;
  29. }

  30. /**
  31.  * Traverse the linked_list.
  32.  * @param pHead [the head of this linked_list]
  33.  */
  34. void TraverseDulLink(PDULNODE pHead)
  35. {
  36.     PDULNODE pTmp = pHead->pNext;
  37.     while (pTmp != pHead)
  38.     {
  39.         printf("%d ", pTmp->data);    
  40.         pTmp = pTmp->pNext;
  41.     }
  42.     printf("\n");
  43. }

  44. /**
  45.  * Backward traverse this linked_list.
  46.  * @param pHead [the head of this linked_list]
  47.  */
  48. void TraverseDulLinkBack(PDULNODE pHead)
  49. {
  50.     PDULNODE pTmp = pHead->pPrior;
  51.     while (pTmp != pHead)
  52.     {
  53.         printf("%d ", pTmp->data);
  54.         pTmp = pTmp->pPrior;
  55.     }
  56.     printf("\n");
  57. }

  58. /**
  59.  * Judge the linked_list is empty or not.
  60.  * @param pHead [the head of this linked_list]
  61.  * @return [if the linked_list is empty, return 1,else, return 0]
  62.  */
  63. int IsLinkEmpty(PDULNODE pHead)
  64. {
  65.     return (pHead->pNext == pHead && pHead->pPrior == pHead) ? 1 : 0;
  66. }

  67. /**
  68.  * The length of the linked_list.
  69.  * @param pHead [the head of this linked_list]
  70.  * @return [the length]
  71.  */
  72. int LinkLength(PDULNODE pHead)
  73. {
  74.     int i = 0;
  75.     PDULNODE pTmp = pHead->pNext;
  76.     
  77.     while (pTmp != pHead)
  78.     {
  79.         i++;
  80.         pTmp = pTmp->pNext;    
  81.     }

  82.     return i;
  83. }

  84. /**
  85.  * Get elem from the linked_list.
  86.  * @param pHead [the head of this linked_list]
  87.  * @param pos [position]
  88.  * @return [the PDULNODE]
  89.  */
  90. PDULNODE GetElem(PDULNODE pHead, int pos)
  91. {
  92.     int i;
  93.     PDULNODE pTmp = pHead;

  94.     if (pos < 0 || pos > LinkLength(pHead))
  95.     {
  96.         printf("Error position for GetElem.\n");    
  97.         exit(-1);
  98.     }

  99.     for (i = 1; i <= pos; i++)
  100.         pTmp = pTmp->pNext;

  101.     return pTmp;
  102. }

  103. /**
  104.  * Insert elem into the linked_list.
  105.  * @param pHead [the head of this linked_list]
  106.  * @param pos [position]
  107.  * @param val [the value of the insert elem]
  108.  * @return [if success, return 1]
  109.  */
  110. int InsertDulLink(PDULNODE pHead, int pos, int val)
  111. {
  112.     PDULNODE pNew, pTmp;

  113.     if (pos < 1 || pos > LinkLength(pHead) + 1)
  114.     {
  115.         printf("Error position for InsertDulLink!\n");
  116.         exit(-1);
  117.     }

  118.     pTmp = GetElem(pHead, pos -1);
  119.     if (!pTmp)
  120.     {
  121.         printf("InsertDulLink Error.\n");    
  122.         exit(-1);
  123.     }

  124.     pNew = (PDULNODE)malloc(sizeof(DULNODE));
  125.     if (!pNew)
  126.     {
  127.         printf("Cannot malloc memory for pNew.\n");    
  128.         exit(-1);
  129.     }

  130.     pNew->data = val;
  131.     pNew->pNext = pTmp->pNext;
  132.     pNew->pPrior = pTmp;

  133.     pTmp->pNext->pPrior = pNew;
  134.     pTmp->pNext = pNew;

  135.     return 1;
  136. }

  137. int DeleteDulLink(PDULNODE pHead, int pos, int *val)
  138. {
  139.     PDULNODE pTmp;
  140.     int i = LinkLength(pHead);
  141.     printf("The link length is %d.\n", i);

  142.     if (pos < 1 || pos > LinkLength(pHead) + 1)
  143.     {
  144.         printf("Error position for DeleteDulLink.\n");    
  145.         exit(-1);
  146.     }

  147.     pTmp = GetElem(pHead, pos);
  148.     if (!pTmp)
  149.     {
  150.         printf("Cannot Get elem for DeleteDulLink.\n");
  151.         exit(-1);    
  152.     }

  153.     *val = pTmp->data;
  154.     pTmp->pPrior->pNext = pTmp->pNext;
  155.     pTmp->pNext->pPrior = pTmp->pPrior;
  156.     free(pTmp);

  157.     return 1;
  158. }

  159. int main(int argc, char const *argv[])
  160. {
  161.     PDULNODE pHead;
  162.     int val;

  163.     pHead = InitDulLink(pHead);
  164.     if (IsLinkEmpty(pHead))
  165.     {
  166.         printf("The link is empty.\n");    
  167.     }
  168.     InsertDulLink(pHead, 1, 1);    
  169.     InsertDulLink(pHead, 1, 2);    
  170.     InsertDulLink(pHead, 1, 3);    
  171.     InsertDulLink(pHead, 1, 4);    
  172.     InsertDulLink(pHead, 1, 5);    
  173.     if (IsLinkEmpty(pHead))
  174.     {
  175.         printf("The link is empty.\n");    
  176.     }
  177.     TraverseDulLink(pHead);
  178.     //TraverseDulLinkBack(pHead);
  179.     if (DeleteDulLink(pHead, 2, &val))
  180.     {
  181.         printf("The DeleteDulLink value is :%d. \n", val);    
  182.     }
  183.     TraverseDulLink(pHead);

  184.     return 0;
  185. }

  186. 程序的运行结果如下所示:
  187. The link is empty.
  188. 5 4 3 2 1
  189. The link length is 5.
  190. The DeleteDulLink value is :4.
  191. 5 3 2 1

阅读(1585) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~