| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 625 人关注过本帖, 1 人收藏
标题:单链表 为什么输入成绩 按Enter无法正确输出
只看楼主 加入收藏
xiabuluren
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2012-3-25
结帖率:100%
收藏(1)
 问题点数:0 回复次数:7 
单链表 为什么输入成绩 按Enter无法正确输出

/* Note:Your choice is C IDE */
#include"stdio.h"
#include"malloc.h"
#define LEN sizeof(struct student)
struct student
{
    long num;
    float score;
    struct student*next;
};
int n;

int main()
{
    struct student*creat();
    struct student*del(struct student*,long);
    struct student*insert(struct student*,struct student*);
    void print(struct student*);
    struct student*head,stu;
    long del_num;
    printf("input records:\n");
    head=creat();
    print(head);
    printf("input the deleted number:");
    scanf("%1d",&del_num);
    head=del(head,del_num);
    print(head);
    printf("input the inserted record:");
    scanf("%1d,%f",&stu.num,&stu.score);
    head=insert(head,&stu);
    print(head);
    return 0;
}
struct student*creat()
{
    struct student*head;
    struct student*p1,*p2;
    n=0;
    p1=p2=(struct student*)malloc(LEN);
    scanf("%1d,%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("%1d,%f",&p1->num,&p1->score);
        
    }
    p2->next=NULL;
    return(head);
   
}
struct student*del(struct student*head,long num)
{
    struct student*p1,*p2;
    if(head==NULL)
    {
        printf("\nlist null!\n");
        return (head);
    }
    p1=head;
    while(num!=p1->num&&p1->next!=NULL)
    {
        p2=p1;p1=p1->next;
        
    }
    if(num==p1->num)
    {
        if(p1==head)head=p1->next;
        else p2->next=p1->next;
        printf("delete:%1d\n",num);
        n=n-1;
    }
    else printf("%1d not been found!\n",num);
    return (head);
   
}
struct student*insert(struct student*head,struct student*stud)
{
    struct student*p0,*p1,*p2;
    p1=head;
    p0=stud;
    if(head==NULL)
    {
        head=p0;p0->next=NULL;
    }
    else
    {
        while((p0->num>p1->num)&&(p1->next!=NULL))
        
        {
            p2=p1;
            p1=p1->next;
            
        }
        if(p0->num<=p1->num)
        {
            if(head==p1)head=p0;
            else p2->next=p0;
            p0->next=p1;
        }
        else
        {
            p1->next=p0;p0->next=NULL;
        }
        n=n+1;
        return(head);
    }
    }
  void print(struct student*head)
  {
      struct student*p;
      printf("\n Now,These %d records are:",n);
      p=head;
      if(head!=NULL)
      do
      {printf("%1d,%5.1f",p->num,p->score);
       p=p->next;
      
         
         
         
         
          }while(p!=NULL);
      
      
      
      
      
  }
 





搜索更多相关主题的帖子: head long include records choice 
2012-04-12 15:46
ZJQLOVELYY
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:105
专家分:166
注 册:2011-8-1
收藏
得分:0 
首先说下你的del函数,那个并无删除结点,只是简单的舍弃,还有,我手机看的,不知那个scanf里面的%ld 中那个是字母l还是数字1
2012-04-13 11:11
linkang1024
Rank: 2
等 级:论坛游民
帖 子:90
专家分:83
注 册:2011-10-10
收藏
得分:0 
你敢不敢定些 注释下去、太乱了、都没心情看了、
2012-04-13 12:20
xiabuluren
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2012-3-25
收藏
得分:0 
回复 2楼 ZJQLOVELYY
是1不是L。
2012-04-13 22:06
我菜119
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
帖 子:938
专家分:1756
注 册:2009-10-17
收藏
得分:0 
把代码一粘,然后就说不能读取,你对自己都不负责,谁还会对你负责?

愿用余生致力编程
2012-04-13 22:15
xiabuluren
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2012-3-25
收藏
得分:0 
回复 5楼 我菜119
能运行,输入之后是错误的。
2012-04-14 21:37
ZJQLOVELYY
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:105
专家分:166
注 册:2011-8-1
收藏
得分:0 
回复 4楼 xiabuluren
额,那你在那里就错了,,
2012-04-16 00:32
xiabuluren
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2012-3-25
收藏
得分:0 
自己找到错误了。
2012-04-17 21:18
快速回复:单链表 为什么输入成绩 按Enter无法正确输出
数据加载中...
 
   



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

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