Chinaunix首页 | 论坛 | 博客
  • 博客访问: 416526
  • 博文数量: 73
  • 博客积分: 3326
  • 博客等级: 中校
  • 技术积分: 631
  • 用 户 组: 普通用户
  • 注册时间: 2010-02-05 15:31
文章分类

全部博文(73)

文章存档

2014年(1)

2011年(51)

2010年(21)

分类: C/C++

2011-08-24 08:57:34

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

  3. #define NODE_NUM 15

  4. /* struct of the single link */
  5. typedef struct _link
  6. {
  7.     int data;
  8.     struct _link *next;
  9. }S_LINK;

  10. /*
  11.  *get the length of a linklist
  12.  * */
  13. int get_link_len(S_LINK *link)
  14. {
  15.     S_LINK *p = link;
  16.     int len = 0;

  17.     while(NULL != p)
  18.     {
  19.         p = p->next;
  20.         len += 1;
  21.     }

  22.     return len;

  23. }


  24. /* search from the linklist
  25.  * find out the reversed k pos data
  26.  * */
  27. int search_posk(S_LINK *link,int k)
  28. {
  29.     printf("============SEARCH FROM LINKLIST===========\n");
  30.     if(NULL == link)
  31.     {
  32.         printf("error:null linklist\n");
  33.         exit(0);
  34.     }
  35.     
  36.     S_LINK *tmp;
  37.     int len;
  38.     tmp = link;
  39.     len = get_link_len(link);

  40.     if(len < k)
  41.     {
  42.         printf("Error:k is larger than length\n");
  43.         exit(0);
  44.     }
  45.     k = len - k;
  46.     while(0 != (k--))
  47.     {
  48.         tmp = tmp->next;
  49.     }
  50.     
  51.     if(NULL != tmp)
  52.     {
  53.         return tmp->data;
  54.     }
  55.     else
  56.     {
  57.         return 0;
  58.     }

  59. }

  60. /**
  61.  *create single link node
  62.  **/
  63. S_LINK *create_node(int data)
  64. {
  65.     S_LINK *tmp;
  66.     tmp = (S_LINK*)malloc(sizeof(S_LINK));
  67.     tmp->data = data;
  68.     tmp->next = NULL;    
  69.     printf("create_node %d data:%d\n",data,data);
  70.     return tmp;
  71. }

  72. /* print linklist */
  73. void print_link(S_LINK *head)
  74. {
  75.     S_LINK *p = head->next;
  76.     int i;

  77.     printf("=============PRINT LINKLIST VALUE===========\n");
  78.     while(NULL != p)
  79.     {    
  80.         printf("%d-->",p->data);
  81.         p = p->next;
  82.     }

  83.     printf("NULL\n");
  84. }

  85. int main()
  86. {
  87.     int i;
  88.     S_LINK *linklist;//head node ptr
  89.     S_LINK *node = NULL;
  90.     S_LINK *move = NULL;

  91.     linklist = (S_LINK *)malloc(sizeof(S_LINK));

  92.     linklist->data = 0;
  93.     linklist->next = NULL;
  94. //    move = linklist;
  95.     printf("head.data:%d\n",linklist->data);

  96.     printf("=============SINGLE LINK CREATE==============\n");
  97.     for(i = 0; i < NODE_NUM; i ++)
  98.     {

  99.         S_LINK *node;
  100.         node = create_node(i);
  101.     //    printf("move->data:%d\n",move->data);
  102.     //    printf("node->data:%d\n",node->data);
  103. //        move->next = node;
  104.         node->next = linklist->next;
  105.         linklist->next = node;
  106.     //    move = node;
  107.     //    printf("i=%d,node.data:%d\n",i,node->data);

  108.     }
  109.     
  110.     print_link(linklist);
  111.     
  112.     int ret = search_posk(linklist,3);
  113.     printf("the reversed order pos %d node value is %d\n",3,ret);

  114.     return 0;
  115. }
阅读(1727) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~