| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 612 人关注过本帖
标题:输入的数与输出的数不一样,why?
只看楼主 加入收藏
evil_evil
Rank: 1
等 级:新手上路
帖 子:38
专家分:0
注 册:2006-3-4
收藏
 问题点数:0 回复次数:2 
输入的数与输出的数不一样,why?

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<conio.h>
#define ERROR 1
typedef struct linknode /*结点类型*/
{
double data;
struct linknode *next;
}lnode;

typedef struct /*链表类型*/
{
lnode *head,*tail; /*头尾指针*/
int len;
}linklist;

int creat(linklist *l) /*创建一个长度为n(不计头结点)带头结点的链表*/
{ /*n==0,链表为空*/
lnode *p,*q;int i,n;
printf("input the length of linklist when length is 0 the linklist is empty: ");
scanf("%d",&n);
p=(lnode *)malloc(sizeof(lnode));
if(p==NULL)
return ERROR;
else
{
l->head=p;q=p;
for(i=0;i<n;i++)
{
p=(lnode *)malloc(sizeof(lnode));
if(p==NULL)
return ERROR;
else
{
printf("please input the %dth data:",i);
scanf("%f",&(p->data));
printf("%f",p->data);
q->next=p;q=p;
}
}
p->next=NULL;l->len=n;l->tail=p;
}
}

void printlinklist(linklist *l) /*打印链表元素*/
{
int i;
lnode *p;
p=(l->head)->next;
for(i=0;i<l->len;i++,p=p->next)
printf("%f\n",p->data);
}

main()
{
linklist *l;
l=(linklist *)malloc(sizeof(linklist));
creat(l);
printlinklist(l);
getch();
}


输入的数与输出的数不一样

搜索更多相关主题的帖子: why 链表 结点 include struct 
2006-04-22 12:59
lj_860603
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:714
专家分:0
注 册:2006-1-25
收藏
得分:0 
以下是引用evil_evil在2006-4-22 12:59:00的发言:

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<conio.h>
#define ERROR 1
typedef struct linknode /*结点类型*/
{
double data; //应该t定义为float型
struct linknode *next;
}lnode;

typedef struct /*链表类型*/
{
lnode *head,*tail; /*头尾指针*/
int len;
}linklist;

int creat(linklist *l) /*创建一个长度为n(不计头结点)带头结点的链表*/
{ /*n==0,链表为空*/
lnode *p,*q;int i,n;
printf("input the length of linklist when length is 0 the linklist is empty: ");
scanf("%d",&n);
p=(lnode *)malloc(sizeof(lnode));
if(p==NULL)
return ERROR;
else
{
l->head=p;q=p;
for(i=0;i<n;i++)
{
p=(lnode *)malloc(sizeof(lnode));
if(p==NULL)
return ERROR;
else
{
printf("please input the %dth data:",i);
scanf("%f",&(p->data));//输入输出都是%f
printf("%f",p->data);
q->next=p;q=p;
}
}
p->next=NULL;l->len=n;l->tail=p;
}
}

void printlinklist(linklist *l) /*打印链表元素*/
{
int i;
lnode *p;
p=(l->head)->next;
for(i=0;i<l->len;i++,p=p->next)
printf("%f\n",p->data);
}

main()
{
linklist *l;
l=(linklist *)malloc(sizeof(linklist));
creat(l);
printlinklist(l);
getch();
}


输入的数与输出的数不一样


我的原则很简单:不做不喜欢的事!
2006-04-22 13:24
evil_evil
Rank: 1
等 级:新手上路
帖 子:38
专家分:0
注 册:2006-3-4
收藏
得分:0 
thank you解决了!!

潜水员!
2006-04-22 13:33
快速回复:输入的数与输出的数不一样,why?
数据加载中...
 
   



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

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