用C语言编一个尾插法(数据结构题)
2个回答
2008-10-05
展开全部
//---------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
typedef struct WCF{
char data;
struct WCF *next;
} WCF;
WCF *cr(WCF *a,char d)
{
WCF *t=NULL;
if (a==NULL) {
t=(WCF *)malloc(sizeof(WCF));
t->data =d;
t->next =NULL;
a=t;
}
else {
a->next =cr(a->next ,d);
}
return a;
}
int main(void)
{
WCF *wf=NULL,*tf;
char d;
while ((d=getchar())!='\n')
wf=cr(wf,d);
while (wf!=NULL)
{
tf=wf;
putchar(wf->data);
wf=wf->next ;
free(tf);
}
return 0;
}
//---------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
typedef struct WCF{
char data;
struct WCF *next;
} WCF;
WCF *cr(WCF *a,char d)
{
WCF *t=NULL;
if (a==NULL) {
t=(WCF *)malloc(sizeof(WCF));
t->data =d;
t->next =NULL;
a=t;
}
else {
a->next =cr(a->next ,d);
}
return a;
}
int main(void)
{
WCF *wf=NULL,*tf;
char d;
while ((d=getchar())!='\n')
wf=cr(wf,d);
while (wf!=NULL)
{
tf=wf;
putchar(wf->data);
wf=wf->next ;
free(tf);
}
return 0;
}
//---------------------------------------------------------------------------
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询