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;

链串的定义为

typedef struct Node
{
    char data;
    struct Node *next;
} LiString;
 
主函数:
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;
}

输入

输出

样例输出

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: 链串的基本运算的更多相关文章

  1. YTU 3007: 顺序串的基本运算

    3007: 顺序串的基本运算 时间限制: 1 Sec  内存限制: 128 MB 提交: 1  解决: 1 题目描述 编写一个程序,实现顺序串的各种基本运算,主函数已给出,请补充每一种方法. 1.建立 ...

  2. _DataStructure_C_Impl:链串

    //_DataStructure_C_Impl:链串 #include<stdio.h> #include<stdlib.h> #include<string.h> ...

  3. python学习1-字符串数字基本运算以及if条件和while循环

    python学习1-字符串数字基本运算以及if条件和while循环 字符串表达形式共四种: name = "string" name = 'string' name = " ...

  4. C语言——循环队列和链队列的基本运算

    // 循环队列#include <stdio.h> #include "SeqQue.h" // 循环队列的基本运算 /* const int maxsize = 20 ...

  5. 数据结构(c语言第2版)-----了解链表,栈,队列,串

    关于链表我觉得这都是最基本的东西,但是不常见,在实际的应用中很少的使用,了解它会用就OK,不需要研究的那么深,除非做那种内存压缩,存储方面工作. C语言中动态申请空间 malloc() q=(dlin ...

  6. JavaScript责任链模式

    介绍 责任链模式(Chain of responsibility)是使多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系.将对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理 ...

  7. 【数据结构(C语言版)系列四】 串

    串类型的定义 串(或字符串)是由零个或多个字符组成的有限序列,一般记为 s = 'a1a2...an',s为串名.子串在主串中的位置以子串的第一个字符在主串中的位置来表示. 串和表示和实现——定长顺序 ...

  8. RxJava如何结合观察者与链式处理

    RxJava如何结合观察者与链式处理 Author: Dorae Date: 2018年12月3日17:10:31 转载请注明出处 一.概述 首先问自己几个问题,如果非常清楚这几个问题的目的与答案,那 ...

  9. nginx应用总结(1)--基础认识和应用配置

    在linux系统下使用nginx作为web应用服务,用来提升网站访问速度的经验已五年多了,今天在此对nginx的使用做一简单总结. 一.nginx服务简介Nginx是一个高性能的HTTP和反向代理服务 ...

随机推荐

  1. Protostuff序列化

    前言: Java序列化是Java技术体系当中的一个重要议题,序列化的意义在于信息的交换和存储,通常会和io.持久化.rmi技术有关(eg:一些orm框架会要求持久化的对象类型实现Serializabl ...

  2. c# Start/Stop/Check Status远程计算机的Windows Service

    static void Main(string[] args) { ConnectionOptions op = new ConnectionOptions(); // 登陆远程计算机的远程, op. ...

  3. Lintcode: Flip Bits

    Determine the number of bits required to flip if you want to convert integer n to integer m. Have yo ...

  4. 输入和输出的总结(c语言)

    c语言中有多种的输入和输出方式,下面就简单总结一下: 一.输入的三种方式 (1)scanf scanf 函数可以在变量中使用,也可以在数组中使用,当然指针上也能用到,是一个很好的输入函数.scanf是 ...

  5. Java基础(57):Eclipse中环境配置(视图字体颜色行号调试快捷键等等)

    1:Eclipse的基本配置 A:程序的编译和运行的环境配置(一般不改) window -- Preferences -- Java 编译环境:Compiler 默认选中的就是最高版本. 运行环境:I ...

  6. Hibernate的关系配置

    一对一: <hibernate-mapping> <class name="cn.jnit.bean.User" table="T_user" ...

  7. spring da-y02-go1

    使用构造器实例化bean: springday02:(同样导入5个基本jar包)1.复制xml文件到ioc包下2.B类,实现无参构造器3.A类,B是它的成员变量,实现无参和set/get方法4.修改x ...

  8. 11---Net基础加强

    替换邮箱用户名部分: using System; using System.Collections.Generic; using System.Linq; using System.Text; usi ...

  9. 如何设置DB2I(SPUFI)来正常工作

    首先确定你现在所使用的登录proc,确保有权限可以在对应的PDS内新建member,可以在s.st里面找userid对应的job,然后去serach using,基本可以找到对应的dataset 用t ...

  10. 《Focus On 3D Terrain Programming》中一段代码的注释二

    取自<Focus On 3D Terrain Programming>中的一段: bool CTERRAIN::MakeTerrainFault( int iSize, int iIter ...