您的位置:首页 > 理论基础 > 数据结构算法

数据结构—链串

2016-04-17 18:30 288 查看
/*
编写一个程序,实现链串的各种基本运算,并完成如下功能:
(1)建立串:s="abcdefghefghijklmn"和串s1="xyz";
(2)输出串s;
(3)输出串的长度;
(4)在串的第9个字符位置插入串s1而产生串s2;
(5)输出串s2;
(6)删除串s第2个字符开始的5个字符而产生串s2;
(7)输出串s2;
(8)将串s第2个字符开始的5个字符替换成串s1而产生s2;
(9)输出串s2;
(10)提取串s的第2个符号开始的10个字符而产生串s3;
(11)输出串s3;
(12)将串s1和串s2连接起来而产生串s4;
(13)输出串s4。
*/
#include <iostream>
#include <malloc.h>
using namespace std;
typedef struct snode
{
char data;
struct snode *next;
} LiString;
void StrAssign(LiString *&s,char cstr[])   //将一个字符串常量赋给串s,即生成一个其值等于cstr的串s
{
int i;
LiString *r,*p;
s=(LiString *)malloc(sizeof(LiString));
r=s;
for(i=0; cstr[i]!='\0'; i++)
{
p=(LiString *)malloc(sizeof(LiString));
p->data=cstr[i];
r->next=p;
r=p;
}
r->next=NULL;
}
void DispStr(LiString *s)              //输出串的所有元素值
{
LiString *p=s->next;
while(p!=NULL)
{
cout<<p->data;
p=p->next;
}
cout<<endl;
}
int StrLength(LiString *s)        //串的长度
{
int i=0;
LiString *p=s->next;
while(p!=NULL)
{
i++;
p=p->next;
}
return i;
}
LiString *InsStr(LiString *s,int i,LiString *t)  //将串s2插入到串s1中第i个字符中,即将s2的第一个字符作为s1的第i个字符
{
int k;
LiString *str,*p=s->next,*p1=t->next,*q,*r;
str=(LiString *)malloc(sizeof(LiString));
str->next=NULL;
r=str;
for(k=1;k<i;k++)
{
q=(LiString *)malloc(sizeof(LiString));
q->data=p->data;
r->next=q;
r=q;
p=p->next;
}
while(p1!=NULL)
{
q=(LiString *)malloc(sizeof(LiString));
q->data=p1->data;
r->next=q;
r=q;
p1=p1->next;
}
while(p!=NULL)
{
q=(LiString *)malloc(sizeof(LiString));
q->data=p->data;
r->next=q;
r=q;
p=p->next;
}
r->next=NULL;
return str;
}
LiString *DelStr(LiString *s,int i,int j)  //从串s中删去第i个字符开始的长度为j的子串,并产生新串
{
int k;
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))
return str;
for(k=0;k<i-1;k++)
{
q=(LiString *)malloc(sizeof(LiString));
q->data=p->data;
r->next=q;
r=q;
p=p->next;
}
for(k=0;k<j;k++)
p=p->next;
while(p!=NULL)
{
q=(LiString *)malloc(sizeof(LiString));
q->data=p->data;
r->next=q;
r=q;
p=p->next;
}
r->next=NULL;
return str;
}
LiString *RepStr(LiString *s,int i,int j,LiString *t)   //在串s中,将第i个字符开始的j个字符构成的子串用串t替换,返回新串
{
int k;
LiString *str,*p=s->next,*p1=t->next,*q,*r;
str=(LiString *)malloc(sizeof(LiString));
str->next=NULL;
r=str;
for(k=1;k<i;k++)
{
q=(LiString *)malloc(sizeof(LiString));
q->data=p->data;
r->next=q;
r=q;
p=p->next;
}
for(k=0;k<j;k++)
p=p->next;
while(p1!=NULL)
{
q=(LiString *)malloc(sizeof(LiString));
q->data=p1->data;
r->next=q;
r=q;
p1=p1->next;
}
while(p!=NULL)
{
q=(LiString *)malloc(sizeof(LiString));
q->data=p->data;
r->next=q;
r=q;
p=p->next;
}
r->next=NULL;
return str;
}
LiString *SubStr(LiString *s,int i,int j)   //求子串
{
int k;
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))
return str;
for(k=0;k<i-1;k++)
p=p->next;
for(k=0;k<j;k++)
{
q=(LiString *)malloc(sizeof(LiString));
q->data=p->data;
r->next=q;
r=q;
p=p->next;
}
r->next=NULL;
return str;
}
LiString *Concat(LiString *s,LiString *t)     //串连接
{
int k;
LiString *str,*p=s->next,*q,*r;
str=(LiString *)malloc(sizeof(LiString));
r=str;
while(p!=NULL)
{
q=(LiString *)malloc(sizeof(LiString));
q->data=p->data;
r->next=q;
r=q;
p=p->next;
}
p=t->next;
while(p!=NULL)
{
q=(LiString *)malloc(sizeof(LiString));
q->data=p->data;
r->next=q;
r=q;
p=p->next;
}
r->next=NULL;
return str;
}
int main()
{
LiString *s,*s1,*s2,*s3,*s4;
StrAssign(s,"abcdefghefghijklmn");
StrAssign(s1,"xyz");
cout<<"(1)建立串s=abcdefghefghijklmn和s1=xyz"<<endl;
cout<<"(2)输出串s:"<<endl;
DispStr(s);
cout<<"(3)输出串的长度:"<<StrLength(s)<<endl;
cout<<"(4)在串的第9个字符位置插入串s1而产生串s2"<<endl;
s2=InsStr(s,9,s1);
cout<<"(5)输出串s2:"<<endl;
DispStr(s2);
cout<<"(6)删除串s第2个字符开始的5个字符而产生串s2"<<endl;
s2=DelStr(s,2,5);
cout<<"(7)输出串s2"<<endl;
DispStr(s2);
cout<<"(8)将串s第2个字符开始的5个字符替换成串s1而产生s2"<<endl;
s2=RepStr(s,2,5,s1);
cout<<"(9)输出串s2"<<endl;
DispStr(s2);
cout<<"(10)提取串s的第2个符号开始的10个字符而产生串s3"<<endl;
s3=SubStr(s,2,10);
cout<<"(11)输出串s3"<<endl;
DispStr(s3);
cout<<"(12)将串s1和串s2连接起来而产生串s4"<<endl;
s4=Concat(s1,s2);
cout<<"(13)输出串s4"<<endl;
DispStr(s4);
return 0;
}

运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数据结构