這個程序的目的是回顯之前輸入的字符串,我希望得到的顯示效果是這樣的:結果是:***************.但是程序從第二次遞歸調用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(); }}
熱心網(wǎng)友
呵呵,初學者常犯的毛病,不要緊,多寫些程序就不會了。你在第二次讀取數(shù)據(jù)前,先要求用戶選擇是否還要繼續(xù)輸入數(shù)據(jù):cout<