关于c语言线性表的一个问题,请高手指教!

2025-03-24 16:20:10
推荐回答(4个)
回答1:

没有什么大问题,帮你改了点地方,自己看吧

#include
#include
#include
#define SIZE 10
//#define NUM

struct stu
{
char *elem;
int length;
int listsize;
}La, Lb, Lc;

void main()
{

char *pa, *pb, *pc;
char *pa_last, *pb_last;
pa = La.elem = (char *) malloc(SIZE*sizeof(char));
pb = Lb.elem = (char *) malloc(SIZE*sizeof(char));
La.listsize = Lb.listsize = SIZE;

/*初始化*/

printf("input list a and list b:\n");
scanf("%s%s",pa,pb);
/*调试显示*/
#ifdef NUM
printf("lista=%s\nlistb=%s\n",pa,pb);
#endif;

La.length = strlen(pa);
Lb.length = strlen(pb);
Lc.listsize = Lc.length = La.length + Lb.length;

#ifdef NUM
printf("Lc.length=%d\n",Lc.length);
#endif;

pc = Lc.elem = (char *) malloc((La.length + Lb.length + 1)*sizeof(char));
pa_last = pa + La.length - 1;
pb_last = pb + Lb.length - 1;

#ifdef NUM
printf("pa_last=%c\npb_last=%c\n",*pa_last,*pb_last);
#endif;

/*链接*/

while (pa <= pa_last && pb <= pb_last)
if (*pa <= *pb)
{ *pc = *pa++; pc++;}
else
{ *pc = *pb++; pc++;}

while (pa <=pa_last)
{*pc = *pa++; pc++; }
while (pb <=pb_last)
{*pc = *pb++; pc++; }
*pc='\0';

/*输出*/

printf("%s\n",Lc.elem);
}

回答2:

pc = '\0';
修改为:
*pc = '\0';

then OK!!

回答3:

程序的倒数第六行: pc = '\0'; 前加一个*
改为: *pc = '\0'; 就可以了!

回答4:

额,太长了,不想看