#include<stdio.h>
typedef struct node
{
int value;
struct node *b;
}
node;
node *makelist(int n);
void printlist(node *t);
int main()
{
int n=10;
printf("what's the lenth?");
scanf("%d",&n);
node *head=NULL;
head=makelist(n);
printlist(head);
return 0;
}
struct node *makelist(int n)
{
node *head=NULL;
node list[20];
for(int i=0;i<n-1;i++)
{
printf("what's the number:?");
scanf("%d",&(list[i].value));
list[i].b=&(list[i+1]);
}
list[n-1].value=10;
printf("%d",list[n-1].value);
list[n-1].b=NULL;
head=&(list[0]);
return head;
}
void printlist(struct node *t)
{
node *p=NULL;
p=t;
while(p->b!=NULL)
{
printf("%d ",p->value);
p=p->b;
}
printf("%d ",p->value);
}
输入1到9,输出的数大的离谱;
初学链表,这样做错哪了(清楚可用指针做,但不懂这种做法错因)