满意答案
viplizhi
2017.06.16
采纳率:53% 等级:12
已帮助:7879人
// 测试结果:
// 请输入小写字母串:abcdef
// abcdef [输出]
#include
#include
typedef struct pointer
{
char dat;
struct pointer *link;
} pointer;
pointer *readdata(pointer *head)
{ //读集合
//原代码char tmp[10];
//原代码char tmp2,tmp3;
char *tmp; //tmp是指针
int n=10; //动态数组的大小
pointer *p; //p是新节点
pointer *current; //current指向当前节点
int i=0;
//动态分配内存
tmp=(char *)malloc( n * sizeof(char) );
//原代码printf("input data ('0' for end):");
//原代码scanf("%s",&tmp);
printf("请输入小写字母串:");
scanf("%s",tmp);
while(*(tmp+i) != '\0')
{
if( (*(tmp+i)'z') )
{
printf("输入错误!必须为小写字母!ee\n");
return NULL;
}
i++;
}
i=0;
while(*(tmp+i) != '\0')
{
p=(pointer *)malloc(sizeof(struct pointer));
p->dat = *(tmp+i);
p->link = NULL;
if(head==NULL)
{
head=p;
current=head; //current指向当前节点
}
else
{
current->link=p;
current=p; //current指向当前节点
}
//原代码p->link=head->link;
//原代码head->link=p;
i++;
}
return head; //函数readdata()有返回值,返回链表头
}
void printData(pointer *head) //打印链表
{
pointer *p;
if(head==NULL)
{
printf("\n链表没有数据.\n");
return;
}
p=head;
while(p!=NULL)
{
printf("%c",p->dat);
p=p->link;
}
printf("\n");
}
int main()
{
pointer *head=NULL;
head=readdata(head); //创建链表
printData(head); //打印链表
return 0;
}
00分享举报