免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1595 | 回复: 5
打印 上一主题 下一主题

[C] 我输入数据,总是得不到正确的结果,哪位数据结构或者C语言高手能帮帮我啊! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-10-21 22:05 |只看该作者 |倒序浏览
#include<malloc.h>
#define NULL 0
#define LEN  sizeof(struct student )
int n;
struct student
{long num;
float score;
struct student *next;
};
struct student *creat()        /*链表的创建*/
{struct student *p1,*p2;
struct student *head;
n=0;
p1=p2=(struct student *)malloc(LEN);
scanf("%ld,%f",&p1->num,&p1->score);
head=NULL;
while(p1->num!=0)
{n=n+1;
  if(n==1)head=p1;
  else p2->next=p1;
  p2=p1;
  p1=(struct student *)malloc(LEN);
  scanf("%ld,%f",&p1->num,&p1->score);
}
p2->next=NULL;
return(head);
}
void print(struct student *head)   /*链表的输出*/   
{struct student *p;
printf("\nNow,These %d record are:\n",n);
p=head;
if(head!=NULL)
  do
    {printf("%ld,%5.1f\n",p->num,p->score);
     p=p->next;
   }while(p!=NULL);
}
struct student *insert(struct student *head,struct student *q,long num)      /*链表的后插入*/
{struct student *p0,*p1,*p2;
p1=head;p0=q;
while((p1->num!=num)&&(p1!=NULL))
   {p2=p1;p1=p1->next;}
if(p1==head){p0->next=head->next;head->next=p0;}
else{p2->next=p0;p0->next=p1;}
return(head);
}
struct student *delete(struct student *head,struct student *s)   /*链表的删除*/
{struct student *p1,*p2;
p1=head;
while((p1->num!=s->num)&&(p1!=NULL))
{p2=p1;p1=p1->next;}
if(p1==NULL)printf("The List null!\n");
else p2->next=p1->next;
return(head);
}
main()
{struct student *head,*q;
struct student *s;
long ins_num;
q=(struct student*)malloc(LEN);
scanf("%ld,%f",q->num,q->score);
head=creat();
print(head);
printf("please input the ins_num:\n");
scanf("%ld",&ins_num);
head=insert(head,q,ins_num);
print(head);
printf("please input the *s:\n");
scanf("%ld",s->num);/*输入要删除的号码*/
head=delete(head,s);
print(head);
}








[ 本帖最后由 niqiumao 于 2006-10-23 22:56 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2006-10-23 00:42 |只看该作者
兄台你的程序好难改啊~!改的我吐血.(可能是偶水平烂)
建议以后用带头结点的链表!还有注意写代码的格式


  1. #include <malloc.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #define LEN  sizeof(struct student)

  5. int n;
  6. struct student
  7. {
  8.         long num;
  9.         float score;
  10.         struct student *next;
  11. };

  12. /*            creat()        */
  13. struct student *creat()
  14. {
  15.         struct student *p1,*p2;
  16.         struct student *head;

  17.         n=0;
  18.         p1=(struct student *)malloc(LEN);
  19.         p2 = p1;
  20.         printf("Input num and score:");
  21.         scanf("%ld,%f",&p1->num,&(p1->score));
  22.         head=NULL;

  23.         while (p1->num != 0)
  24.         {
  25.                 printf("num=%ld,score=%f\n",p1->num,p1->score);
  26.                 n=n+1;
  27.                 if(n == 1) head = p1;
  28.                 else p2->next = p1;
  29.                 p2 = p1;
  30.                 p1=(struct student *)malloc(LEN);
  31.                 printf("Input num and score:");
  32.                 scanf("%ld,%f",&p1->num,&p1->score);
  33.         }

  34.         p2->next=NULL;
  35.         return(head);
  36. }

  37. /*      print()      */
  38. void print(struct student *head)
  39. {
  40.         struct student *p;
  41.         printf("\nNow,These %d record are:\n",n);
  42.         p = head;

  43.         if(head!=NULL)
  44.         {
  45.                 do
  46.                 {
  47.                         printf("%ld,%5.1f\n",p->num, p->score);
  48.                         p=p->next;
  49.                 }while(p!=NULL);
  50.         }
  51. }

  52. /*      insert()     */
  53. struct student *insert(struct student *head,int i)
  54. {
  55.         struct student *p1=head,*p;
  56.         struct student *q = NULL;
  57.         int k = 1;

  58.         q = (struct student *)malloc(sizeof(struct student));
  59.         if (!q) printf("OVERFLOW!"),exit(0);
  60.         printf("Input num and score:");
  61.         scanf("%ld,%f", &q->num, &q->score);

  62.         for (; k<i&&p1; k++)
  63.         {
  64.                 p1 = p1->next;
  65.         }
  66.         if (!p1 && i<1) printf("input error!\n"),exit(0);

  67.         q->next = p1->next;
  68.         p1->next = q;

  69.         return head;
  70. }

  71. struct student *delete(struct student *head, int num)
  72. {
  73.         struct student *p1,*p2;
  74.         p1=head;

  75.         while((p1->num != num) && (p1!=NULL))
  76.         {
  77.                 p2=p1;
  78.                 p1=p1->next;
  79.         }

  80.         if (!p1) printf("The List null!\n"),exit(0);
  81.         else p2->next = p1->next;

  82.         return(head);
  83. }

  84. int main()
  85. {
  86.         struct student *head,*q;
  87.         long del_num;
  88.         int ins_location;

  89.         head=creat();
  90.         print(head);

  91.         printf("Input the ins_location:");
  92.         scanf("%d",&ins_location);
  93.         head = insert(head, ins_location);
  94.         print(head);

  95.         printf("delete:please input the num:");
  96.         scanf("%ld",&del_num);/*   ????????*/
  97.         head=delete(head,del_num);
  98.         print(head);
  99.         return 0;
  100. }
复制代码

论坛徽章:
0
3 [报告]
发表于 2006-10-23 12:35 |只看该作者
嘻嘻,我还是学生嘛,刚刚学C,有不到之处还请指教,谢谢你的帮助!

论坛徽章:
0
4 [报告]
发表于 2006-10-23 12:59 |只看该作者

回复 2楼 qqq112233g 的帖子

To: 2  楼

你的 delete 函数中,是否应增加一句: free(p1) ?  否则会有内存泄漏.

struct student *delete(struct student *head, int num)
{
        struct student *p1,*p2;
        p1=head;

        while((p1->num != num) && (p1!=NULL))
        {
                p2=p1;
                p1=p1->next;
        }

        if (!p1)  
        {
             printf("The List null!\n"),exit(0);
        }
        else
        {
              p2->next = p1->next;
              free(p1);                       //这里
         }

        return(head);
}

论坛徽章:
0
5 [报告]
发表于 2006-10-23 13:17 |只看该作者
原帖由 老蔡 于 2006-10-23 12:59 发表
To: 2  楼

你的 delete 函数中,是否应增加一句: free(p1) ?  否则会有内存泄漏.

struct student *delete(struct student *head, int num)
{
        struct student *p1,*p2;
        p1=head;

      ...


你加上去你去看看对不对?

论坛徽章:
0
6 [报告]
发表于 2006-10-23 13:58 |只看该作者
呵呵,多谢楼上细心人指正,的确会!
我一开始只是打算改的能出个结果,有很多地方还应该完善的!
我也是个学生,和一楼的共同加油~!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP