第八周项目2-建立链串的算法库
来源:互联网 发布:python自动生成试卷 编辑:程序博客网 时间:2024/05/16 06:49
- /*
- * Copyright (c) 2016, 烟台大学计算机与控制工程学院
- * All rights reserved。
- * 文件名称 :2.cpp
- * 作 者 :杨俊杰
- * 完成日期 :2016年 11月3日
- * 版 本 号 :v1.0
- * 问题描述 :定义链串的存储结构,实现串的基本运算,并完成测试
- * 输入描述 :
- * 输出描述 :
- */
[cpp] view plain copy
- //1.头文件sqString.h中定义数据结构并声明用于完成基本运算的函数。
- #ifndef SqString_H_INCLUDED
- #define SqString_H_INCLUDED
- typedef struct snode
- {
- char data;
- struct snode *next;
- } LiString;
- void StrAssign(LiString *&s,char cstr[]); //字符串常量cstr赋给串s
- void StrCopy(LiString *&s,LiString *t); //串t复制给串s
- bool StrEqual(LiString *s,LiString *t); //判串相等
- int StrLength(LiString *s); //求串长
- LiString *Concat(LiString *s,LiString *t); //串连接
- LiString *SubStr(LiString *s,int i,int j); //求子串
- LiString *InsStr(LiString *s,int i,LiString *t) ; //串插入
- LiString *DelStr(LiString *s,int i,int j); //串删去
- LiString *RepStr(LiString *s,int i,int j,LiString *t); //串替换
- void DispStr(LiString *s); //输出串
- #endif // SqString_H_INCLUDED
[cpp] view plain copy
- //2.在sqString.cpp中实现这些函数
- #include<stdio.h>
- #include<malloc.h>
- #include"sqString.h"
- void StrAssign(LiString *&s,char cstr[]) //字符串常量cstr赋给串s
- {
- int i;
- LiString *r,*p;
- s=(LiString *)malloc(sizeof(LiString));
- r=s; //r始终指向尾节点
- 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 StrCopy(LiString *&s,LiString *t) //串t复制给串s
- {
- LiString *p=t->next,*q,*r;
- s=(LiString *)malloc(sizeof(LiString));
- r=s; //r始终指向尾节点
- while (p!=NULL) //将t的所有节点复制到s
- { q=(LiString *)malloc(sizeof(LiString));
- q->data=p->data;
- r->next=q;r=q;
- p=p->next;
- }
- r->next=NULL;
- }
- bool StrEqual(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)
- return true;
- else
- return false;
- }
- int StrLength(LiString *s) //求串长
- {
- int i=0;
- LiString *p=s->next;
- while (p!=NULL)
- { i++;
- p=p->next;
- }
- return i;
- }
- LiString *Concat(LiString *s,LiString *t) //串连接
- {
- LiString *str,*p=s->next,*q,*r;
- str=(LiString *)malloc(sizeof(LiString));
- r=str;
- while (p!=NULL) //将s的所有节点复制到str
- { q=(LiString *)malloc(sizeof(LiString));
- q->data=p->data;
- 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;
- 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; //r指向新建链表的尾节点
- 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=1;k<=j;k++) //将s的第i个节点开始的j个节点复制到str
- { q=(LiString *)malloc(sizeof(LiString));
- q->data=p->data;
- r->next=q;r=q;
- p=p->next;
- }
- r->next=NULL;
- return str;
- }
- LiString *InsStr(LiString *s,int i,LiString *t) //串插入
- {
- int k;
- LiString *str,*p=s->next,*p1=t->next,*q,*r;
- str=(LiString *)malloc(sizeof(LiString));
- str->next=NULL;
- r=str; //r指向新建链表的尾节点
- if (i<=0 || i>StrLength(s)+1) //参数不正确时返回空串
- return str;
- for (k=1;k<i;k++) //将s的前i个节点复制到str
- { q=(LiString *)malloc(sizeof(LiString));
- q->data=p->data;
- r->next=q;r=q;
- p=p->next;
- }
- while (p1!=NULL) //将t的所有节点复制到str
- { q=(LiString *)malloc(sizeof(LiString));
- q->data=p1->data;
- r->next=q;r=q;
- p1=p1->next;
- }
- while (p!=NULL) //将*p及其后的节点复制到str
- { 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) //串删去
- {
- int k;
- LiString *str,*p=s->next,*q,*r;
- str=(LiString *)malloc(sizeof(LiString));
- str->next=NULL;
- r=str; //r指向新建链表的尾节点
- if (i<=0 || i>StrLength(s) || j<0 || i+j-1>StrLength(s))
- return str; //参数不正确时返回空串
- for (k=0;k<i-1;k++) //将s的前i-1个节点复制到str
- { q=(LiString *)malloc(sizeof(LiString));
- q->data=p->data;
- r->next=q;r=q;
- p=p->next;
- }
- for (k=0;k<j;k++) //让p沿next跳j个节点
- p=p->next;
- while (p!=NULL) //将*p及其后的节点复制到str
- { 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) //串替换
- {
- int k;
- LiString *str,*p=s->next,*p1=t->next,*q,*r;
- str=(LiString *)malloc(sizeof(LiString));
- str->next=NULL;
- r=str; //r指向新建链表的尾节点
- if (i<=0 || i>StrLength(s) || j<0 || i+j-1>StrLength(s))
- return str; //参数不正确时返回空串
- for (k=0;k<i-1;k++) //将s的前i-1个节点复制到str
- { q=(LiString *)malloc(sizeof(LiString));
- q->data=p->data;q->next=NULL;
- r->next=q;r=q;
- p=p->next;
- }
- for (k=0;k<j;k++) //让p沿next跳j个节点
- 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;
- }
- r->next=NULL;
- return str;
- }
- void DispStr(LiString *s) //输出串
- {
- LiString *p=s->next;
- while (p!=NULL)
- { printf("%c",p->data);
- p=p->next;
- }
- printf("\n");
- }
[cpp] view plain copy
- //3.在main函数中完成测试
- #include <stdio.h>
- #include "sqString.h"
- int main()
- {
- LiString *s,*s1,*s2,*s3,*s4;
- printf("链串的基本运算如下:\n");
- printf(" (1)建立串s和串s1\n");
- StrAssign(s,"abcdefghijklmn");
- printf(" (2)输出串s:");
- DispStr(s);
- StrAssign(s1,"123");
- printf(" (2)输出串s1:");
- DispStr(s1);
- printf(" (3)串s的长度:%d\n",StrLength(s));
- printf(" (4)在串s的第9个字符位置插入串s1而产生串s2\n");
- s2=InsStr(s,9,s1);
- printf(" (5)输出串s2:");
- DispStr(s2);
- printf(" (6)删除串s第2个字符开始的5个字符而产生串s2\n");
- s2=DelStr(s,2,3);
- printf(" (7)输出串s2:");
- DispStr(s2);
- printf(" (8)将串s第2个字符开始的5个字符替换成串s1而产生串s2\n");
- s2=RepStr(s,2,5,s1);
- printf(" (9)输出串s2:");
- DispStr(s2);
- printf(" (10)提取串s的第2个字符开始的10个字符而产生串s3\n");
- s3=SubStr(s,2,10);
- printf(" (11)输出串s3:");
- DispStr(s3);
- printf(" (12)将串s1和串s2连接起来而产生串s4\n");
- s4=Concat(s1,s2);
- printf(" (13)输出串s4:");
- DispStr(s4);
- return 0;
- }
运行结果:
0 0
- 第八周 项目2-建立链串的算法库
- 第八周 项目2 建立链串的算法库
- 第八周项目2 - 建立链串的算法库
- 第八周项目2-建立链串的算法库
- 第八周项目2-建立链串的算法库
- 第八周-项目2 - 建立链串的算法库
- 第八周 项目2-建立链串的算法库
- 第八周项目2--建立链串的算法库
- 第八周 项目2-建立链串的算法库
- 第八周项目2 - 建立链串的算法库
- 第八周项目2-建立链串的算法库
- 第八周 项目2-建立链串的算法库
- 第八周项目2--建立链串的算法库
- 第八周 项目2-建立链串的算法库
- (第八周项目2)建立链串的算法库
- 第八周项目2 建立链串的算法库
- 第八周项目2--建立链串的算法库
- 第八周--项目2-建立链串的算法库
- HTML怎么样才有利于SEO搜索?
- LeetCode笔记:437. Path Sum III
- 版本控制系统中的必须设定内容忽略文件
- Poedu_C语言_lesson22_20160927_函数2
- 第十周--二叉树遍历的递归算法
- 第八周项目2-建立链串的算法库
- 第9周 项目1 猴子选大王(数组版)
- 第十周【项目二-二叉树遍历的递归算法】
- android 温故知新之activity
- 第十周 项目1-二叉树算法库
- python学习(12)处理文件
- 第十周项目1-二叉树算法库
- Android7.0适配教程与心得
- leetcode-59 Spiral Matrix II