求教链表的基础问题
#include "stdafx.h"#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct line)
#define NULL 0
struct line
{
int a;
int b;
int c;
struct line *next;
};
int n;
struct line *creat(void)
{
struct line *head;
struct line *p1,*p2;
n=0;
p1=p2=(struct line *)malloc(LEN);
scanf("%d",&p1->c);
head=NULL;
while(p1->c!=0)
{n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p1=(struct line *)malloc(LEN);
scanf("%d",&p1->c);
}
p2->next=NULL;
return(head);
}
void main()
{ line *h;
h=creat();
while(h!=NULL)
{printf("%d",h->c);
h=h->next;
}
}
为何不能将链表完全输出而是只输出第一个数??
如何才能将链表完全输出?
----------------解决方案--------------------------------------------------------
#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct line)
#define NULL 0
struct line
{
char c;
struct line *next;
};
int n;
struct line *creat(void)
{
struct line *head;
struct line *p1,*p2;
n=0;
p2=head=(struct line *)malloc(LEN);
printf("输入第%d个结点:",n);
scanf("%c",&head->c);
head->next=NULL;
while(p2->c!='0')
{
n=n+1;
p1=(struct line *)malloc(LEN);
p2->next=p1;
p2=p1;
printf("输入第%d个结点:",n);
fflush(stdin);
scanf("%c",&p1->c);
}
p2->next=NULL;
return head;
}
void main()
{ line *h;
h=creat();
while(h!=NULL)
{printf("%c",h->c);
h=h->next;
}
}
----------------解决方案--------------------------------------------------------
为何这两个程序有不同的结果呢?
----------------解决方案--------------------------------------------------------
自己分析啊....
----------------解决方案--------------------------------------------------------
谢了
----------------解决方案--------------------------------------------------------