Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1323769
  • 博文数量: 168
  • 博客积分: 2124
  • 博客等级: 大尉
  • 技术积分: 2590
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-16 23:51
文章分类

全部博文(168)

文章存档

2014年(6)

2013年(74)

2012年(71)

2011年(17)

分类: C/C++

2013-08-19 21:08:22

今天下午码的代码,注意,有时候有些链表需要一个链表头,而有些链表不用链表头,这样的话就需要区别对待。

点击(此处)折叠或打开

  1. #include<stdio.h>

  2. typedef struct node{
  3.     int data;
  4.     struct node *next;
  5. }node;
  6. /**************************************
  7. node *list_creat(void)

  8. 创建一个新链表(包含表头)
  9. 输入的数的个数作为链表的长度的
  10. 输入的数作为链表的数据
  11. 输入0则返回
  12. **************************************/
  13. node *list_creat(void)
  14. {
  15.     node *head, *new, *p;
  16.     int num = 0;
  17.     int cycle = 1;
  18.     head = (node*)malloc(sizeof(node));
  19.     head->next = NULL;
  20.     p = head;

  21.     
  22.     while(cycle)
  23.     {
  24.         printf("please enter a num\n");
  25.         scanf("%d", &num);
  26.         
  27.         if(num != 0)
  28.         {
  29.             new = (node*)malloc(sizeof(node));
  30.             p->next = new;
  31.             new->next = NULL;
  32.             new->data = num;
  33.             p = new;
  34.         }
  35.         else
  36.             cycle = 0;
  37.     }
  38.     
  39.     return head;
  40. }
  41. /**************************************
  42. void list_print_length(node *head)


  43. 打印节点的数据并打印链表的总长度
  44. **************************************/
  45. void list_print_length(node *head)
  46. {
  47.     node *p;
  48.     int num = 0;
  49.     p = head->next;//因为head只是头没有数据

  50.     if(head->next != NULL)
  51.     {
  52.         while(p->next != NULL)
  53.         {
  54.             num++;
  55.             printf("p->data = %d\n", p->data);
  56.             p = p->next;
  57.         }
  58.         num++;
  59.         printf("p->data = %d\n", p->data);
  60.     }

  61.     printf("the length of list is %d \n", num);
  62. }
  63. /********************************************************
  64. node list_sort(node *head)
  65. 对链表的数据进行排序
  66. 要点:是对数据排序,指针并不动

  67. ********************************************************/
  68. void list_sort(node *head)
  69. {
  70.     int i, tmp, j;
  71.     int num = 0;
  72.     
  73.     node *p = NULL;
  74.     p = head->next;
  75.     
  76.     while(p != NULL)
  77.     {
  78.         p = p->next;
  79.         num++;
  80.     }
  81.     for(i = 0; i < num - 1; i++)
  82.     {
  83.         p = head->next;//这步很重要!
  84.         for(j = 0; j + i < num - 1; j++)
  85.         {
  86.             if(p->data > p->next->data)
  87.             {
  88.                 tmp = p->data;
  89.                 p->data = p->next->data;
  90.                 p->next->data = tmp;
  91.             }
  92.             p = p->next;//控制着在节点上的行走
  93.         }    
  94.     }
  95. }

  96. /***************************************************************
  97. void list_insert(node *head, int data)

  98. 插入是将数据插入到有序单链表中
  99. ***************************************************************/
  100. void list_insert(node *head, int data)
  101. {
  102.     node *new, *pre , *cur;
  103.     int num;
  104.     new = (node*)malloc(sizeof(node));
  105.     new->data = data;
  106.     
  107.     pre = cur = head->next;
  108.     if(cur->data > data)
  109.     {    
  110.         new->next = cur;
  111.         head->next = new;
  112.     }
  113.     else
  114.     {
  115.         while((cur != NULL) && (cur->data < new->data))
  116.         {//(cur != NULL)要放在前面,因为&&是从左至右访问,若不放在前面,则可能访问到NULL->data,出现非法访问
  117.             pre = cur;
  118.             cur = cur->next;
  119.         }
  120.         pre->next = new;
  121.         new->next = cur;
  122.     }
  123. }

  124. /*******************************************************
  125. int node_delete(node *head, int data)

  126. 删除链表所有数据为data的节点

  127. ********************************************************/
  128. int node_delete(node *head, int data)
  129. {
  130.     node *pre, *cur, *p;
  131.     int num = 0;
  132.     int count = 0;
  133.     pre = head;
  134.     p = cur = head->next;
  135.     
  136.     while(p != NULL)
  137.     {
  138.         num++;
  139.         p = p->next;
  140.     }
  141.     if(num == 0)
  142.         return NULL;

  143.     while(cur != NULL)
  144.     {    
  145.         cur = pre; //很重要
  146.         while((cur != NULL) &&(cur->data != data))
  147.         {
  148.             pre = cur;
  149.             cur = cur->next;
  150.         }
  151.         if((cur != NULL) && (cur->data == data))
  152.         {
  153.             pre->next = cur->next;
  154.             free(cur);
  155.             count++;
  156.         }    
  157.     }
  158.     if(count ==0)
  159.         printf("cannot find the node\n");
  160.     return 1;
  161. }
  162. /*******************************************************
  163. node* list_reverse(head)

  164. 链表的逆序

  165. ********************************************************/
  166. node* list_reverse(node *head)
  167. {
  168.     node *p1, *p2, *p3, *tmp;

  169.   
  170. #if 1 //1表示有链表头部的输出方法,0表示无链表头部的输出方法
  171.     
  172.     //有链表头部的写法
  173.     tmp = p1 = head->next;
  174.     p2 =head->next->next;    
  175.     if(head == NULL)
  176.         return NULL;
  177.     if(head->next == NULL || head->next->next == NULL)
  178.         return head;

  179.     while(p2 != NULL)
  180.     {
  181.         p3 = p2->next;
  182.         p2->next = p1;
  183.         p1 = p2;
  184.         p2 = p3;
  185.     }
  186.     tmp->next = NULL;
  187.     head->next = p1;
  188.     return head;

  189. #else

  190.     //无链表头部写法
  191.     p1 = head;
  192.     p2 = head->next;
  193.     if(head == NULL || head->next == NULL)
  194.         return NULL;
  195.     
  196.     while(p2 != NULL)
  197.     {
  198.         p3 = p2->next;
  199.         p2->next = p1;
  200.         p1 = p2;
  201.         p2 = p3;
  202.     }
  203.     
  204.     head->next = NULL;
  205.     head = p1;
  206.     return head;
  207. #endif

  208. }

  209. int main(int argc, char **argv)
  210. {    
  211.     int i;
  212.     node *head;
  213.     
  214.     head = list_creat();
  215. /*    
  216.     printf("please enter the num you want insert!\n");
  217.     scanf("%d", &i);
  218.     list_insert(head, i);
  219.     
  220.     printf("please enter the num you want delete!\n");
  221.     scanf("%d", &i);
  222.     node_delete(head, i);
  223. */    
  224. //    list_sort(head);

  225. //    list_print_length(head);

  226.     head = list_reverse(head);
  227.     list_print_length(head);
  228.     
  229.     while(1);
  230. }

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