鱼C论坛

 找回密码
 立即注册
查看: 3324|回复: 6

[技术交流] 单链表的写的一个小程序,大家拿去测试下,有不足之处望指出.

[复制链接]
发表于 2011-8-14 20:16:14 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include<stdio.h>
  2. #include<malloc.h>

  3. struct node
  4. {
  5. int data; //数据域
  6. struct node *next; //指针域
  7. };

  8. //函数声明
  9. struct node * creat(void); //创建链表
  10. void print(struct node *); //输出链表
  11. void listinsert(struct node *); //插入元素
  12. void listdelete(struct node *); //删除元素

  13. //主函数
  14. int main(void)
  15. {
  16. //在主函数里创建空的头指针
  17. struct node * phead = NULL;

  18. phead = creat(); //从头指针开始创建链表
  19. printf("\n");
  20. char quit;
  21. do
  22. {
  23. char select;
  24. printf("输入 p:打印 i:插入 d:删除\n");
  25. select = getch();
  26. switch(select)
  27. {
  28. case 'p' :
  29. {
  30. printf("\n当前可以接客的小妞有:\n");
  31. print(phead);
  32. printf("\n");
  33. } break;
  34. case 'i' :
  35. {
  36. char exit;
  37. do
  38. {
  39. printf("在职的小妞:\n");
  40. print(phead);
  41. printf("你有多少小妞我全收了!\n");
  42. listinsert(phead);
  43. printf("\ne:退出\t任意键继续\n");
  44. exit = getch();
  45. }
  46. while(exit != 'e');
  47. printf("\n感谢您为我们带来新的小妞!\n");
  48. } break;
  49. case 'd' :
  50. {
  51. char exit;
  52. do
  53. {
  54. printf("在职的小妞:");
  55. print(phead);
  56. printf("我们这里有很多小妞,你可以随意的挑选!\n");
  57. listdelete(phead);
  58. printf("\ne:退出\t任意键继续\n");
  59. exit = getch();
  60. }
  61. while(exit != 'e');
  62. printf("\n记得再来哦!\n");
  63. } break;
  64. default : exit(-1);
  65. }
  66. printf("欢迎下次光临!\n\n");
  67. printf("q:quit\tanykey to continue\n\n");
  68. quit = getch();
  69. }while(quit != 'q');
  70. return 0;

  71. }

  72. //创建链表函数
  73. struct node * creat(void)
  74. {
  75. int length; //链表的长度
  76. int variables; //零时变量
  77. int i; //循环变量

  78. struct node * phead = (struct node *)malloc(sizeof(struct node));
  79. //为头指针分配一个没有数据的空间

  80. if(NULL == phead) //如果头指针分配为空,说明内存没有空间了
  81. {
  82. printf("先生,客房已满,欢迎下次光临!");
  83. exit(-1);
  84. }

  85. struct node * ptail = phead; //创建一个尾指针,使头指针的值赋给尾指针
  86. ptail->next = NULL; //尾指针的后继赋空值

  87. printf("你想要几位小妞?\n");
  88. scanf("%d",&length);

  89. for(i = 0; i < length; i++)
  90. {
  91. printf("输入你想要的第%d位小妞的编号: ",i+1);
  92. scanf("%d",&variables);

  93. struct node * pnew = (struct node *)malloc(sizeof(struct node));

  94. if(NULL == pnew)
  95. {
  96. printf("先生,客房已满,欢迎下次光临!");
  97. exit(-1);
  98. }

  99. pnew->data = variables; //把输入的数据给新指针的数据域
  100. ptail->next = pnew; //把新指针的当前地址给尾指针的后继
  101. pnew->next = NULL; //新指针的后继赋空值
  102. ptail = pnew; //尾指针移动到新指针的位置,也就是尾指针向后移
  103. }
  104. return (phead);
  105. }//创建链表函数完成

  106. //链表打印函数
  107. void print(struct node * phead)
  108. {
  109. struct node * p = phead->next; //创建一个p指针指向第一个元素的位置

  110. while(p)
  111. {
  112. printf("%d ",p->data); //输出当前数据
  113. p = p->next; //p指针向后移
  114. }
  115. printf("\n\n");
  116. }//链表打印函数完成

  117. //链表插入函数
  118. void listinsert(struct node * phead)
  119. {
  120. struct node * p = phead; //创建查找指针

  121. int locate=0; //插入位置
  122. int veriables=0; //插入数据
  123. int i=1; //循环变量
  124. struct node * lover = NULL; //建一个情人

  125. printf("先生,你要把新带来的小妞放在哪?\n");
  126. scanf("%d",&locate);

  127. while(p && i<locate) //查找位置
  128. {
  129. p=p->next;
  130. ++i;
  131. }

  132. struct node * insert = (struct node *)malloc(sizeof(struct node)); //创建插入节点

  133. if(NULL == insert)
  134. {
  135. printf("先生,客房已满,欢迎下次光临!");
  136. exit(-1);
  137. }

  138. printf("先生,请为你新带来的这位小姐编号:\n");
  139. scanf("%d",&veriables);

  140. insert->data = veriables; //赋值给插入结点
  141. lover = p->next; //用建立的情人lover保存当前地址的后继地址
  142. p->next = insert; //把插入结点地址给当前指针的后继
  143. insert->next = lover; //把情人lover保存的地址给插入结点的后继
  144. print(phead);
  145. }//插入函数完成

  146. void listdelete(struct node * phead)
  147. {
  148. struct node * p = phead; //创建查找指针p

  149. int locate=0; //要删除的位置
  150. int i=1; //循环变量
  151. int veriables; //保存删除的数据

  152. struct node * deleter = NULL; //创建一个空指针,用于指向要删除的结点

  153. printf("你想要带走第几位小妞?\n");
  154. scanf("%d",&locate);

  155. while(p && i<locate) //查找删除位置
  156. {
  157. p = p->next;
  158. ++i;
  159. }

  160. if(!(p->next) || i>locate)
  161. {
  162. printf("先生你找的小妞不在,请另选一位!");
  163. exit(-1);
  164. }

  165. deleter = p->next; //把p的地址后继的地址给零食指针
  166. p->next = deleter->next ; //删除了p指针后面的一项
  167. veriables = deleter->data; //把删除的数据保存起来
  168. free(deleter);
  169. print(phead);
  170. printf("被你带走的是%d小妞: \n",veriables);
  171. }
