鱼C论坛

 找回密码
 立即注册
查看: 4540|回复: 5

程序没有出错。。为什么运行出现乱码

[复制链接]
发表于 2013-8-26 15:29:30 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 ck329054222 于 2013-8-26 18:10 编辑

  1. #include<stdio.h>
  2. #include<malloc.h>
  3. #include<stdlib.h>

  4. #define LEN sizeof(struct student)

  5. struct student
  6. {
  7.         int num;
  8.         struct student *next;
  9. };
  10. struct student *creat();         //创建链表
  11. struct student *del(struct student *head,int m);//插入链表操作

  12. void print(struct student *head);//将链表打印输出

  13. int n;                           //全局变量 ,记录链表总共有多少数据

  14. int main(void)
  15. {
  16.         struct student *stu,*p;
  17.         int m;
  18.         stu = creat();
  19.         p = stu;
  20.         print(p);
  21.         
  22.         printf("\n请输入你要删除的学号:");
  23.         
  24.         scanf("%d",&m);
  25.         print(del(p,m));
  26.         
  27.         
  28.         return 0;//exit(0)
  29. }
  30. struct student *creat()
  31. {
  32.         struct student *head;//创建头节点
  33.         struct student *p1,*p2;
  34.         //p1负责创建新节点,p2负责存储节点,每循环一次,指针递增一次(分配了新的内存空间)
  35.         
  36.         p1=p2=(struct student *)malloc(LEN);
  37.         
  38.         printf("请输入学号:");
  39.         scanf("%d",&p1->num);
  40.         
  41.     head = NULL;//
  42.         n = 0;
  43.         while(p1->num)
  44.         {
  45.                 n++;//链表节点个数
  46.                 if(n == 1)
  47.                 {
  48.                         head = p1;
  49.                 }//表示链表只有一个结点
  50.                 else
  51.                 {
  52.                         p2->next = p1;//此处p1为下面创建的新结点
  53.                 }         
  54.                 p2 = p1;//将新结点保存
  55.                 p1=(struct student *)malloc(LEN);//创建下一个结点
  56.                 printf("请输入学号:");
  57.                 scanf("%d",&p1->num);
  58.         }
  59.         
  60.         p2->next = NULL;//链表结束,丢弃p1,p2为最后一个节点
  61.         
  62.         return head;
  63. }

  64. void print(struct student *head)
  65. {
  66.         struct student *p;
  67.         printf("\n总共有%d个学生\n",n);
  68.         
  69.         p=head;
  70.         if(head)                                                                        //判断头是否为空指针
  71.         {
  72.                 while(p)                                                                //判断至最后一个结点为空
  73.                 {
  74.                         printf("学号:%d\n",p->num);
  75.                         p = p->next;                                                //下次循环指向下一个节点
  76.                 }
  77.         }         
  78. }

  79. struct student *del(struct student *head,int m)
  80. {
  81.         struct student *p1,*p2;
  82.         p1 = head;
  83.         if(head)
  84.         {
  85.                 while( p1->num != m)
  86.                 {
  87.                         p2 = p1;
  88.                         p1 = p1->next;
  89.                 }
  90.                 if(p1 == NULL)
  91.                         printf("没有找到结点\n");
  92.                 else
  93.                 {
  94.                         if(p1 == head)
  95.                         head = p1->next;
  96.                         else
  97.                         p2->next = p1->next;
  98.                 }
  99.                 n = n-1;
  100.         }
  101.         return head;
  102. }



  103. 是编码问题。将UTF-8改为ANSI就可以了
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-8-26 15:36:38 | 显示全部楼层
鱼油啊,会调试吗?不会联系我,我教你!这种问题一定要学会自己调试,否则不可能有进步!!!

我qq 273282378
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2013-8-26 16:46:18 | 显示全部楼层
1.png
运行没有问题。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-8-26 16:54:25 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2013-8-27 00:06:26 | 显示全部楼层
现在还看不懂
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2013-8-27 00:31:51 | 显示全部楼层
ck329054222 发表于 2013-8-26 16:54
我这里显示乱码。。

同一段程序居然有不一样的运行结果,奇怪——
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-5 18:09

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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