数据结构—链串
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; }
运行结果:
相关文章推荐
- C#数据结构之顺序表(SeqList)实例详解
- Lua教程(七):数据结构详解
- 解析从源码分析常见的基于Array的数据结构动态扩容机制的详解
- C#数据结构之队列(Quene)实例详解
- C#数据结构揭秘一
- C#数据结构之单链表(LinkList)实例详解
- 数据结构之Treap详解
- 用C语言举例讲解数据结构中的算法复杂度结与顺序表
- C#数据结构之堆栈(Stack)实例详解
- C#数据结构之双向链表(DbLinkList)实例详解
- JavaScript数据结构和算法之图和图算法
- Java数据结构及算法实例:冒泡排序 Bubble Sort
- Java数据结构及算法实例:插入排序 Insertion Sort
- Java数据结构及算法实例:考拉兹猜想 Collatz Conjecture
- java数据结构之java实现栈
- java数据结构之实现双向链表的示例
- Java数据结构及算法实例:选择排序 Selection Sort
- Java数据结构及算法实例:朴素字符匹配 Brute Force
- Java数据结构及算法实例:汉诺塔问题 Hanoi
- Java数据结构及算法实例:快速计算二进制数中1的个数(Fast Bit Counting)