YTU 3008: 链串的基本运算
3008: 链串的基本运算
时间限制: 1 Sec 内存限制: 128 MB
提交: 1 解决: 1
题目描述
编写一个程序,实现链串的各种基本运算,主函数已给出,请补充每一种方法。
1、建立串s="abcdefghefghijklmn",和串s1="xyz";
2、输出串s;
3、输出串s的长度;
4、在串s的第九个字符位置插入串s1而产生s2;
5、输出串s2;
6、删除串s的第二个字符开始的五个字符而产生串s2;
7、输出串s2;
8、将串s第二个字符开始的五个字符替换成串s1而产生串s2;
9、输出串s2;
10、提取串s的第二个字符开始的10个字符而产生s2;
11、输出串s3;
12、将串s1和串s2连接起来而产生串s3;
13、输出串s3;
链串的定义为
输入
输出
样例输出
abcdefghefghijklmn
18
abcdefghxyzefghijklmn
aghefghijklmn
axyzghefghijklmn
bcdefghefg
xyzbcdefghefg
提示
请使用C++编译并提交
迷失在幽谷中的鸟儿,独自飞翔在这偌大的天地间,却不知自己该飞往何方…
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Node
{
char data;
struct Node *next;
} LiString;
void StrAssign(LiString *&s,char cstr[])
{
int i;
LiString *r,*p;
s=(LiString *)malloc(sizeof(LiString));
r=s;
for(i=0; i<(int)strlen(cstr); 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)
{
printf("%c",p->data);
p=p->next;
}
printf("\n");
}
void StrLength(LiString *s)
{
int i=0;
LiString *p=s->next;
while(p!=NULL)
{
i++;
p=p->next;
}
printf("%d\n",i);
}
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;
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)
{
int k;
LiString *str,*p=s->next,*q,*r;
str=(LiString*)malloc(sizeof(LiString));
str->next=NULL;
r=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)
{
int k;
LiString *str,*p=s->next,*p1=t->next,*q,*r;
str=(LiString*)malloc(sizeof(LiString));
str->next=NULL;
r=str;
for(k=0; k<i-1; k++)
{
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=p->next;
while(p1!=NULL)
{
q=(LiString*)malloc(sizeof(LiString));
q->data=p1->data;
q->next=NULL;
r->next=q;
r=q;
p1=p1->next;
}
while(p!=NULL)
{
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;
}
LiString *TiStr(LiString *s,int i,int j)
{
LiString *str;
int k=0,m=0;
char c[50]= {0};
while(s!=NULL)
{
k++;
if(k<=i+j&&k>i)c[m++]=s->data;
s=s->next;
}
StrAssign(str,c);
return str;
}
LiString *Concat(LiString *s,LiString *t)
{
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;
char c[]="abcdefghefghijklmn",d[]="xyz";
StrAssign(s,c); //建立串
StrAssign(s1,d);
DispStr(s); //输出串
StrLength(s);
s2=InsStr(s,9,s1); //将s1插入到s的第九个位置
DispStr(s2);
s2=DelStr(s,2,5); //删除s第二个字符开始的五个字符
DispStr(s2);
s2=RepStr(s,2,5,s1); //将串s第二个字符开始的五个字符替换成串s1
DispStr(s2);
s2=TiStr(s,2,10); //提取串s的第二个字符开始的10个字符
DispStr(s2);
s3=Concat(s1,s2); //将串s1和串s2连接起来
DispStr(s3);
return 0;
}
YTU 3008: 链串的基本运算的更多相关文章
- YTU 3007: 顺序串的基本运算
3007: 顺序串的基本运算 时间限制: 1 Sec 内存限制: 128 MB 提交: 1 解决: 1 题目描述 编写一个程序,实现顺序串的各种基本运算,主函数已给出,请补充每一种方法. 1.建立 ...
- _DataStructure_C_Impl:链串
//_DataStructure_C_Impl:链串 #include<stdio.h> #include<stdlib.h> #include<string.h> ...
- python学习1-字符串数字基本运算以及if条件和while循环
python学习1-字符串数字基本运算以及if条件和while循环 字符串表达形式共四种: name = "string" name = 'string' name = " ...
- C语言——循环队列和链队列的基本运算
// 循环队列#include <stdio.h> #include "SeqQue.h" // 循环队列的基本运算 /* const int maxsize = 20 ...
- 数据结构(c语言第2版)-----了解链表,栈,队列,串
关于链表我觉得这都是最基本的东西,但是不常见,在实际的应用中很少的使用,了解它会用就OK,不需要研究的那么深,除非做那种内存压缩,存储方面工作. C语言中动态申请空间 malloc() q=(dlin ...
- JavaScript责任链模式
介绍 责任链模式(Chain of responsibility)是使多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系.将对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理 ...
- 【数据结构(C语言版)系列四】 串
串类型的定义 串(或字符串)是由零个或多个字符组成的有限序列,一般记为 s = 'a1a2...an',s为串名.子串在主串中的位置以子串的第一个字符在主串中的位置来表示. 串和表示和实现——定长顺序 ...
- RxJava如何结合观察者与链式处理
RxJava如何结合观察者与链式处理 Author: Dorae Date: 2018年12月3日17:10:31 转载请注明出处 一.概述 首先问自己几个问题,如果非常清楚这几个问题的目的与答案,那 ...
- nginx应用总结(1)--基础认识和应用配置
在linux系统下使用nginx作为web应用服务,用来提升网站访问速度的经验已五年多了,今天在此对nginx的使用做一简单总结. 一.nginx服务简介Nginx是一个高性能的HTTP和反向代理服务 ...
随机推荐
- linux:ACL权限
ACL权限是为了防止权限不够用的情况,一般的权限有所有者.所属组.其他人这三种,当这三种满足不了我们的需求的时候就可以使用ACL权限: 比如:一个网络老师,给一个班的学员上课,他在linux的根目录下 ...
- SQL百分比
百分比=cast(cast(count(ProvinceName)*100./ (select count(Id) from dbo.TopicCurrent where boardId=316 ...
- PostgreSQL 非持久化设置(Non-Durable Settings)
Durability is a database feature that guarantees the recording of committed transactions even if the ...
- c语言的基本语法
1. 二目运算符从右往左 优先级 运算符 名称或含义 使用形式 结合方向 说明 1 [] 数组下标 数组名[常量表达式] 左到右 () 圆括号 (表达式)/函数名(形参表) . 成员选择(对象 ...
- Leetcode: Word Pattern II
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- 免费VPN 实测可用
vpngate.net 的镜像站点列表 (更新于 2014-05-18 03:06:00 UTC): http://121.135.220.121:26633/cn/ (Mirror location ...
- sdutoj 2154 Shopping
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2154 Shopping Time Limit: ...
- javaapi中的排序
有的时候需要对数组里的element进行排序.当然可以自己编写合适的排序方法,但既然java包里有自带的Arrays.sort排序方法,在 数组元素比较少的时候为何不用? Sorting an Arr ...
- 在 msbuild 生成时,每次都生成很多css,js,img 文件,如果不想每次编译都有这些需要这样配置
在 msbuild 不想生成一大堆文件,可以在 Web 项目的csproj 文件里,注释掉这一串. <!--<Import Project="$(VSToolsPath)\Web ...
- PHP中9大缓存技术总结(转载 http://www.php100.com/html/php/lei/2015/0919/8969.html)
PHP中9大缓存技术总结 来源: 时间:2015-09-19 02:40:33 阅读数:57767 分享到: 12 [导读] 1.全页面静态化缓存也就是将页面全部生成html静态页面,用户访问 ...