数据结构---单链表(建立,节点删除,节点插入)

来源:互联网 发布:asp.net 字符串转json 编辑:程序博客网 时间:2024/05/17 06:37

//UNIX环境2011-02-03


[cpp] view plaincopy
  1. #include <stdio.h>  
  2. #include <string.h>  
  3. #include <stdlib.h>  
  4. typedef struct student  
  5. {  
  6.     int num;  
  7.     char name[16];  
  8.     struct student *next;  
  9. }node;  
  10. node *creat()//创建链表  
  11. {  
  12.     int x;  
  13.     int cycle = 1;  
  14.     char stu_name[16];  
  15.     node *head,*p,*s;  
  16.     head =(node*)malloc(sizeof(node));  
  17.     p = head;  
  18.     while(cycle)  
  19.     {  
  20.         printf("please input the student number:");  
  21.         scanf("%d",&x);  
  22.         if(x != 0)  
  23.         {  
  24.             printf("please input the student name:");  
  25.             scanf("%s",stu_name);  
  26.         //  printf("STU_NAME:%s/n", stu_name);  
  27.             s = (node *)malloc(sizeof(node));  
  28.                         if(NULL == s)  
  29.                         {  
  30.                         exit(1);  
  31.                         }  
  32.             s->num = x;  
  33.             memcpy(s->name, stu_name, 16);  
  34.             p->next = s;  
  35.             p = s;   
  36.         }  
  37.         else  
  38.             cycle = 0;  
  39.     }  
  40.     head = head->next;  
  41.     p->next = NULL;  
  42.     return head;  
  43. }  
  44. int length(node *head)//链表测长  
  45. {  
  46.     int n = 0;  
  47.     node *p;  
  48.     p = head;  
  49.     while(p != NULL)  
  50.     {  
  51.         n++;  
  52.                 p = p->next;  
  53.                   
  54.     }  
  55.     return (n);  
  56. }  
  57. node *insert(node *head, int num, int length)//链表插入,NUM表示在第几个位置插入  
  58. {  
  59.     int n=num;  
  60.     int i = 1;  
  61.     node *p0,*p1,*p2;  
  62.     p1 = head;  
  63.     p0 = (node *)malloc(sizeof(node));  
  64.     printf("please input the student number:");  
  65.     scanf("%d", &p0->num);  
  66.     printf("please input the student name:");  
  67.     scanf("%s", &p0->name);  
  68.     if(n == 1)//插入表头  
  69.     {  
  70.         p0->next = p1;  
  71.         head = p0;  
  72.         return (head);  
  73.     }  
  74.     while(i < n-1)//找到要插入的前置节点  
  75.     {  
  76.         p1 = p1->next;  
  77.         i++;  
  78.     }  
  79.     p2 = p1->next;  
  80.     p1->next = p0;  
  81.     p0->next = p2;  
  82.     if(n == length+1)//插入表尾  
  83.     {  
  84.         p1->next = p0;  
  85.         p0->next = NULL;  
  86.     }  
  87.     return (head);  
  88. }  
  89. node *delete(node *head, int location, int length)//删除链表节点  
  90. {  
  91.     int n = location;  
  92.     int i = 1;  
  93.     node *p1,*p2;  
  94.     p1 = head;  
  95.     if(n == 1)  
  96.     {  
  97.         head = p1->next;  
  98.         free(p1);  
  99.         return (head);  
  100.     }  
  101.     while(i < n-1)//找到要删除的节点的前置节点  
  102.     {  
  103.         p1 = p1->next;  
  104.         i++;  
  105.     }  
  106.     if(n < length)  
  107.     {  
  108.         p2 = p1->next;  
  109.         p1->next = p2->next;  
  110.         free(p2);  
  111.     }  
  112.     if(n == length)  
  113.     {  
  114.         p2 = p1->next;  
  115.         p1->next = NULL;  
  116.         free(p2);  
  117.     }  
  118.     return (head);  
  119. }  
  120. void print(node *head)  
  121. {  
  122.     while(head != NULL)  
  123.     {  
  124.         printf("students:%d/n", head->num);  
  125.                 printf("student name: %s/n", head->name);  
  126.         head = head->next;  
  127.                   
  128.     }  
  129. }  
  130. node *invert(node *head)//链表逆置  
  131. {  
  132. node *p1,*p2,*p3;  
  133. p1 = head;  
  134. p2 = p1->next;  
  135. while(p2)  
  136. {  
  137. p3 = p2->next;  
  138. p2->next = p1;  
  139. p1=p2;  
  140. p2=p3;  
  141. }  
  142. head->next = NULL;  
  143. head = p1;  
  144. return (head);  
  145. }  
  146. int main(int argc, char **argv)  
  147. {  
  148.     int len,insert_num,del_num;  
  149.     node *stu_node;  
  150.     stu_node = creat();  
  151.     print(stu_node);  
  152.     len = length(stu_node);  
  153.     printf("there are %d node/n", len);  
  154.     printf("what dou you want to do?/n[a]打印链表 [b]逆置链表 [c]删除节点 [d]插入节点: ");  
  155. char ch,c;  
  156. c = getchar();//用于清除缓冲区中的/n字符  
  157. scanf("%c",&ch);  
  158.     switch (ch)  
  159.     {  
  160.         case 'a': print(stu_node);  
  161.               break;  
  162.         case 'b': stu_node = invert(stu_node);  
  163.               print(stu_node);  
  164.               break;  
  165.         case 'c':   
  166.               printf("which node dou you want to detele?:");  
  167.               scanf("%d", &del_num);  
  168.               stu_node = delete(stu_node, del_num, len);  
  169.               print(stu_node);  
  170.               break;  
  171.         case 'd':  
  172.               printf("which location dou you want to insert:");  
  173.               scanf("%d",&insert_num);  
  174.               stu_node = insert(stu_node , insert_num, len);  
  175.               print(stu_node);  
  176.               break;  
  177.     }  
  178.     return 0;  
  179. }  

0 0
原创粉丝点击