思路1:头插法–在头结点插入next对象
typedef struct node_{
char c;
struct node_* next;
}abc;
//释放链表
void Free(abc* head)
{
abc* p;
while(head != NULL) {
p = head;
head = head->next;
free(p);
}
if(head == NULL) printf("free succes\n");
}
//头插入
abc* SetHead(abc* head)
{
abc* a,*b,*c;
a = head;
c = (abc*)malloc(sizeof(abc));
if(a->next == NULL) {
c->next = NULL;
a->next = c;
return a;
}
c->next = a->next;
a->next = c;
// free(c);
return a;
}
void main()
{
abc* p,*d;
p = (abc*)malloc(sizeof(abc));
p->next = NULL;
char cc[100];
printf("please input string:");
scanf("%s",cc);
printf("cc = [%s] size = [%d] [%d]\n",cc,strlen(cc),sizeof(cc));
static int i=0;
char* pi = cc;
while(*pi){
i++;
i++;
p = SetHead(p);
p->next->c = *pi;
pi++;
}
d = p;
i=0;
printf("d=");
while(d->next != NULL) {
i++;
printf("%c",d->next->c);
d = d->next;
}
printf("\n");
Free(p);
}
结果:
please input string:0123456789
cc = [0123456789] size = [10] [100]
d=9876543210