| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 523 人关注过本帖
标题:刚学完链表,编了一个程序,为什么运行的那么慢呢?
只看楼主 加入收藏
xdh0817
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:193
专家分:195
注 册:2011-10-20
结帖率:92.86%
收藏
已结贴  问题点数:10 回复次数:3 
刚学完链表,编了一个程序,为什么运行的那么慢呢?
#include<stdio.h>
#include<malloc.h>
#define len sizeof(struct student)
struct student
{ long num;
    float score;
    struct student *next;
};
struct student *create(void)
{   struct student *p1,*p2,*head;
    int n=0;
    p1=p2=(struct student *)malloc(len);
    //scanf("%ld,%f",&p1->num,&p1->score);
        printf("请输入学号:");
        scanf("%ld",&p1->num);
        printf("请输入成绩:");
        scanf("%f",&p1->score);
    head=NULL;
    while(p1->num!=0)
    {   n++;
        if(n==1) head=p1;
        else p2->next=p1;
        p2=p1;
        p1=(struct student *)malloc(len);
        //scanf("%ld,%f",&p1->num,&p1->score);
        printf("请输入学号:");
        scanf("%ld",&p1->num);
        if(p1->num!=0)
        {
            printf("请输入成绩:");
            scanf("%f",&p1->score);
        }
    }
    p2->next=NULL;
    return(head);
}
void output(struct student *head)
{   struct student *p;
     p=head;
     if(head!=NULL)
     do
    {  printf("%ld  %.0f\n",p->num,p->score);
       p=p->next;
     }while(p!=NULL);
}
struct student *shanchu(struct student *head,long n)
{
    struct student *p1,*p2;
    if(head==NULL) printf("错误");
    p1=head;
    while(p1->num!=n&&p1->next!=NULL)
    { p2=p1;  p1=p1->next;}
     if(n==p1->num)
     { if(head==p1) head=p1->next;
          else p2->next=p1->next;
     }
    else printf("not found\n");
    return(head);
}
struct student *add(struct student *head,struct student *stu)
{   struct student *p1,*p2,*p0;
    p1=head;p2=stu;
    if(p1==NULL) { head=p2;p2->next=NULL;}
    else {
           while(p2->num>p1->num&&p1->next!=NULL)
            {p0=p1;p1=p1->next;}
          if(p2->num<=p1->num)
             {
                 if(head==p1)     {head=p2; p2->next=p1;}
                      else           {p0->next=p2; p2->next=p1;  }
             }
            else {p1->next=p2; p2->next=NULL;  }
          }
    return(head);
}
void main()
{ long n;
  struct student *p,*head,*p3;
    printf("请输入数据(输入0可以结束):\n");   
    head=create();
    p=head;
    printf("输出数据:\n");
    output(p);
    printf("请输入要删除的学号:");
    scanf("%ld",&n);
    while(n!=0)
    {  head=(struct student *)shanchu(p,n);
        p=head;
        printf("请输入要删除的学号:");
        scanf("%ld",&n);
        }
    printf("显示数据:\n");
    output(p);
    printf("请输入要添加的学生及其成绩:\n");
    p3=(struct student *)malloc(len);
    //scanf("%ld,%f",&p3->num,&p3->score);
        printf("请输入学号:");
        scanf("%ld",&p3->num);
        printf("请输入成绩:");
        scanf("%f",&p3->score);
    while(p3->num!=0)
    {   head=(struct student *)add(p,p3);
        p=head;
        p3=(struct student *)malloc(len);
        printf("请输入要添加的学生及其成绩:\n");
        //scanf("%ld,%f",&p3->num,&p3->score);
        printf("请输入学号:");
        scanf("%ld",&p3->num);
        if(p3->num!=0)
        {
            printf("请输入成绩:");
            scanf("%f",&p3->score);
        }
    }
    output(p);
}
搜索更多相关主题的帖子: include create 
2011-11-15 22:54
hellovfp
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:禁止访问
威 望:30
帖 子:2976
专家分:7697
注 册:2009-7-21
收藏
得分:10 
慢?哪里慢了?很快啊。

我们都在路上。。。。。
2011-11-16 11:29
xdh0817
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:193
专家分:195
注 册:2011-10-20
收藏
得分:0 
回复 2楼 hellovfp
大概3秒才跑起来
2011-11-16 12:48
hellovfp
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:禁止访问
威 望:30
帖 子:2976
专家分:7697
注 册:2009-7-21
收藏
得分:0 
我这里运行很快,没有任何停顿。。。0秒启动。
你的程序被你的扫毒程序给光顾了吧?

我们都在路上。。。。。
2011-11-16 13:39
快速回复:刚学完链表,编了一个程序,为什么运行的那么慢呢?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017803 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved