#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int num;
struct node *next;
}LN;
LN *Creat();
LN Divide(LN *h);
void Printf(LN *s);
void main()
{
LN *h;
h=Creat();
Divide(h);
printf("The even term is: ");
Printf(h);
printf("The odd term is: ");
Printf(q);
}
LN *Creat()
{
LN *p;
LN *b;
LN *h;
int a;
h=(LN *)malloc(sizeof(LN));
h->next=NULL;
b=h;
printf("Input the sum of the numbers: ");
scanf("%d",&a);
for(int i=0;i<a;i++)
{
printf("Input the %d number: ",i+1);
p=(LN *)malloc(sizeof(LN));
scanf("%d",&p->num);
b->next=p;
b=p;
}
p->next=NULL;
system("CLS");
return h;
}
LN Divide(LN *h)
{
LN *q;
LN *b;
q=(LN *)malloc(sizeof(LN));
b=q;
while(h!=NULL&&h->next!=NULL)
{
b=h->next;
h->next=b->next;
h=h->next;
}
return *q;
}
void Printf(LN *s)
{
LN *p;
p=s->next;
while(p)
{
printf("%d ",p->num);
p=p->next;
}
}
**在Divide這個函數裡面我有兩個鏈表,如代碼所示,我已經返回頭指針q,可是為什麼程序還是運行不了?簡而言之就是我的q怎麼傳出來,我明明已經傳出來了,在主函數裡面又要由什麼接應他。
**
Divide函數應該返回一個指針,然後在main函數裡接收該指針地址,而且Divide函數裡本身也有點問題,我已經改了,樓主可以看一下
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int num;
struct node *next;
}LN;
LN *Creat();
LN *Divide(LN *h);
void Printf(LN *s);
void main()
{
LN *h;
h=Creat();
LN *q = Divide(h);
printf("The even term is: ");
Printf(h);
printf("The odd term is: ");
Printf(q);
}
LN *Creat()
{
LN *p;
LN *b;
LN *h;
int a;
h=(LN *)malloc(sizeof(LN));
h->next=NULL;
b=h;
printf("Input the sum of the numbers: ");
scanf("%d",&a);
for(int i=0;i<a;i++)
{
printf("Input the %d number: ",i+1);
p=(LN *)malloc(sizeof(LN));
scanf("%d",&p->num);
b->next=p;
b=p;
}
p->next=NULL;
system("CLS");
return h;
}
LN *Divide(LN *h)
{
LN *q;
LN *b;
q=(LN *)malloc(sizeof(LN));
b=q;
while(h!=NULL&&h->next!=NULL)
{
b->next=h->next;
b=b->next;
h->next=b->next;
h=h->next;
}
return q;
}
void Printf(LN *s)
{
LN *p;
p=s->next;
while(p)
{
printf("%d ",p->num);
p=p->next;
}
}