1. #include <stdio.h> 
  2. #include <stdlib.h> 
  3. //#define CRT_SECURE_NO_WARNINGS 
  4.  
  5. #define LEN struct DataSequence 
  6.  
  7. struct DataSequence 
  8.     int id; 
  9.     char commit[50]; 
  10.     struct DataSequence * next; 
  11. }; 
  12. //添加数据 
  13. struct DataSequence * add(struct DataSequence * head) 
  14.     struct DataSequence *p, *q; 
  15.     q = p = (struct DataSequence *)malloc(sizeof(struct DataSequence)); 
  16.      
  17.     if(head == NULL) 
  18.     { 
  19.         head = q; 
  20.     } 
  21.     else 
  22.     { 
  23.         p = head; 
  24.         while(p -> next != NULL) 
  25.         { 
  26.             p = p -> next; 
  27.         } 
  28.         p -> next = q; 
  29.     } 
  30.      
  31.     printf("请输入编号:"); 
  32.     scanf("%d", &q -> id); 
  33.     printf("请输入备注:"); 
  34.     scanf("%s", q -> commit); 
  35.     q -> next = NULL; 
  36.  
  37.     return head; 
  38.      
  39. //  就地逆置 
  40. struct DataSequence * reverse(struct DataSequence * head) 
  41.     struct DataSequence * p, * q, * temp; 
  42.     temp = q = p = head; 
  43.  
  44.     if(head == NULL) 
  45.     { 
  46.         printf("糟糕,该链表还没有数据\n\n"); 
  47.         return head; 
  48.     } 
  49.  
  50.     p = p -> next; 
  51.     temp -> next = NULL; 
  52.     q = temp; 
  53.     head = q; 
  54.  
  55.     while(p != NULL) 
  56.     {        
  57.         temp = p; 
  58.         p = p -> next; 
  59.         temp -> next = q; 
  60.         q = temp; 
  61.         head = q; 
  62.     } 
  63.      
  64.     return head; 
  65.  
  66. //  求链表长度 
  67. void length(struct DataSequence * head) 
  68.     int n = 0; 
  69.     struct DataSequence * p = head; 
  70.     if(p == NULL) 
  71.     { 
  72.         n = 0; 
  73.     } 
  74.     while(p != NULL) 
  75.     { 
  76.         n++; 
  77.         p = p -> next; 
  78.     } 
  79.     printf("长度为:%d\n\n",n); 
  80. //  遍历链表并打印输出 
  81. void print(struct DataSequence * head) 
  82.     struct DataSequence * p = head; 
  83.     if(p == NULL) 
  84.     { 
  85.         printf("没有发现数据\n\n"); 
  86.     } 
  87.     while(p != NULL) 
  88.     { 
  89.         printf("\t编号:%d, 备注:%s\n", p -> id, p -> commit); 
  90.         p = p -> next; 
  91.     } 
  92.  
  93. void main() 
  94.     struct DataSequence * head = (struct DataSequence *)malloc(sizeof(LEN)); 
  95.     int input; 
  96.     head = NULL; 
  97.      
  98.     while(1) 
  99.     { 
  100.         printf("\t1.添加\n"); 
  101.         printf("\t2.逆序\n"); 
  102.         printf("\t3.输出\n"); 
  103.         printf("\t4.长度\n"); 
  104.         printf("\t0.退出\n"); 
  105.  
  106.         scanf("%d", &input); 
  107.         if(input == 1) 
  108.         { 
  109.             head = add(head); 
  110.         } 
  111.         else if(input == 2) 
  112.         { 
  113.             head = reverse(head); 
  114.         } 
  115.         else if(input == 3) 
  116.         { 
  117.             print(head); 
  118.         } 
  119.         else if(input == 4) 
  120.         { 
  121.             length(head); 
  122.         } 
  123.         else if(input == 0) 
  124.         { 
  125.             printf("\t您选择了退出操作\n"); 
  126.             exit(0); 
  127.         } 
  128.     } 
  129.