#includeiostreamusingnamespacestd;typedefstructnode{chardata;structnode*next;}link;link*get(link*l,inti){link*p;intj=0;p=l;while((ji)next!=NULL)){p=p-next;j;}if(j==i)returnp;elsereturnNULL;}link*ins(link*l,charch,inti){link*p,*s;p=get(l,i-1);if(p==NULL)cout\"输入有误\"endl;else{s=(link*)malloc(sizeof(link));s-data=ch;s-next=p-next;p-next=s;}returnl;}link*find(link*l,charch){link*p;inti=0;intj=0;p=l;while(p!=NULL){i;if(p-data!=ch)p=p-next;else{cout\"您查找的数据在第\"i-1\"个位置.\"endl;j=1;p=p-next;}}if(j!=1)cout\"您查找的数据不在线性表中.\"endl;returnl;}link*del(link*l,inti){link*p,*s;p=get(l,i-1);if(p==NULL)cout\"输入有误\"endl;else{s=p-next;p-next=s-next;free(s);}returnl;}link*add(link*l){link*p,*s;cout\"请输入一串单字符数据,以*结束!\"endl;charch;link*HEAD;link*R,*P,*L;HEAD=(link*)malloc(sizeof(link));HEAD-next=NULL;R=HEAD;getchar();ch=getchar();while(ch!=\'*\'){P=(link*)malloc(sizeof(link));P-data=ch;P-next=NULL;R-next=P;R=R-next;getchar();ch=getchar();}L=HEAD;cout\"当前输入的线性表为:\"endl;P=L;P=P-next;if(L!=NULL)do{coutP-data\"\";P=P-next;}while(P!=NULL);coutendl;p=l;while(p-next!=NULL)p=p-next;s=L;p-next=s-next;p=l;returnl;}link*print(link*l){inti,k;charch;link*p,*q;cout\"当前线性表为:\"endl;p=l;p=p-next;if(l!=NULL)do{coutp-data\"\";p=p-next;}while(p!=NULL);coutendl;cout\"请选择您要的操作:\";cout\"1、插入\";cout\"2、查找\";cout\"3、删除\";cout\"4、合并\";cout\"0、退出\";coutendl;cink;if(k==1){cout\"请输入您要插入的数据值:\";cinch;cout\"请输入您要插入的位置:\";cini;p=ins(l,ch,i);q=print(l);}elseif(k==2){cout\"请输入您要查找的数据值:\";cinch;p=find(l,ch);q=print(l);}elseif(k==3){cout\"请输入您要删除的数据的位置:\";cini;p=del(l,i);q=print(l);}elseif(k==4){p=add(l);q=print(l);}elseif(k==0);else{cout\"输入错误!\"endl;q=print(l);}returnl;}intmain(){cout\"请输入一串单字符数据,以*结束!\"endl;charch;//link*head;link*r,*p,*q,*l;l=(link*)malloc(sizeof(link));l-next=NULL;r=l;ch=getchar();//getchar();while(ch!=\'*\'){p=(link*)malloc(sizeof(link));p-data=ch;p-next=NULL;r-next=p;r=r-next;ch=getchar();//getchar();}//l=head;q=print(l);return0;}c语言的#includestdio.h#includemalloc.h#defineN8typedefstructnode{intdata;structnode*next;}node;node*createsl(){node*p,*s,*h;intj=1,x;p=s=h=(node*)malloc(sizeof(node));h-next=NULL;printf(\"pleaseinputthedatatocreatethelist,endwith-1or%dnupmbers\\n\",N);while(x!=-1=N){printf(\"number%d:\",j);scanf(\"%d\",s=(node*)malloc(sizeof(node));s-data=x;if(h-next==NULL)h-next=s;elsep-next=s;p=s;j;}p-next=NULL;returnh;}intaccess(node*h,inti){node*p;intj=1;p=h-next;while(p!=NULL){if(p-data==i)break;p=p-next;j;}if(p!=NULL){printf(\"findthenumberinposition:%d\\n\",j);return(p-data);}else{printf(\"can\'tfindthenumberinthelist!\\n\");return-1;}}voidinsertsl(node*h,inti){node*p,*t;intj=1;p=h-next;;while(p-next!=NULL){p=p-next;j;}t=(node*)malloc(sizeof(node));t-data=i;t-next=p-next;p-next=t;printf(\"insertsuccessinposition%d\\n\",j1);}voiddeletesl(node*h,inti){node*p,*s,*q;intj=1;p=h;while(p-next!=NULL){q=p-next;if(q-data==i)break;p=p-next;j;}if(p-next==NULL){printf(\"Can\'tfindthenumberyouwanttodelete.\\n\");return;}else{s=p-next;p-next=s-next;free(s);printf(\"deletesuccessinposition%d\\n\",j1);}}voidprint(node*h){printf(\"\\nprintallthedatainthelist:\");node*s;s=h-next;if(s!=NULL){while(s!=NULL){printf(\"%d\",s-data);s=s-next;}}elseprintf(\"thelistisempty!%d\");printf(\"\\n\");}intmain(){node*p;inta;p=createsl();printf(\"\\nyouneedfindthenumber:\\n\");scanf(\"%d\",access(p,a);printf(\"\\npleaseinputthenumberyouwanttoinsert:\\n\");scanf(\"%d\",insertsl(p,a);printf(\"\\npleaseinputthenumberyouwanttodelete:\\n\");scanf(\"%d\",deletesl(p,a);print(p);return0;}
|