数据结构—链串
来源:互联网 发布:torrentkitty新域名 编辑:程序博客网 时间:2024/05/19 20:48
/*编写一个程序,实现链串的各种基本运算,并完成如下功能:(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;}
运行结果:
0 0
- 数据结构—链串
- 数据结构——什么是数据结构
- 【大话数据结构】——-数据结构
- 数据结构 —— 数据结构概述
- [数据结构]数据结构——链表法
- PHP数据结构之一:PHP数据结构基本概念—数据结构
- 数据结构——基本数据结构之栈
- 数据结构——基本数据结构之队列
- 数据结构——基本数据结构之有根树
- 数据结构——基本数据结构之栈
- 数据结构——基本数据结构之队列
- 数据结构——大话数据结构源码
- 数据结构学习总结——数据结构概述
- 大话数据结构——数据结构绪论
- 【大话数据结构】——-数据结构概述
- 数据结构(1)——大话数据结构
- 数据结构(2)——大话数据结构
- 数据结构——堆
- opencv角点检测
- 如何在Windows下安装boost
- 如何手动彻底消除U盘使用痕迹
- Linux的chmod与symbolic link
- MySql 单表的查询sql语句(一)
- 数据结构—链串
- 【codevs 2342】[2007年省队选拔赛上海市队选拔赛] 园丁的烦恼(离散化+树状数组)
- Android-Sqlite使用手册
- JAVA 注解Annotation入门
- 初识hbase
- 二维数组、指针数组、行指针、二级指针间的关联
- 递推之折叠方阵问题
- POI之Excel简单导入
- 杭电2199 Can you solve this equation?