#include <iostream>
using namespace std;
typedef struct node
{
char data;
struct node *next;
}LinkStrNode;
typedef LinkStrNode * LinkString; //链式串的指针
//初始化串
void Init_LinkString(LinkString &ls)
{
ls=(LinkStrNode *)malloc(sizeof(LinkStrNode));
ls->next=NULL;
}
//打印串
void Print_LinkString(LinkString &ls)
{
if(!ls->next)
cout<<"空串!"<<endl;
else
{
LinkStrNode *cur=ls->next;
while(cur)
{
cout<<cur->data;
cur=cur->next;
}
}
}
//给串赋值
void Build_LinkString(LinkString &ls,char *str)
{
LinkStrNode *pre=ls;
int length=strlen(str);
int i;
for(i=0;i<length;i++)
{
LinkStrNode *temp=(LinkStrNode *)malloc(sizeof(LinkStrNode));
temp->data=str[i];
temp->next=NULL;
pre->next=temp;
pre=pre->next;
}
}
//求串的长度
int LinkStringLen(LinkString &ls)
{
int len=0;
LinkStrNode *cur=ls->next;
while(cur)
{
len++;
cur=cur->next;
}
return len;
}
//串复制
/*
void LinkStringCopy(LinkString &to,LinkString &from)
{
LinkStrNode *cur=from->next;
LinkStrNode *pre=to;
while(cur)
{
LinkStrNode *temp=(LinkStrNode *)malloc(sizeof(LinkStrNode));
temp->data=cur->data;
cur=cur->next;
temp->next=NULL;
pre->next=temp;
pre=pre->next;
}
}
*/
//串连接
/*
void LinkStringCat(LinkString &ls1,LinkString &ls2)
{
LinkStrNode *cur=ls2->next;
LinkStrNode *pre=ls1->next;
while(pre->next!=NULL)
pre=pre->next;
pre->next=cur;
}
*/
//串比较
/*
int LinkStringcmp(LinkString &ls1,LinkString &ls2)
{
LinkStrNode *cur1=ls1->next;
LinkStrNode *cur2=ls2->next;
while(cur1 && cur2)
{
if(cur1->data != cur2->data)
return cur1->data - cur2->data;
cur1=cur1->next;
cur2=cur2->next;
}
if(cur1)
return 1;
else if(cur2)
return -1;
else
return 0;
}
*/
//提取hs1中的子串
/*
LinkString LinkStringSub(LinkString &ls, int index, int len)
{
LinkString temp;
Init_LinkString(temp);
int length=LinkStringLen(ls);
if(len>length-index+1)
{
cout<<"提取的子串过长!"<<endl;
}
else
{
LinkStrNode *lscur=ls->next;
LinkStrNode *pre=temp; //把需要提取的子符复制到temp上
int i=1;
while(i!=index)
{
lscur=lscur->next;
i++;
}
i=1;
while(i<=len)
{
LinkStrNode *lstemp=(LinkStrNode *)malloc(sizeof(LinkStrNode));
lstemp->data=lscur->data;
lscur=lscur->next;
lstemp->next=NULL;
pre->next=lstemp;
pre=pre->next;
i++;
}
}
return temp;
}
*/
//向串s中第index个位置插入串t
void LinkStringInsert(LinkString &s, int index, LinkString &t)
{
LinkStrNode *spre=s; //指向被插串的index位置的前一个字符
LinkStrNode *scur=s->next; //指向被插串的index位置
int i=1;
while(i!=index)
{
spre=scur;
scur=scur->next;
i++;
}
LinkStrNode *tpre=t->next; //插入串的第一个字符的指针
LinkStrNode *tcur=tpre; //插入串的最后一个字符的指针
while(tcur->next!=NULL)
{
tcur=tcur->next;
}
tcur->next=scur;
spre->next=tpre;
free(t);
}
//替换
void LinkStringRep(LinkString &s, int index,int len, LinkString &t)
{
LinkStrNode *scur=s->next;
LinkStrNode *tcur=t->next;
int i=1;
while(i!=index)
{
scur=scur->next;
i++;
}
for(i=0;i<len;i++)
{
if(scur!=NULL)
{
scur->data=tcur->data;
scur=scur->next;
tcur=tcur->next;
}
else
{
scur=(LinkStrNode *)malloc(sizeof(LinkStrNode));
scur->data=tcur->data;
scur->next=NULL;
}
}
}
int main()
{
LinkString ls1;
Init_LinkString(ls1);
//给串赋值
char *str1="Hello World!";
Build_LinkString(ls1,str1);
cout<<LinkStringLen(ls1)<<endl; //长度
Print_LinkString(ls1);
cout<<endl;
LinkString ls2;
Init_LinkString(ls2);
//给串赋值
char *str2="fuckyou!";
Build_LinkString(ls2,str2);
cout<<LinkStringLen(ls2)<<endl;
Print_LinkString(ls2);
cout<<endl;
//串复制
//LinkString ls3;
//Init_LinkString(ls3);
//LinkStringCopy(ls3,ls1);
//cout<<LinkStringLen(ls3)<<endl;
//Print_LinkString(ls3);
//cout<<endl;
//串连接
//LinkStringCat(ls1,ls2);
//cout<<LinkStringLen(ls1)<<endl;
//Print_LinkString(ls1);
//cout<<endl;
//串1和串2比较
/*
int result=LinkStringcmp(ls1,ls2);
if(result>0)
cout<<"串1大于串2"<<endl;
else if(result==0)
cout<<"串1等于串2"<<endl;
else
cout<<"串1小于串2"<<endl;
*/
//提取hs1中的子串
//cout<<"提取的子串是:"<<endl;
//LinkString lssub=LinkStringSub(ls1, 7, 6);
//Print_LinkString(lssub);
//cout<<endl;
//插入串
LinkStringInsert(ls1, 13, ls2);
Print_LinkString(ls1);
cout<<endl;
//替换
LinkString T;
Init_LinkString(T);
char *str3="********";
Build_LinkString(T,str3);
LinkStringRep(ls1, 13,8, T);
Print_LinkString(ls1);
cout<<endl;
return 0;
}
数据结构——串的链式存储
最新推荐文章于 2021-08-19 10:30:11 发布