织梦CMS - 轻松建站从此开始!

罗索

链表操作

jackyhwei 发布于 2011-03-30 09:50 点击:次 
在turbo c 2.0 下调试通过.(把汉语注释去掉)
TAG:

在turbo c 2.0 下调试通过.(把汉语注释去掉)

  1. #include"stdio.h" 
  2. #include"malloc.h" 
  3. #define NULL 0 
  4. #define L sizeof(struct integer) 
  5. struct integer                           /*定义结构体*/ 
  6. int num; 
  7. int zhengshu; 
  8.     struct integer *next; 
  9. }; 
  10. int n;   //纪录链表的长度 
  11. struct integer *creat(void)             /*创建链表*/ 
  12.     struct integer *head; 
  13.     struct integer *p1,*p2; 
  14.     n=0; 
  15.     p1=p2=(struct integer *)malloc(L); 
  16.     scanf("%d,%d",&p1->num,&p1->zhengshu); 
  17.     head=NULL; 
  18.     while(p1->num!=0) 
  19. n=n+1; 
  20.         if(n==1) head=p1; 
  21.         else p2->next=p1; 
  22.         p2=p1; 
  23.         p1=(struct integer *)malloc(L); 
  24.         scanf("%d,%d",&p1->num,&p1->zhengshu); 
  25.     p2->next=NULL; 
  26.     return(head); 
  27.  
  28. void print(struct integer *head)    /*打印链表中的数据*/ 
  29.     struct integer *p; 
  30.     printf("Now %d biaohao and zhengshu are :n",n); 
  31.     p=head; 
  32.     if(head!=NULL) 
  33.     do 
  34.      {printf("%d,%5.1dn",p->num,p->zhengshu); 
  35.       p=p->next; 
  36.      }while(p!=NULL); 
  37.  
  38. int count(struct integer *head)   /*返回链表的长度*/ 
  39. int i=0; 
  40.     struct integer *p; 
  41. p=head; 
  42. while(p!=NULL) 
  43. p=p->next;i++; 
  44. return i; 
  45.  
  46. void *findnode(struct integer *head,int num)  /*查找链表中的第num个数据*/ 
  47. int j=1; 
  48. struct integer *p; 
  49. /*if(head==NULL) 
  50. { 
  51. printf("n空链表,请先创建!n"); 
  52. return; 
  53. }*/ 
  54. p=head; 
  55. if(num>count(head)||num<=0) 
  56. printf("Input error! Please input againn"); 
  57. else 
  58. while(p!=NULL && j<num) 
  59. j++; 
  60. p=p->next; 
  61. printf("%d bianhao reprensts %dn",p->num,p->zhengshu); 
  62. printf("n"); 
  63. return(head); 
  64.  
  65.  
  66. struct integer *del(struct integer *head,long num)  /*删除链表中的第num个数据*/ 
  67. struct integer *p1,*p2; 
  68.     if(head==NULL) 
  69. printf("nList Null!n"); 
  70.         return
  71.     p1=head; 
  72.     while(num!=p1->num && p1->next!=NULL) 
  73.         p2=p1; 
  74.         p1=p1->next; 
  75.     if(num==p1->num) 
  76.     { 
  77.         if(p1==head) head=p1->next; 
  78.         else p2->next=p1->next; 
  79.         printf("Delete: %dn",num); 
  80.         n=n-1; 
  81.     } 
  82.     else printf("%d not been fonnd!n",num); 
  83.     return(head); 
  84.  
  85. struct integer *insert(struct integer *head,struct integer *stud)  /*插入数据*/ 
  86.     struct integer *p0,*p1,*p2; 
  87.     p1=head; 
  88.     p0=stud; 
  89.     if(head==NULL) 
  90. head=p0; 
  91.         p0->next=NULL; 
  92.     else 
  93. while((p0->num>p1->num)&&(p1->next!=NULL)) 
  94. p2=p1; 
  95.             p1=p1->next; 
  96. if(p0->num<=p1->num) 
  97.             if(head==p1)head=p0; 
  98.             else p2->next=p0; 
  99.             p0->next=p1; 
  100.         else 
  101.             p1->next=p0; 
  102.             p0->next=NULL; 
  103.     n=n+1; 
  104.     return(head); 
  105.  
  106. main()  /*主功能函数*/ 
  107. int a,b; 
  108.     struct integer *head,stu; 
  109.     int del_num,fin_num; 
  110. printf("1 to go on / 0 to exit:n"); 
  111. scanf("%d",&a); 
  112. while(a!=0) 
  113. /*struct integer *head,stu; 
  114.         int del_num;*/ 
  115. printf("1 creat 2 print 3 delete 4 insert 5 findonde 0 exitn"); 
  116. /*菜单的实现*/ 
  117. scanf("%d",&b); 
  118. switch(b) 
  119. case 1: 
  120. /*clrscr();*/ 
  121. printf("Please Input bianhao and data:n"); 
  122. printf("for example 1,90  0,0 to exit:n"); 
  123. head=creat(); 
  124. }break
  125. case 2: 
  126. /*clrscr();*/ 
  127. print(head); 
  128. }break
  129. case 3: 
  130. /*clrscr();*/ 
  131.     printf("nInput the deleted biaohao:"); 
  132.     scanf("%d",&del_num); 
  133.     head=del(head,del_num); 
  134. }break
  135.         case 4: 
  136. /*clrscr();*/ 
  137.                 printf("nInput the inserted biaohao and zhengshu:"); 
  138.                 scanf("%d,%d",&stu.num,&stu.zhengshu); 
  139.                 head=insert(head,&stu); 
  140. }break
  141.         case 5: 
  142. /*clrscr();*/ 
  143. printf("Please Input the bianhao you want to find:"); 
  144. scanf("%d",&fin_num); 
  145. head=findnode(head,fin_num); 
  146. }break
  147. case 0: 
  148.      return
  149. }break
  150. default
  151. printf("Input error! Please input againn"); 

 

(cuishinankobe)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201103/11139.html]
本文出处:CSDN博客 作者:cuishinankobe
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容