#include<stdio.h>
#include<malloc.h>
struct num
{
char n;
struct num*next;
};
struct num*creat(void) //创造链表函数
{
struct num*head,*r,*s;
char str[10];
head=(struct num*)malloc(sizeof(struct num));
head=NULL;
r=head;
scanf("%c",&str[0]);
head->n=str[0];
int i=0;
while(str[i]!=0)
{
s=(struct num*)malloc(sizeof(struct num));
i++;
scanf("%c",str[i]);
s->n=str[i];
r->next=s;
r=s;
}
r->next=head;
return head;
}
struct num*print(struct num*head) //输出链表函数
{
struct num*p;
p=head;
while(p)
{
printf("%5c",p->n);
p=p->next;
if(p->next==head)
break;
}
}
struct num*insert(struct num*head)
{
struct num*p,*q,*save;
if(head->next==head)
{
q=(struct num*)malloc(sizeof(struct num));
q->n='b';
head->next=q;
q->next=head;
}
else
{
head->n='a';
}
p=(struct num*)malloc(sizeof(struct num));
p=head->next;
while(p->n!='a')
{
save=p;
p=p->next;
}
q=(struct num*)malloc(sizeof(struct num));
q->n='b';
q->next=p;
save->next=q;
return head;
}
int main(void)
{
struct num*head,*p2;
head=creat();
print(head);
insert(head);
print(head);
return 0;
}
你可以先只测试 create 和 print ,这两个正常么?可以加下日志。