菜鸟每天问之。。。。链串的问题~~
- C/C++ code
#include<stdio.h>typedef struct student///////串链{ char a[5]; struct student *next;}stu1;newstring(stu1 *s)//////////////////窜链的初始{ s=(stu1 *)malloc(sizeof(stu1)); s->next=NULL;}withstring(stu1 *s1,char *s2)/////////////////////串链的赋值{ stu1 *p,*q; int i=0,j=0,t; p=s1; t=strlen(s2); for(;i<t;i++,j++){ p->a[i]=s2[j]; if(i==3){ q=(stu1 *)malloc(sizeof(stu1)); p->next=q; p=q; i=0;}}p->next=NULL;p->a[i]='\0';}print(stu1 *s){ stu1 *q; q=s; while(q!=NULL) { puts(q->a); q=q->next; }}main(){ char a[100]; stu1 b; newstring(&b); gets(a); withstring(&b,a); print(&b);}这个输出链串的程序为什么只能输出第一个数组呢?
请高手调试下,并且告诉我错误的地方谢谢了哇
[code=C/C++][/code]
[解决办法]
1。链串? 链表!
2。
newstring(stu1 *s)//////////////////链串的初始
{
s=(stu1 *)malloc(sizeof(stu1)); ??? 给局部变量s分配内存????
s->next=NULL;
}
3。withstring(stu1 *s1,char *s2)/////////////////////链串的赋值
{
stu1 *p,*q;
int i=0,j=0,t;
p=s1;
t=strlen(s2);
for(;i<t;i++,j++)
{
p->a[i]=s2[j];
if(i==3)
{
q=(stu1 *)malloc(sizeof(stu1));
p->next=q;
p=q;
i=0; // 此处清0,i < t怎么结束???!!!
}