这个字符串编辑程序是用单链表做的,程序最后要求输出编辑后的字符串,比如执行替换操作,把o换成e,则原字符串Thisisabook.变为Thisisabeek.但是现在我写的这个程序输出的是Thisisabeek.单词中间没有空格。怎样才能把空格加上呢?#include<iostream.h#include<malloc.htypedefstructnode{chardata;structnode*next;}node,*Lstring;//结构体定义voidcreat(Lstring&s){node*p1,*p2;s=(Lstring)malloc(sizeof(node));//新建头结点cout<<"请输入字符串:";p2=s;p1=p2;while(p1-data!='.')//建立链表{p1=(Lstring)malloc(sizeof(node));//新建首元结点cinp1-data;p2-next=p1;p2=p2-next;}p1-next=NULL;}voidinsert(Lstring&s)//插入函数{node*p1,*p2,*q;charc,x;repeat:;cout<<"需要在哪个字符之前插入?";cinc;for(p1=s,p2=p1;p1-next!=NULL;p1=p1-next)if(p1-next-data==c)p2=p1;if(p2==s){cout<<endl<<"字符串中不存在字符"<<c<<",请重新输入!"<<endl<<endl;gotorepeat;}cout<<"请输入欲插入的字符:";cinx;q=(Lstring)malloc(sizeof(node));q-data=x;q-next=p2-next;p2-next=q;}voiddel(Lstring&s){node*p,*q;charc;repeat:;cout<<"请输入欲删除的字符:";cinc;p=s;while(p-next-data!=c){p=p-next;if(p-next==NULL){cout<<endl<<"字符串中不存在字符"<<c<<",请重新输入!"<<endl<<endl;gotorepeat;}}q=p-next;p-next=q-next;free(q);}voidtrans(Lstring&s){node*p;chara1,a2;inti=0;cout<<"请输入被替换字符:";cina1;cout<<"请输入替换的字符:";cina2;p=s;while(p-next!=NULL){if(p-next-data==a1){p-next-data=a2;++i;}p=p-next;}if(i==0){cout<<endl<<"字符串中不存在字符"<<a1<<",请重新输入!"<<endl<<endl;trans(s);}}voidmain(){repeat2:;structnode*p;Lstringa;creat(a);cout<<endl<<"您可以输入这3个指令之一:I—插入D—删除R—替换"<<endl;repeat1:;cout<<endl<<"请输入指令:";chary;ciny;if(y!='I'&&y!='D'&&y!='R'){cout<<endl<<"您输入了一个错误指令,请重新输入!"<<endl;gotorepeat1;}switch(y){case'I':insert(a);break;case'D':del(a);break;case'R':trans(a);break; }cout<<endl<<"结果是:";p=a-next;if(p==NULL)cout<<"空串"<<endl;else{while(p!=NULL){cout<data;p=p-next;}}rep:;cout<<endl<<endl<<"是否继续编辑当前字符串?(y/n)";ciny;if(y=='y')gotorepeat1;elseif(y=='n')cout<<endl<<"是否重新输入字符串?(y/n)";else{cout<<endl<<"请按Y或N进行选择!";gotorep;}ciny;if(y=='y'){cout<<endl<<endl;gotorepeat2;}}

热心网友

源码错误不少。略一看,发现问题,(不包括风格的问题)在创建链表时,输入字符cin的方法不对。会过滤空格,作为下一次输入的标志。可以查一下,创建之后输出来应该也没有空格符。while(p1-data!='.') //建立链表{p1=(Lstring)malloc(sizeof(node)); //新建首元结点cinp1-data;}