Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1320596
  • 博文数量: 175
  • 博客积分: 2743
  • 博客等级: 少校
  • 技术积分: 4024
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-30 01:41
文章分类

全部博文(175)

文章存档

2015年(1)

2013年(53)

2012年(71)

2011年(50)

分类: LINUX

2013-03-14 10:35:22

一.单链表:

点击(此处)折叠或打开

  1. #include"stdio.h"
  2. #include"malloc.h"
  3. #define NULL 0
  4. #define L sizeof(struct integer)
  5. struct integer /*定义结构体*/
  6. {
  7. int num;
  8. int zhengshu;
  9.     struct integer *next;
  10. };
  11. int n; //纪录链表的长度
  12. struct integer *creat(void) /*创建链表*/
  13. {
  14.     struct integer *head;
  15.     struct integer *p1,*p2;
  16.     n=0;
  17.     p1=p2=(struct integer *)malloc(L);
  18.     scanf("%d,%d",&p1->num,&p1->zhengshu);
  19.     head=NULL;
  20.     while(p1->num!=0)
  21. {
  22. n=n+1;
  23.         if(n==1) head=p1;
  24.         else p2->next=p1;
  25.         p2=p1;
  26.         p1=(struct integer *)malloc(L);
  27.         scanf("%d,%d",&p1->num,&p1->zhengshu);
  28. }
  29.     p2->next=NULL;
  30.     return(head);
  31. }

  32. void print(struct integer *head) /*打印链表中的数据*/
  33. {
  34.     struct integer *p;
  35.     printf("Now %d biaohao and zhengshu are :n",n);
  36.     p=head;
  37.     if(head!=NULL)
  38.     do
  39.      {printf("%d,%5.1dn",p->num,p->zhengshu);
  40.       p=p->next;
  41.      }while(p!=NULL);
  42. }

  43. int count(struct integer *head) /*返回链表的长度*/
  44. {
  45. int i=0;
  46.     struct integer *p;
  47. p=head;
  48. while(p!=NULL)
  49. {
  50. p=p->next;i++;
  51. }
  52. return i;
  53. }

  54. void *findnode(struct integer *head,int num) /*查找链表中的第num个数据*/
  55. {
  56. int j=1;
  57. struct integer *p;
  58. /*if(head==NULL)
  59. {
  60. printf("n空链表,请先创建!n");
  61. return;
  62. }*/
  63. p=head;
  64. if(num>count(head)||num<=0)
  65. {
  66. printf("Input error! Please input againn");
  67. }
  68. else
  69. {
  70. while(p!=NULL && jnext;
  71. }
  72. printf("%d bianhao reprensts %dn",p->num,p->zhengshu);
  73. printf("n");
  74. }
  75. return(head);
  76. }


  77. struct integer *del(struct integer *head,long num) /*删除链表中的第num个数据*/
  78. {
  79. struct integer *p1,*p2;
  80.     if(head==NULL)
  81. {
  82. printf("nList Null!n");
  83.         return;
  84. }
  85.     p1=head;
  86.     while(num!=p1->num && p1->next!=NULL)
  87. {
  88.         p2=p1;
  89.         p1=p1->next;
  90. }
  91.     if(num==p1->num)
  92.     {
  93.         if(p1==head) head=p1->next;
  94.         else p2->next=p1->next;
  95.         printf("Delete: %dn",num);
  96.         n=n-1;
  97.     }
  98.     else printf("%d not been fonnd!n",num);
  99.     return(head);
  100. }

  101. struct integer *insert(struct integer *head,struct integer *stud) /*插入数据*/
  102. {
  103.     struct integer *p0,*p1,*p2;
  104.     p1=head;
  105.     p0=stud;
  106.     if(head==NULL)
  107. {
  108. head=p0;
  109.         p0->next=NULL;
  110. }
  111.     else
  112. {
  113. while((p0->num>p1->num)&&(p1->next!=NULL))
  114. {
  115. p2=p1;
  116.             p1=p1->next;
  117. }
  118. if(p0->num<=p1->num)
  119. {
  120.             if(head==p1)head=p0;
  121.             else p2->next=p0;
  122.             p0->next=p1;
  123. }
  124.         else
  125. {
  126.             p1->next=p0;
  127.             p0->next=NULL;
  128. }
  129. }
  130.     n=n+1;
  131.     return(head);
  132. }

  133. main() /*主功能函数*/
  134. {
  135. int a,b;
  136.     struct integer *head,stu;
  137.     int del_num,fin_num;
  138. printf("1 to go on / 0 to exit:n");
  139. scanf("%d",&a);
  140. while(a!=0)
  141. {
  142. /*struct integer *head,stu;
  143.         int del_num;*/
  144. printf("1 creat 2 print 3 delete 4 insert 5 findonde 0 exitn");
  145. /*菜单的实现*/
  146. scanf("%d",&b);
  147. switch(b)
  148. {
  149. case 1:
  150. {
  151. /*clrscr();*/
  152. printf("Please Input bianhao and data:n");
  153. printf("for example 1,90 0,0 to exit:n");
  154. head=creat();
  155. }break;
  156. case 2:
  157. {
  158. /*clrscr();*/
  159. print(head);
  160. }break;
  161. case 3:
  162. {
  163. /*clrscr();*/
  164.     printf("nInput the deleted biaohao:");
  165.     scanf("%d",&del_num);
  166.     head=del(head,del_num);
  167. }break;
  168.         case 4:
  169. {
  170. /*clrscr();*/
  171.                 printf("nInput the inserted biaohao and zhengshu:");
  172.                 scanf("%d,%d",&stu.num,&stu.zhengshu);
  173.                 head=insert(head,&stu);
  174. }break;
  175.         case 5:
  176. {
  177. /*clrscr();*/
  178. printf("Please Input the bianhao you want to find:");
  179. scanf("%d",&fin_num);
  180. head=findnode(head,fin_num);
  181. }break;
  182. case 0:
  183. {
  184.      return;
  185. }break;
  186. default:
  187. printf("Input error! Please input againn");
  188. }
  189. }
  190.  
  191. }

在链表头插入
  1. struct integer *insert_head(struct integer *head,struct integer *stud) /*插入数据*/
  2. {
  3.     struct integer *p0,*p1,*p2;
  4.     p1=head;
  5.     p0=stud;
  6.     if(head==NULL)
  7. {
  8.     printf("erro table is null\r\n");
  9.     return -1;
  10. }

  11. p1->next = p0;
  12. p0->next = p1->next;
  13. return(head);
  14. }


在链表尾插入

  1. struct integer *insert_head(struct integer *head,struct integer *stud) /*插入数据*/
  2. {
  3.     struct integer *p0,*p1,*p2;
  4.     p1=head;
  5.     p0=stud;
  6.     if(head==NULL)
  7. {
  8.     printf("erro table is null\r\n");
  9.     return -1;
  10. }
  11. while(p1->next!=NULL)
  12. {
  13.     p1=p1->next;
  14. }
    p1->next = p0;
  15. p0->next = p1->next;
  16. return(head);
  17. }




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