|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
请问为什么输入有问题?
#include<stdio.h>
#include<stdlib.h>
#define NULL 0
#define LEN sizeof(struct nb)
int n;
struct nb
{
long xh;
float cj;
struct nb *next;
};
struct nb *p()
{
n=0;
struct nb *head,*p1,*p2;
p1=p2=(struct nb *)malloc(LEN);
head=NULL;
scanf("%d,%f",&p1->xh,&p1->cj);
while(p1->xh)
{
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct nb *)malloc(LEN);
scanf("%d,%f",p1->xh,p1->cj);
}
p2->next=NULL;
return head;
}
void sc(struct nb *head)
{
struct nb *p;
p=head;
if(head)
{
do
{
printf("%d %f",p->xh,p->cj);
p=p->next;
}while(p);
}
}
void main()
{
struct nb *b;
b=p();
sc(b);
}
问题多多
- p1=(struct nb *)malloc(LEN);
- scanf("%d,%f",p1->xh,p1->cj);
复制代码
这里要用 - scanf("%d,%f",&p1->xh,&p1->cj);
复制代码
scanf函数的引号里面的逗号输入的时候要输
- void main()
- {
- struct nb *b;
- b=p();
- sc(b);
- }
复制代码
这里的函数p()的返回值是在p里面定义的,调用完就被释放了,不能这样做 ,要在main里面定义,然后在p函数里用二维指针修改
还有,链表用完了要释放内存,加了一个release函数
这是我改完的
- #include<stdio.h>
- #include<stdlib.h>
- #define LEN sizeof(struct nb)
- struct nb
- {
- long xh;
- float cj;
- struct nb *next;
- };
- void p(struct nb **head)
- {
- struct nb *p1,*p2;
- p1=p2=(struct nb *)malloc(LEN);
- scanf("%d%f",&p1->xh,&p1->cj);
- while(p1->xh)//第一个数字输入0时结束
- {
- if((*head)==NULL)
- (*head)=p1;
- else
- p2->next=p1;
-
- p2=p1;
- p1=(struct nb *)malloc(LEN);
- scanf("%d%f",&p1->xh,&p1->cj);
- }
- p2->next=NULL;
- }
- void sc(struct nb *head)
- {
- struct nb *p;
- p=head;
- if(head!=NULL)
- {
- do
- {
- printf("%d %f\n",p->xh,p->cj);
- p=p->next;
- }
- while(p!=NULL);
- }
- }
- void release(struct nb **head)
- {
- struct nb *temp;
- while((*head)!=NULL)
- {
- temp=*head;
- *head=(*head)->next;
- free(temp);
- }
- }
- int main(void)
- {
- struct nb *head=NULL;
-
- p(&head);
- sc(head);
- release(&head);
-
- return 0;
- }
复制代码
|
|