数据结构课后作业 42.docx
《数据结构课后作业 42.docx》由会员分享,可在线阅读,更多相关《数据结构课后作业 42.docx(13页珍藏版)》请在冰点文库上搜索。
![数据结构课后作业 42.docx](https://file1.bingdoc.com/fileroot1/2023-7/1/80f43738-e86c-47bd-8ce8-1d2e129aa9b3/80f43738-e86c-47bd-8ce8-1d2e129aa9b31.gif)
数据结构课后作业42
实验报告(示例)
实验名称:
数据结构与算法
实验学期:
2012-2013
(1)
实验班级:
11物联
姓名:
所属学院:
计算机学院
指导教师:
二〇一二年九月十二日
广东技术师范学院实验报告(示例)
实验内容:
线性表及其操作
实验目的:
实验地点:
工业中心202
实验序号:
2
预习内容:
已完成(见报告第1-3节)
操作情况
完成(见报告第4节)
实验中出现的主要问题和解决情况
结果:
运行正常,输出正确,达到实验目的
2.算法描述:
(包含函数:
)
/*文件名:
4-2.hpp*/
#include
#include
typedefstructsnode
{
chardata;
structsnode*next;
}LiString;
voidStrAssign(LiString*&s,chart[])
{
inti;
LiString*r,*p;
s=(LiString*)malloc(sizeof(LiString));
s->next=NULL;r=s;
for(i=0;t[i]!
='\0';i++)
{
p=(LiString*)malloc(sizeof(LiString));
p->data=t[i];p->next=NULL;
r->next=p;r=p;
}
}
voidStrCopy(LiString*&s,LiString*t)
{
LiString*p=t->next,*q,*r;
s=(LiString*)malloc(sizeof(LiString));
s->next=NULL;s->next=NULL;r=s;
while(p!
=NULL)/*将t的所有结点复制到s*/
{
q=(LiString*)malloc(sizeof(LiString));
q->data=p->data;q->next=NULL;
r->next=q;r=q;
p=p->next;
}
}
intStrEqual(LiString*s,LiString*t)
{
LiString*p=s->next,*q=t->next;
while(p!
=NULL&&q!
=NULL&&p->data==q->data)
{
p=p->next;
q=q->next;
}
if(p==NULL&&q==NULL)
return1;
else
return0;
}
intStrLength(LiString*s)
{
inti=0;
LiString*p=s->next;
while(p!
=NULL)
{
i++;p=p->next;
}
returni;
}
LiString*Concat(LiString*s,LiString*t)
{
LiString*str,*p=s->next,*q,*r;
str=(LiString*)malloc(sizeof(LiString));
str->next=NULL;r=str;
while(p!
=NULL)/*将s的所有结点复制到str*/
{
q=(LiString*)malloc(sizeof(LiString));
q->data=p->data;q->next=NULL;
r->next=q;r=q;
p=p->next;
}
p=t->next;
while(p!
=NULL)/*将t的所有结点复制到str*/
{
q=(LiString*)malloc(sizeof(LiString));
q->data=p->data;q->next=NULL;
r->next=q;r=q;
p=p->next;
}
returnstr;
}
LiString*SubStr(LiString*s,inti,intj)
{
intk;
LiString*str,*p=s->next,*q,*r;
str=(LiString*)malloc(sizeof(LiString));
str->next=NULL;r=str;
if(i<=0||i>StrLength(s)||j<0||i+j-1>StrLength(s))
{
printf("参数不正确\n");
returnstr;/*参数不正确时返回空串*/
}
for(k=0;kp=p->next;
for(k=1;k<=j;k++)/*将s的第i个结点开始的j个结点复制到str*/
{
q=(LiString*)malloc(sizeof(LiString));
q->data=p->data;q->next=NULL;
r->next=q;r=q;
p=p->next;
}
returnstr;
}
LiString*InsStr(LiString*s,inti,LiString*t)
{
intk;
LiString*str,*p=s->next,*p1=t->next,*q,*r;
str=(LiString*)malloc(sizeof(LiString));
str->next=NULL;r=str;
if(i<=0||i>StrLength(s)+1)/*参数不正确时返回空串*/
{
printf("参数不正确\n");
returnstr;
}
for(k=1;k
{
q=(LiString*)malloc(sizeof(LiString));
q->data=p->data;q->next=NULL;
r->next=q;r=q;
p=p->next;
}
while(p1!
=NULL)/*将t的所有结点复制到str*/
{
q=(LiString*)malloc(sizeof(LiString));
q->data=p1->data;q->next=NULL;
r->next=q;r=q;
p1=p1->next;
}
while(p!
=NULL)/*将*p及其后的结点复制到str*/
{
q=(LiString*)malloc(sizeof(LiString));
q->data=p->data;q->next=NULL;
r->next=q;r=q;
p=p->next;
}
returnstr;
}
LiString*DelStr(LiString*s,inti,intj)
{
intk;
LiString*str,*p=s->next,*q,*r;
str=(LiString*)malloc(sizeof(LiString));
str->next=NULL;r=str;
if(i<=0||i>StrLength(s)||j<0||i+j-1>StrLength(s))
{
printf("参数不正确\n");
returnstr;/*参数不正确时返回空串*/
}
for(k=0;k{
q=(LiString*)malloc(sizeof(LiString));
q->data=p->data;q->next=NULL;
r->next=q;r=q;
p=p->next;
}
for(k=0;kp=p->next;
while(p!
=NULL)/*将*p及其后的结点复制到str*/
{
q=(LiString*)malloc(sizeof(LiString));
q->data=p->data;q->next=NULL;
r->next=q;r=q;
p=p->next;
}
returnstr;
}
LiString*RepStr(LiString*s,inti,intj,LiString*t)
{
intk;
LiString*str,*p=s->next,*p1=t->next,*q,*r;
str=(LiString*)malloc(sizeof(LiString));
str->next=NULL;r=str;
if(i<=0||i>StrLength(s)||j<0||i+j-1>StrLength(s))
{
printf("参数不正确\n");
returnstr;/*参数不正确时返回空串*/
}
for(k=0;k{
q=(LiString*)malloc(sizeof(LiString));
q->data=p->data;q->next=NULL;
r->next=q;r=q;
p=p->next;
}
for(k=0;kp=p->next;
while(p1!
=NULL)/*将t的所有结点复制到str*/
{
q=(LiString*)malloc(sizeof(LiString));
q->data=p1->data;q->next=NULL;
r->next=q;r=q;
p1=p1->next;
}
while(p!
=NULL)/*将*p及其后的结点复制到str*/
{
q=(LiString*)malloc(sizeof(LiString));
q->data=p->data;q->next=NULL;
r->next=q;r=q;
p=p->next;
}
returnstr;
}
voidDispStr(LiString*s)
{
LiString*p=s->next;
while(p!
=NULL)
{
printf("%c",p->data);
p=p->next;
}
printf("\n");
}
/*文件名:
4-2.cpp*/
/*文件名:
algo4-2.cpp*/
#include"algo4-2.hpp"
#include
#include
voidmain()
{
charstr[19]="abcdefghefghijklmn";
charstr1[4]="xyz";
inti;
LiString*s;
LiString*s1;
LiString*s2;
LiString*s3;
LiString*s4;
cout<<"建立串S=abcdefghefghijklmn和串S1=xyz\n";
StrAssign(s,str);//建立串S
StrAssign(s1,str1);//建立串S1
cout<<"建立串S成功.\n";
cout<cout<<"输出串S:
";
DispStr(s);
cout<cout<cout<<"输出串S的长度:
";
cout<cout<cout<cout<<"在串S的第9个字符位置插入串S1而产生串S2\n";
s2=InsStr(s,9,s1);
cout<<"插入成功";
cout<cout<cout<<"输出串S2:
";
DispStr(s2);
cout<cout<cout<<"删除串S的第2个字符开始的5个字符而产生串S2\n";
s2=DelStr(s,2,5);
cout<<"删除成功\n";
cout<cout<<"输出串S2:
";
DispStr(s2);
cout<cout<cout<<"将串S的第2个字符开始的5个字符替换成串S1而产生串S2\n";
s2=RepStr(s,2,5,s1);
cout<<"替换成功\n";
cout<cout<<"输出串S2:
";
DispStr(s2);
cout<cout<cout<<"提取串S的第2个字符开始的10个字符而产生串S3\n";
s3=SubStr(s,2,10);
cout<<"提取成功\n";
cout<cout<<"输出串S3:
";
DispStr(s3);
cout<cout<
cout<<"将串S1和串S2连接起来而产生串S4\n";
s4=Concat(s1,s2);
cout<<"连接成功\n";
cout<cout<<"输出串S4:
";
DispStr(s4);
cout<cout<}
3.实验结果分析:
实验程序运行结果