我是新的c编程,我的代码工作,但我的问题是,如果我在主函数中声明struct node *a,*b;
,如何将a
和b
传递到void create()
。并且为什么它不工作,有人可以帮我理解它吗?如何在c中的main函数中声明struct变量?
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
int d;
struct node *next;
}*start=NULL;struct node *a,*b; //move this part to main function -> struct node *a,*b; but its not working
void create()
{
a=(struct node *)malloc(sizeof(struct node));
printf("Enter the data : ");
scanf("%d",&a->d);
a->next=NULL;
if(start==NULL)
{
start=a;
b=a;
}
else
{
b->next=a;
b=a;
}
}
void display()
{
struct node *a;
printf("\nThe Linked List : ");
a=start;
while(a!=NULL)
{
printf("%d--->",a->d);
a=a->next;
}
printf("NULL\n");
}
void main()
{
char ch;
do
{
create();
printf("Do you want to create another : ");
ch=getche();
}
while(ch!='n');
display();
}
void freenodes()
{
struct node *a;
a = start;
while(a != NULL)
{
struct node *freenode = a ;
a = a->next;
free(freenode) ;
}
}
请定义你的 “不工作” 的意思。我们无法读懂你的想法。 –
什么“不工作”? –
也许读一些C教程? – zoska