| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 253 人关注过本帖
标题:关于链表问题.
只看楼主 加入收藏
wawyt962464
Rank: 1
来 自:广东佛山
等 级:新手上路
帖 子:8
专家分:9
注 册:2011-4-3
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:1 
关于链表问题.
各位高人帮个尽快,我找了很久都不知道问题出在那里??? 我不怎么懂.....
  
程序代码:
#include<stdio.h>
#define NULL 0
struct stu
{ int num;
  int age;
  struct stu *next;
};
struct stu *creat(int n)
{ struct stu *head,*pf,*pd;
  int i;
  for(i=0;i<n;i++)
  { pd=(struct stu*)malloc(sizeof(struct stu));
    printf("input number and age:\n");
    scanf("%d%d",&pd->num,&pd->age);
    if(i==0){
    head=pd;
    pf=head;}
    else
    pf->next=pd;
    pd->next=NULL;
    pf=pd;
  }
  head->data=n;
  return head;
}
void main()
{ struct stu *student;
  int i,count=6;
  student=creat(count);
  printf("num:\t   age:\t\n");
  for(i=0;i<6;i++){
    printf("%d  \t\t%d\n",student->num,student->age);
  }
}
2011-04-26 23:51
boxinchao
Rank: 4
等 级:业余侠客
帖 子:51
专家分:231
注 册:2011-4-13
收藏
得分:20 
程序代码:
#include <stdio.h>
#include <stdlib.h>      /* malloc函数需要包含头文件 */

#define NULL 0
struct stu
{ int num;
  int age;
  struct stu *next;
};
struct stu *creat(int n)
{ struct stu *head,*pf,*pd;
  int i;
  for(i=0;i<n;i++)
  { pd=(struct stu*)malloc(sizeof(struct stu));
    printf("input number and age:");
    scanf("%d%d",&pd->num,&pd->age);

    if(i==0){
    head=pd;
    pf=head;}
    else           /* else后面不止一条语句,必须带大括号 */
    {
    pf->next=pd;
    pd->next=NULL;
    pf=pd;
    }
  }
#if 0
  head->data=n;    /* 你的结构体里并没有data这个元素' */
#endif
  return head;
}
void main()
{ struct stu *student;
  int i,count=2;
  student=creat(count);
  printf("num:\t   age:\t\n");
  for(i=0;i<2;i++){
    printf("%d  \t\t%d\n",student->num,student->age);
    student = student->next;      /* 移到下一个节点 */
  }
}

修改了一点,顺便说下你的排版有点乱,基本功能实现了,我把count修改为2做了验证,打印如下:
input number and age:12
12
input number and age:23
23
num:       age:
12              12
23              23
Press any key to continue
2011-04-27 00:10
快速回复:关于链表问题.
数据加载中...
 
   



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

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