054.链表操作(1)

举报
C语言与CPP编程 发表于 2022/04/30 23:16:33 2022/04/30
【摘要】 #include <stdio.h>#include <conio.h>#define N 10 typedef struct node{ char name[20]; struct node *link;}stud; stud * creat(){ stud *p,*h,*s; int ...

  
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #define N 10
  4. typedef struct node
  5. {
  6. char name[20];
  7. struct node *link;
  8. }stud;
  9. stud * creat()
  10. {
  11. stud *p,*h,*s;
  12. int i,n;
  13. puts("\nPlease input the number of linklist:");
  14. scanf("%d",&n);
  15. if((h=(stud *)malloc(sizeof(stud)))==NULL)
  16. {
  17. printf("cannot find space!");
  18. exit(0);
  19. }
  20. h->name[0]='\0';
  21. h->link=NULL;
  22. p=h;
  23. for(i=0;i<n;i++)
  24. {
  25. if((s= (stud *) malloc(sizeof(stud)))==NULL)
  26. {
  27. printf("cannot find space!");
  28. exit(0);
  29. }
  30. p->link=s;
  31. printf("please input %d student's name: ",i+1);
  32. scanf("%s",s->name);
  33. s->link=NULL;
  34. p=s;
  35. }
  36. return(h);
  37. }
  38. stud * search(stud *h,char *x)
  39. {
  40. stud *p;
  41. char *y;
  42. p=h->link;
  43. while(p!=NULL)
  44. {
  45. y=p->name;
  46. if(strcmp(y,x)==0)
  47. return(p);
  48. else p=p->link;
  49. }
  50. if(p==NULL)
  51. printf("data not find!");
  52. return 0;
  53. }
  54. stud * search2(stud *h,char *x)
  55. {
  56. stud *p,*s;
  57. char *y;
  58. p=h->link;
  59. s=h;
  60. while(p!=NULL)
  61. {
  62. y=p->name;
  63. if(strcmp(y,x)==0)
  64. return(s);
  65. else
  66. {
  67. p=p->link;
  68. s=s->link;
  69. }
  70. }
  71. if(p==NULL)
  72. printf("data not find!");
  73. return 0;
  74. }
  75. void insert(stud *p)
  76. {
  77. char stuname[20];
  78. stud *s;
  79. if((s= (stud *) malloc(sizeof(stud)))==NULL)
  80. {
  81. printf("cannot find space!");
  82. exit(0);
  83. }
  84. printf("\nplease input the student's name: ");
  85. scanf("%s",stuname);
  86. strcpy(s->name,stuname);
  87. s->link=p->link;
  88. p->link=s;
  89. }
  90. void del(stud *x,stud *y)
  91. {
  92. stud *s;
  93. s=y;
  94. x->link=y->link;
  95. free(s);
  96. }
  97. void print(stud *h)
  98. {
  99. stud *p;
  100. p=h->link;
  101. printf("Now the link list is:\n");
  102. while(p!=NULL)
  103. {
  104. printf("%s ",&*(p->name));
  105. p=p->link;
  106. }
  107. printf("\n");
  108. }
  109. void quit()
  110. {
  111. clrscr();
  112. puts("\n Thank you for your using!\n Press any key to quit...");
  113. getch();
  114. exit(0);
  115. }
  116. void menu(void)
  117. {
  118. clrscr();
  119. printf(" simple linklise realization of c\n");
  120. printf(" ||=====================================||\n");
  121. printf(" || ||\n");
  122. printf(" || [1] create linklist ||\n");
  123. printf(" || [2] seach ||\n");
  124. printf(" || [3] insert ||\n");
  125. printf(" || [4] delete ||\n");
  126. printf(" || [5] print ||\n");
  127. printf(" || [6] exit ||\n");
  128. printf(" || ||\n");
  129. printf(" || if no list exist,create first ||\n");
  130. printf(" || ||\n");
  131. printf(" ||=====================================||\n");
  132. printf(" Please input your choose(1-6): ");
  133. }
  134. main()
  135. {
  136. int choose;
  137. stud *head,*searchpoint,*forepoint;
  138. char fullname[20];
  139. while(1)
  140. {
  141. menu();
  142. scanf("%d",&choose);
  143. switch(choose)
  144. {
  145. case 1:
  146. clrscr();
  147. head=creat();
  148. puts("Linklist created successfully! \nPress any key to return...");
  149. getch();
  150. break;
  151. case 2:
  152. clrscr();
  153. printf("Input the student's name which you want to find:\n");
  154. scanf("%s",fullname);
  155. searchpoint=search(head,fullname);
  156. printf("The stud name you want to find is:%s",*&searchpoint->name);
  157. printf("\nPress any key to returen...");
  158. getchar();
  159. getchar();
  160. break;
  161. case 3:
  162. clrscr();
  163. insert(head);
  164. print(head);
  165. printf("\nPress any key to returen...");
  166. getchar();getchar();
  167. break;
  168. case 4:
  169. clrscr();
  170. print(head);
  171. printf("\nInput the student's name which you want to delete:\n");
  172. scanf("%s",fullname);
  173. searchpoint=search(head,fullname);
  174. forepoint=search2(head,fullname);
  175. del(forepoint,searchpoint);
  176. print(head);
  177. puts("\nDelete successfully! Press any key to return...");
  178. getchar();
  179. getchar();
  180. break;
  181. case 5:print(head);
  182. printf("\nPress any key to return...");
  183. getchar();getchar();
  184. break;
  185. case 6:quit();
  186. break;
  187. default:
  188. clrscr();
  189. printf("Illegal letter! Press any key to return...");
  190. menu();
  191. getchar();
  192. }
  193. }
  194. }

文章来源: blog.csdn.net,作者:程序员编程指南,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/weixin_41055260/article/details/124494951

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。