《数据构造》实验报告四串的操作班级:13网络工程学号:实验日期:/5/4姓名:王凯程序文献名及阐明:一、上机实验的内容和规定:1.采用次序存储,完毕串的比较运算strcmp(S,T)。2.若S和T是用结点大小为1的单链表存储的两个串,试设计一种算法将S中初次与串T匹配的子串逆置。二、实验报告必须写明内容1.程序设计的基本思想,原理和算法描述:(涉及程序的构造,数据构造,输入/输出设计,符号名阐明等)2.源程序及注释:3.运行输出成果:4.调试和运行程序过程中产生的问题及采用的方法:5.对算法的程序的讨论、分析,改善构想,其它经验教训。源程序代码及其运行成果//as.cpp:定义控制台应用程序的入口点。//#include"stdafx.h"#include#include#includevoidmain(void){charS[20],T[20];inti,j;char*n1,*n2;printf("请输入两个在20位以内的字符串:\n");gets(S);gets(T);n1=S;n2=T;j=strcmp(n1,n2);if(j<0)printf("ST");}#include#includetypedefstructnode{charch;structnode*next;}link;typedefstruct{link*head;intlen;}lkstring;//打印单链表voidprintl(link*h,intf){link*p;chars[20];inti;printf("\n");//f=0,表达没有头结点if(f==0)p=h;elsep=h->next;//f=1,表达带头结点,头结点不打印while(p!=NULL){s[i]=p->ch;i++;p=p->next;}for(i;i>-1;i--)printf("%c",s[i]);}//尾插法建立单链表voidstrint(lkstring*st,char*mys){charch;link*head,*s,*r;intl=0;head=(link*)malloc(sizeof(link));r=head;ch=mys[l];while(ch!='\0'){s=(link*)malloc(sizeof(link));l++;s->ch=ch;r->next=s;r=s;ch=mys[l];}r->next=NULL;st->head=head;st->len=l;}link*strindex(lkstring*s,lkstring*t){link*sp,*tp,*start;if(t->len==0)returns->head->next;start=s->head->next;sp=start;tp=t->head->next;while(sp&&tp){if(sp->ch==tp->ch){sp=sp->next;tp=tp->next;}else{start=start->next;sp=start;tp=t->head->next;}}if(!tp)returnstart;elsereturnNULL;}intstrnz(lkstring*s,lkstring*t){link*sp,*tp,*start,*p,*q;if(t->len==0)return1;start=s->head->next;sp=start;tp=t->head->next;while(sp&&tp){if(sp->ch==tp->ch){sp=sp->next;tp=tp->next;}else{start=start->next;sp=start;tp=t->head->next;}}if(tp)return0;//如果没有子串,返回p=s->head->next;//到子串开始的前一位置while(p->next!=start)p=p->next;//到子串开始的前一位置p->next=sp;//删除子串tp=t->head->next;//while(tp)//将t串逐个读出,插入到P之后,就是逆置子串了{q=(link*)malloc(sizeof(link));q->ch=tp->ch;q->next=p->next;p->next=q;tp=tp->next;}return1;}intmain(void){lkstring*s,*t;link*p,*q;s=(lkstring*)malloc(sizeof(lkstring));t=(lkstring*)malloc(sizeof(lkstring));strint(s,"fyhjufrfrdvsd");strint(t,"frfr");printf("逆置后子串为:");if(strnz(s,t))printl(t->head,0);elseprintf("没有匹配子串");return1;}