复制代码

评分

参与人数 1鱼币 +5 贡献 +5 收起 理由
番茄 + 5 + 5 很给力!

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
 楼主| 发表于 2011-8-14 23:05:43 | 显示全部楼层
  1. #include<stdio.h>
  2. #include<malloc.h>

  3. int listlength=0;

  4. struct node
  5. {
  6.        int data;  //数据域
  7.        struct node *next;  //指针域
  8. };

  9. //函数声明
  10. struct node * creat(void);  //创建链表
  11. void print(struct node *);  //输出链表
  12. void listinsert(struct node *);  //插入元素
  13. void listdelete(struct node *);  //删除元素

  14. //主函数
  15. int main(void)
  16. {
  17.     //在主函数里创建空的头指针
  18.     struct node * phead = NULL;
  19.    
  20.     phead = creat();  //从头指针开始创建链表
  21.     printf("\n");
  22.     char quit;
  23.     do
  24.     {
  25.     char select;
  26.     printf("输入 p:打印 i:插入 d:删除\n");
  27.     select = getch();
  28.     switch(select)
  29.     {
  30.                   case 'p' :
  31.                        {
  32.                            printf("\n当前小妞总数: %d\n当前可以接客的小妞有:\n",listlength);
  33.                            print(phead);
  34.                            printf("\n");
  35.                        } break;
  36.                   case 'i' :
  37.                        {
  38.                            char exit;
  39.                            do
  40.                            {
  41.                                 printf("在职的小妞:\n");
  42.                                 print(phead);
  43.                                 printf("你有多少小妞我全收了!\n");
  44.                                 listinsert(phead);
  45.                                 ++listlength;
  46.                                 printf("当前小妞总数: %d",listlength);
  47.                                 printf("\ne:退出\t任意键继续\n");
  48.                                 exit = getch();
  49.                            }
  50.                            while(exit != 'e');
  51.                            printf("\n感谢您为我们带来新的小妞!\n");
  52.                        } break;
  53.                   case 'd' :
  54.                        {
  55.                            char exit;
  56.                            do
  57.                            {
  58.                                 printf("在职的小妞:");
  59.                                 print(phead);
  60.                                 printf("我们这里有很多小妞,你可以随意的挑选!\n");
  61.                                 listdelete(phead);
  62.                                 --listlength;
  63.                                 printf("当前小妞总数: %d",listlength);
  64.                                 printf("\ne:退出\t任意键继续\n");
  65.                                 exit = getch();
  66.                            }
  67.                            while(exit != 'e');
  68.                            printf("\n记得再来哦!\n");
  69.                        } break;
  70.                   default : exit(-1);
  71.     }
  72.     printf("欢迎下次光临!\n\n");
  73.      printf("q:quit\tanykey to continue\n\n");
  74.      quit = getch();
  75.     }while(quit != 'q');
  76.     return 0;
  77.      
  78. }

  79. //创建链表函数
  80. struct node * creat(void)
  81. {      
  82.        int length;  //链表的长度
  83.        int variables;  //零时变量
  84.        int i;  //循环变量
  85.       
  86.        struct node * phead = (struct node *)malloc(sizeof(struct node));
  87.        //为头指针分配一个没有数据的空间
  88.       
  89.         if(NULL == phead)  //如果头指针分配为空,说明内存没有空间了
  90.         {
  91.                 printf("先生,客房已满,欢迎下次光临!");
  92.                 exit(-1);
  93.         }
  94.         
  95.         struct node * ptail = phead;   //创建一个尾指针,使头指针的值赋给尾指针
  96.         ptail->next = NULL;  //尾指针的后继赋空值
  97.         
  98.         printf("你想要几位小妞?\n");
  99.         scanf("%d",&length);
  100.         listlength = length;
  101.         
  102.         for(i = 0; i < length; i++)
  103.         {
  104.               printf("输入你想要的第%d位小妞的编号: ",i+1);
  105.               scanf("%d",&variables);
  106.               
  107.               struct node * pnew = (struct node *)malloc(sizeof(struct node));
  108.               
  109.               if(NULL == pnew)
  110.               {
  111.                       printf("先生,客房已满,欢迎下次光临!");
  112.                       exit(-1);
  113.               }
  114.               
  115.               pnew->data = variables;  //把输入的数据给新指针的数据域
  116.               ptail->next = pnew;  //把新指针的当前地址给尾指针的后继
  117.               pnew->next = NULL;  //新指针的后继赋空值
  118.               ptail = pnew;  //尾指针移动到新指针的位置,也就是尾指针向后移
  119.         }
  120.         return (phead);
  121. }//创建链表函数完成

  122. //链表打印函数
  123. void print(struct node * phead)
  124. {
  125.       struct node * p = phead->next;  //创建一个p指针指向第一个元素的位置
  126.       
  127.       while(p)
  128.       {
  129.               printf("%d ",p->data); //输出当前数据
  130.               p = p->next;  //p指针向后移
  131.       }
  132.       printf("\n\n");
  133. }//链表打印函数完成

  134. //链表插入函数
  135. void listinsert(struct node * phead)
  136. {
  137.      struct node * p = phead; //创建查找指针
  138.      
  139.      int locate=0;  //插入位置
  140.      int veriables=0;  //插入数据
  141.      int i=1;  //循环变量
  142.      struct node * lover = NULL; //建一个情人
  143.      
  144.      printf("先生,你要把新带来的小妞放在哪?\n");
  145.      scanf("%d",&locate);
  146.      
  147.      while(p && i<locate) //查找位置
  148.      {
  149.               p=p->next;
  150.               ++i;
  151.      }
  152.      
  153.      struct node * insert = (struct node *)malloc(sizeof(struct node)); //创建插入节点
  154.      
  155.      if(NULL == insert)
  156.      {
  157.              printf("先生,客房已满,欢迎下次光临!");
  158.              exit(-1);
  159.      }
  160.      
  161.      printf("先生,请为你新带来的这位小姐编号:\n");
  162.      scanf("%d",&veriables);
  163.      
  164.      insert->data = veriables;  //赋值给插入结点
  165.      lover = p->next;  //用建立的情人lover保存当前地址的后继地址
  166.      p->next = insert;  //把插入结点地址给当前指针的后继
  167.      insert->next = lover;   //把情人lover保存的地址给插入结点的后继
  168.      print(phead);
  169. }//插入函数完成

  170. void listdelete(struct node * phead)
  171. {
  172.      struct node * p = phead;  //创建查找指针p
  173.      
  174.      int locate=0; //要删除的位置
  175.      int i=1;  //循环变量
  176.      int veriables;  //保存删除的数据
  177.      
  178.      struct node * deleter = NULL; //创建一个空指针,用于指向要删除的结点
  179.      
  180.      printf("你想要带走第几位小妞?\n");
  181.      scanf("%d",&locate);
  182.      
  183.      while(p && i<locate)  //查找删除位置
  184.      {
  185.               p = p->next;
  186.               ++i;
  187.      }
  188.      
  189.      if(!(p->next) || i>locate)
  190.      {
  191.                    printf("先生你找的小妞不在,请另选一位!");
  192.                    exit(-1);
  193.      }
  194.      
  195.      deleter = p->next;  //把p的地址后继的地址给零食指针
  196.      p->next = deleter->next ; //删除了p指针后面的一项
  197.      veriables = deleter->data;  //把删除的数据保存起来
  198.      free(deleter);
  199.      print(phead);
  200.      printf("被你带走的是%d小妞: \n",veriables);
  201. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2011-8-15 22:50:41 | 显示全部楼层
好东西,谢谢分享,学习了~~~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2012-5-2 23:18:48 | 显示全部楼层
写struct node好麻烦,用typedef定义,这样不麻烦。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2012-6-16 01:08:27 | 显示全部楼层
有创新啊 学习学习咯 嘿嘿
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2012-7-27 10:13:11 | 显示全部楼层
单链表么?    你是学c版的  数据结构吧~   在链表这里, 你只要注意节点之间的关系的存储和数据存储  好好掌握住,还有就是你这 listdelete  这删除链表的元素这里是通过while 和 if 的配合来确定删除元素的确定的  但是 这样大大增加时间复杂度    有种方法可以大大减少时间复杂度。 希望你想想,  对了别的地方还是有些问题的你在看看, 说的不好的地方,请见谅{:1_1:}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2013-7-1 23:09:33 | 显示全部楼层
看看学习学习
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-20 01:35

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表