| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 288 人关注过本帖
标题:链表找高手…………………………
只看楼主 加入收藏
defhvk
Rank: 2
等 级:论坛游民
帖 子:62
专家分:29
注 册:2011-4-8
结帖率:93.33%
收藏
 问题点数:0 回复次数:1 
链表找高手…………………………
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student
{
  long num;
  char name[20];
  float score;
  struct student *next;
};
struct student *x();
void main()
{
 struct student *p;
 p=x();
 while(p->num)
 {
     printf("%ld  %s  %f",p->num,p->name,p->score);
     p=p->next;
 }
}

struct student *x()
{
    int size;
  struct student *head,*tail,*p;
  head=tail=NULL;
  size=sizeof(struct student);
  p=(struct student *)malloc(size);
  p->next=NULL;
  long num;
  char name[20];
  float score;
  printf("请输入学生信息:\n");
  scanf("%ld%s%f",&num,name,&score);
  while(num){
  if(p!=NULL){
      p->num=num;
      strcpy(p->name,name);
      p->score=score;
  }
  else
  {
    printf("error!");
    break;
  }
  if(head==NULL){
      head=p;
  }
  else
  {
      tail->next=p;
      tail=p;
  }
  printf("请再输入学生信息,学号0将结束:\n");
  scanf("%ld",&num);
  if(num==0)
     continue;
  else
  {
    scanf("%s%f",name,&score);
  }
  p=(struct student *)malloc(size);
 }
  return head;
}
搜索更多相关主题的帖子: include 
2011-05-01 21:55
defhvk
Rank: 2
等 级:论坛游民
帖 子:62
专家分:29
注 册:2011-4-8
收藏
得分:0 
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student
{
  long num;
  char name[20];
  float score;
  struct student *next;
};
struct student *x();
void main()
{
 struct student *p;
 p=x();
 while(p->num)
 {
     printf("%ld  %s  %f",p->num,p->name,p->score);
     p=p->next;
 }
}

struct student *x()
{
    int size;
  struct student *head,*tail,*p;
  head=tail=NULL;
  size=sizeof(struct student);
  p=(struct student *)malloc(size);
  p->next=NULL;
  long num;
  char name[20];
  float score;
  printf("请输入学生信息:\n");
  scanf("%ld%s%f",&num,name,&score);//先把数据保存到变量中.
  while(num){
  if(p!=NULL){
      p->num=num;
      strcpy(p->name,name);/*如果申请成功,给结构成员赋值*/
      p->score=score;
  }
  else
  {
    printf("error!");
    break;
  }
  if(head==NULL){//是否是新建链表。
      head=p;
  }
  else
  {
      tail->next=p;//建结点。
      tail=p;
  }
  printf("请再输入学生信息,学号0将结束:\n");
  scanf("%ld",&num);
  if(num==0)
     continue;
  else
  {
    scanf("%s%f",name,&score);
  }
  p=(struct student *)malloc(size);
 }
  return head;
}
2011-05-01 22:02
快速回复:链表找高手…………………………
数据加载中...
 
   



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

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