Chinaunix首页 | 论坛 | 博客
  • 博客访问: 488263
  • 博文数量: 111
  • 博客积分: 3146
  • 博客等级: 中校
  • 技术积分: 939
  • 用 户 组: 普通用户
  • 注册时间: 2009-07-07 11:23
个人简介

Nathing

文章分类

全部博文(111)

文章存档

2016年(2)

2015年(1)

2014年(31)

2012年(2)

2011年(9)

2010年(36)

2009年(30)

我的朋友

分类: C/C++

2014-04-21 23:23:06


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <malloc.h>

  4. typedef struct Node
  5. {
  6.     int data;
  7.     struct Node * pNext;
  8. }NODE, *PNODE;

  9. PNODE create_Node();
  10. void traversal(PNODE p);
  11. bool is_empty(PNODE);
  12. int length(PNODE);
  13. void sort(PNODE);
  14. bool insert(PNODE pHead, int pos, int val);
  15. bool delete_list(PNODE pHead, int pos, int *pVal);

  16. int main(int argc, char * argv)
  17. {
  18.     PNODE p = create_Node();

  19.     traversal(p);

  20.     if (is_empty(p))
  21.         printf("empty\n");
  22.     else
  23.         printf("not empty\n");

  24.     printf("链表的长度为:%d\n", length(p));

  25.     sort(p);

  26.     traversal(p);

  27.     insert(p, 2, 9);
  28.     printf("插入后:\n");

  29.     traversal(p);

  30.     int d_val;
  31.     delete_list(p, 1, &d_val);
  32.     printf("删除后:\n");
  33.     traversal(p);
  34.     free(p);
  35.     return 0;
  36. }

  37. PNODE create_Node()
  38. {
  39.     PNODE pHead = (PNODE)malloc(sizeof(NODE));
  40.     if (NULL == pHead)
  41.     {
  42.         printf("分配失败,程序终止!");
  43.         exit(-1);
  44.     }
  45.     PNODE pTail = pHead;
  46.     pTail->pNext = NULL;
  47.     int i;
  48.     int len;
  49.     int val;
  50.     printf("input arg number: len=");
  51.     scanf("%d", &len);
  52.     for (i = 0; i < len; ++i)
  53.     {
  54.         printf("第%d个值:", i+1);
  55.         scanf("%d", &val);
  56.         PNODE pNew = (PNODE)malloc(sizeof(NODE));
  57.         if (NULL == pNew)
  58.         {
  59.             printf("分配失败,程序终止!");
  60.             exit(-1);
  61.         }
  62.         //set pNew
  63.         pNew->data = val;
  64.         pNew->pNext = NULL;
  65.         //将pNew挂到pTail下
  66.         pTail->pNext = pNew;
  67.         //更新中间变量
  68.         pTail = pNew;
  69.     }
  70.     return pHead;
  71. }

  72. void traversal(PNODE pHead)
  73. {
  74.     PNODE p = pHead->pNext;
  75.     while (NULL != p)
  76.     {
  77.         printf("%d\n",p->data);
  78.         p = p->pNext;
  79.     }
  80. }

  81. bool is_empty(PNODE pHead)
  82. {
  83.     if (NULL == pHead->pNext)
  84.         return true;
  85.     else
  86.         return false;
  87. }

  88. int length(PNODE pHead)
  89. {
  90.     PNODE p = pHead->pNext;
  91.     int len = 0;
  92.     while (NULL != p)
  93.     {
  94.         ++len;
  95.         p = p->pNext;
  96.     }
  97.     return len;
  98. }

  99. void sort(PNODE pHead)
  100. {
  101.     int i, j, t;
  102.     int len = length(pHead);
  103.     PNODE p, q;
  104.     for (i = 0,p = pHead->pNext; i < len - 1; ++i, p = p->pNext)
  105.     {
  106.         for (j = i + 1, q = p->pNext; j < len; ++j, q = q->pNext)
  107.         {
  108.             if (p->data > q->data)
  109.             {
  110.                 t = p->data;
  111.                 p->data = q->data;
  112.                 q->data = t;
  113.             }
  114.         }
  115.     }
  116. }

  117. //bool insert(PNODE pHead, int pos, int val)
  118. //{
  119. // bool flag;
  120. // int i;
  121. // int len = length(pHead);
  122. // PNODE p;
  123. // for (i = 0, p = pHead->pNext; i < len; ++i,p = p->pNext)
  124. // {
  125. // if (i + 2 == pos)
  126. // {
  127. // PNODE pNew = (PNODE)malloc(sizeof(NODE));
  128. // pNew->data = val;
  129. // pNew->pNext = p->pNext;
  130. // p->pNext = pNew;
  131. //
  132. // break;
  133. // }
  134. // }
  135. //
  136. // return flag;
  137. //}

  138. bool insert(PNODE pHead, int pos, int val)
  139. {
  140.     int i = 0;
  141.     PNODE p = pHead;

  142.     while (NULL != p && i < pos - 1)
  143.     {
  144.         p = p->pNext;
  145.         ++i;
  146.     }

  147.     if (i>pos - 1 || NULL == p)
  148.     {
  149.         return false;
  150.     }

  151.     PNODE pNew = (PNODE)malloc(sizeof(NODE));
  152.     if (NULL == pNew)
  153.     {
  154.         printf("分配失败,程序终止!");
  155.         exit(-1);
  156.     }
  157.     pNew->data = val;
  158.     pNew->pNext = p->pNext;
  159.     p->pNext = pNew;

  160.     return true;
  161. }

  162. bool delete_list(PNODE pHead, int pos, int *pVal)
  163. {
  164.     int i = 0;
  165.     PNODE p = pHead;

  166.     while (NULL != p->pNext && i < pos - 1)
  167.     {
  168.         p = p->pNext;
  169.         ++i;
  170.     }

  171.     if (i>pos - 1 || NULL == p)
  172.     {
  173.         return false;
  174.     }

  175.     PNODE q = p->pNext;
  176.     *pVal = q->data;

  177.     p->pNext = p->pNext->pNext;
  178.     free(q);
  179.     q = NULL;
  180.     return true;
  181. }

阅读(866) | 评论(0) | 转发(0) |
0

上一篇:四季养生

下一篇:数据结构之栈

给主人留下些什么吧!~~