免费注册 查看新帖 |

Chinaunix

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

[C] 求解程序 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-09-18 13:59 |只看该作者 |倒序浏览
#include<stdio.h>
#include<malloc.h>
//#define NULL 0
#define LEN sizeof(struct stu)

struct stu
{
long  num;
char  name[20];
float score;
struct stu *next;
};

int n;

/***********〃*******创建链表*******/


struct stu *creat(void)
{
struct stu *head;
struct stu *p1,*p2;
n=0;
p1=p2=(struct stu*)malloc(LEN);
scanf("%ld %s %f",&p1->num,&p1->name,&p1->score);
head=NULL;
while(p1->num!=0)
{
  n=n+1;
  if(n==1)
   head=p1;
  else
   p1=(struct stu*)malloc(LEN);
   scanf("%ld %s %f",&p1->num,&p1->name,&p1->score);
   p2->next=p1;
   p2=p1;
//   printf("%ld%s%5.1f\n",p1->num,p1->name,p1->score);
}

//free(p1);
n=n-1;
p2->next=NULL;
return(head);

}

/***********输出链表***********/

void print(struct stu *head)
{
  struct stu *p;
  printf("\nNow,These %d records are:\n",n);
  p=head;
//  if(head!=NULL)
  while(p!=NULL)
    {
      printf("%s        %ld        %5.1f\n",p->name,p->num,p->score);
      p=p->next;
    }
}


/************删除节点**************/

struct stu *del(struct stu *head,long num)
{
struct stu *p1,*p2;
if(head==NULL)
  { printf("\nlist is null!");    goto end;
  }
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("\ndelete:%ld\n",num);
   n=n-1;/* */
  }
else printf("\n%ld not been found!\n",num);
end:
return(head);
}

/************插入节点**********/

struct stu *insert(struct stu *head,struct stu *stud)
{
struct stu *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 main()
{
  struct stu *head,*stus;
  long del_num;
  printf("input records:\n");
  head=creat();
  print(head);
  printf("\ninput the deleted number:\n");
  scanf("%ld",&del_num);
  while(del_num!=0)
   {
    head=del(head,del_num);
    print(head);
    printf("input the deleted number:\n");
    scanf("%ld",&del_num);
   }

  printf("\ninput the inserted record:");
  stus=(struct stu *)malloc(LEN);
  scanf("%ld %s %f",&stus->num,&stus->name,&stus->score);
  while(stus->num!=0)
   {
    head=insert(head,stus);
    print(head);
    printf("\ninput the inserted record:\n");
    stus=(struct stu *)malloc(LEN);
    scanf("%ld %s %f",&stus->num,&stus->name,&stus->score);
   }
print(head);
}

运行后
[root@dale dz_1]# ./list_1
input records:
1 dz 88
2 mm 99
3 nn 92
4 ff 94
0 none 0

Now,These 3 records are:
mm      2        99.0
nn      3        92.0
ff      4        94.0
none    0         0.0


为什么输入四个有效地数 输出三个 无效的0号也输出来了
求高手指点
还有指点一下free函数的应用

论坛徽章:
2
程序设计版块每日发帖之星
日期:2015-06-17 22:20:00每日论坛发贴之星
日期:2015-06-17 22:20:00
2 [报告]
发表于 2011-09-18 14:16 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
3 [报告]
发表于 2011-09-18 15:06 |只看该作者
#include
#include
//#define NULL 0
#define LEN sizeof(struct stu)

struct stu
{
long  num;
c ...
linux_dale 发表于 2011-09-18 13:59



    先把排版弄好。太乱看着就不想看了。

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
4 [报告]
发表于 2011-09-18 15:07 |只看该作者
回复 1# linux_dale


    這麼多人喜歡用 root 啊?

论坛徽章:
2
程序设计版块每日发帖之星
日期:2015-06-17 22:20:00每日论坛发贴之星
日期:2015-06-17 22:20:00
5 [报告]
发表于 2011-09-18 15:09 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP