这个程序的目的是回显之前输入的字符串,我希望得到的显示效果是这样的:结果是:***************.但是程序从第二次递归调用main()起输出的格式就变成了:结果是:************.也就是由预期的一行变成了两行。怎么修正这个问题啊?#include<iostream.h#include<malloc.h#include<stdio.htypedef struct node{ char data; struct node *next;}node,*Lstring; //结构体定义void creat(Lstring &s){ node *p1,*p2; s=(Lstring)malloc(sizeof(node)); //新建头结点 printf(" 请输入字符串:"); p2=s; p1=p2; int c; while((c=getchar())!='.') //建立链表 { p1=(Lstring)malloc(sizeof(node)); //新建首元结点 p1-data=c; p2-next=p1; p2=p2-next; } p1=(Lstring)malloc(sizeof(node)); p1-data='.'; p2-next=p1; p1-next=NULL;}void main(){ struct node *p; Lstring a; creat(a); char y; cout<<endl<<" 结果是:"; p=a-next; if(p==NULL)cout<<" 空串"<<endl; else { while(p!=NULL) { cout<data; p=p-next; } cout<<endl; } cout<<endl<<" 是否重新输入字符串?(y/n)"; ciny; if(y=='y') { cout<<endl<<endl; main(); }}
热心网友
呵呵,初学者常犯的毛病,不要紧,多写些程序就不会了。你在第二次读取数据前,先要求用户选择是否还要继续输入数据:cout<