singly linked list 单向链表反转

3763阅读 0评论2012-01-13 jonas_mao
分类:C/C++

以前一直都没写过,今天写了个:
 
  1. /*
  2.  * reserve the linked list without circular 链表反向
  3.  */

  4.  #include <stdio.h>
  5.  #include <stdlib.h>

  6.  struct linklist
  7.  {
  8.    struct linklist *next;
  9.    
  10.    int key;
  11.  };

  12.  void
  13.  printf_linklist (struct linklist *head)
  14.  {
  15.    struct linklist *lnode;
  16.    
  17.    if (head == NULL)
  18.      printf ("%% linklist is NULL\n");
  19.     
  20.    lnode = head;
  21.    printf (" Linklist : ");
  22.    for (; lnode; lnode = lnode->next)
  23.      {
  24.      printf (" %d ", lnode->key);
  25.      }
  26.    
  27.    return;
  28.  }

  29.  struct linklist *
  30.  reverse_linklist (struct linklist *head)
  31.  {
  32.    struct linklist *re_link;
  33.    struct linklist *link_tmp;
  34.    struct linklist *link_next;
  35.    
  36.    if (head == NULL)
  37.      return NULL;

  38.    re_link = head;
  39.    link_next = head->next;
  40.    re_link->next = NULL;
  41.    
  42.    while (link_next)
  43.      {
  44.      link_tmp = link_next;
  45.        
  46.        link_next = link_next->next;
  47.        
  48.        link_tmp->next = re_link;
  49.        re_link = link_tmp;    
  50.      }
  51.     
  52.     return re_link;
  53.  }

  54.  struct linklist *
  55.  create_linklist (int numb)
  56.  {
  57.    int i;
  58.    struct linklist *lists[numb];
  59.    struct linklist *lnode;
  60.    struct linklist *lnode_n;

  61.    for (i = 0; i < numb; i++)
  62.      {
  63.      lists[i] = (struct linklist *) malloc (sizeof (struct linklist));
  64.         if (lists[i] == NULL)
  65.          break;
  66.         
  67.         lists[i]->key = i;
  68.         lists[i]->next = NULL;
  69.         if (i > 0)
  70.          lists[i-1]->next = lists[i];
  71.      }
  72.     
  73.    if (i < numb)
  74.      {
  75.      for (lnode = lists[0]; lnode; lnode = lnode_n)
  76.      {
  77.          lnode_n = lnode->next;
  78.          free (lnode);
  79.          lnode = NULL;
  80.          }
  81.         
  82.      return NULL;
  83.      }
  84.    
  85.    return lists[0];
  86.  }

  87.  void
  88.  destroy_linklist (struct linklist *head)
  89.  {
  90.    struct linklist *lnode;
  91.    struct linklist *lnode_n;
  92.    
  93.    if (head == NULL)
  94.      return;
  95.    
  96.    for (lnode = head; lnode; lnode = lnode_n)
  97.      {
  98.      lnode_n = lnode->next;
  99.      free (lnode);
  100.      lnode = NULL;
  101.      }
  102.   
  103.    return;
  104.  }
  105.  int
  106.  main (void)
  107.  {
  108.    int numb;
  109.    struct linklist *head;
  110.    
  111.    printf ("\n pls enter the listnode numb : ");
  112.    scanf ("%d", &numb);
  113.    
  114.    head = create_linklist (numb);
  115.    printf ("\n before reverse :");
  116.    printf_linklist (head);
  117.    
  118.    head = reverse_linklist (head);
  119.    printf ("\n after reverse :");
  120.    printf_linklist (head);
  121.    
  122.    destroy_linklist (head);
  123.    
  124.    return 0;
  125.  }

运行结果:(gcc, 今天利用这个特意试了下gdb 调试)

 

上一篇:BGP中采用静态配置邻居的好处
下一篇:GDB调试精粹及使用实例