堆串的基本运算

#include <stdio.h>
#include <stdlib.h>
#include <string.h>  

typedef struct
{
    char *str;
    int length;
}HeapString;  

void InitString(HeapString *S);//串的初始化操作
void StrAssign(HeapString *S,char cstr[]);//串的赋值操作
int StrEmpty(HeapString S);//判断串是否为空
int StrLength(HeapString S);//求串的长度操作
void StrCopy(HeapString *T,HeapString S);//串的复制操作
int StrCompare(HeapString S,HeapString T);//串的比较操作
int StrInsert(HeapString *S,int pos,HeapString T);//串的插入操作
int StrDelete(HeapString *S,int pos,int len);//串的删除操作
int StrConcat(HeapString *T,HeapString S);//串的连接操作
int SubString(HeapString *Sub,HeapString S,int poos,int len);//截取子串操作
int StrReplace(HeapString *S,HeapString T,HeapString V);//串的替换操作
int StrIndex(HeapString S,int pos,HeapString T);//串的定位操作
void Str1Clear(HeapString *S);//清空串操作
void Str2Clear(HeapString *S);//摧毁串操作
void StrPrint(HeapString S);//串的输出声明  

#include "HeapString.h"  

void InitString(HeapString *S)
{
    S->length = 0;
    S->str = '\0';
}
void StrAssign(HeapString *S,char cstr[])//串的赋值操作(将常量cstr中的字符赋值给串S)
{
    int i = 0,len;
    if(S->str)
    {
        free(S->str);
    }
    for(i = 0;cstr[i]!='\0';i++)
    {
        ;
    }
    len = i;
    if(!i)
    {
        S->length = 0;
        S->str = '\0';
    }
    else
    {
        S->str = (char*)malloc(len*sizeof(char));
        if(!S->str)
        {
            exit(-1);
        }
        for(i = 0;i < len;i++)
        {
            S->str[i] = cstr[i];
        }
        S->length = len;
    }
}
int StrEmpty(HeapString S)//判断串是否为空
{
    if(S.length == 0)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
int StrLength(HeapString S)//求串的长度操作
{
    return S.length ;
}
void StrCopy(HeapString *T,HeapString S)//串的复制操作(将串S中的每一个字符赋给T)
{
    int i;
    T->str = (char*)malloc(S.length*sizeof(char));
    if(!T->str)
    {
        exit(-1);
    }
    for(i = 0;i < S.length ;i++)
    {
        T->str[i] = S.str[i];
    }
    T->length = S.length ;
}
int StrCompare(HeapString S,HeapString T)//串的比较操作
{
    int i;
    for(i = 0;i < S.length&&i < T.length ;i++)//比较两个串中的字符
    {
        if(S.str[i] != T.str[i])//如果出现字符不同,则返回两个字符的差值
        {
            return (S.str[i]-T.str[i]);
        }
    }
    return (S.length - T.length);//如果比较完毕,返回两个字符串的长度的差值
}
int StrInsert(HeapString *S,int pos,HeapString T)//串的插入操作(在串S的pos个位置插入串T)
{
    int i;
    if(pos < 0 || pos-1 > S->length)
    {
        printf("插入位置不正确\n");
        return 0;
    }
    S->str = (char*)realloc(S->str,(S->length+T.length)*sizeof(char));
    if(!S->str)
    {
        printf("内存分配失败");
        exit(-1);
    }
    for(i = S->length -1;i >= pos-1;i--)
    {
        S->str[i+T.length] = S->str[i];
    }
    for(i = 0;i < T.length ;i++)
    {
        S->str[i+pos-1] = T.str[i];
    }
    S->length = S->length + T.length;
    return 1;
}
int StrDelete(HeapString *S,int pos,int len)//串的删除操作(在串S中删除pos开始的len个字符,然后将后面的字符向前移动)
{
    int i;
    char *p;
    if(pos < 0 || len < 0 || pos+len-1 > S->length)
    {
        printf("删除位置不正确,参数len不合法\n");
        return 0;
    }
    p = (char*)malloc(S->length-len);
    if(!p)
    {
        exit(-1);
    }
    for(i = 0;i < pos-1;i++)//将串第pos位置之前的字符复制到p中
    {
        p[i] = S->str[i];
    }
    for(i = pos-1;i < S->length-len;i++)//将串第pos+len位置以后的字符复制到p中
    {
        p[i] = S->str[i+len];
    }
    S->length = S->length -len;//修改串的长度
    free(S->str);//释放原来的串S的内存空间
    S->str = p;//将串的str指向p字符串
    return 1;
}
int StrConcat(HeapString *T,HeapString S)//串的连接操作(将串S连接在串T的后面)
{
    int i;
    T->str = (char*)realloc(T->str ,(T->length +S.length )*sizeof(char));
    if(!T->str)
    {
        printf("分配空间失败");
        exit(-1);
    }
    else
    {
        for(i = T->length ;i < T->length +S.length ;i++)//将串S直接连接到T的末尾
        {
            T->str[i] = S.str[i-T->length];
        }
        T->length = T->length +S.length ;//修改串T的长度
    }
    return 1;
}
int SubString(HeapString *Sub,HeapString S,int pos,int len)//截取子串操作(截取串S中从第pos个字符开始,长度为len的连续字符,并赋值给Sub)
{
    int i;
    if(Sub->str)
    {
        free(Sub->str);
    }
    if(pos < 0 || len < 0 || pos+len-1 > S.length)
    {
        printf("参数pos和len不合法\n");
        return 0;
    }
    else
    {
        Sub->str = (char*)malloc(len*sizeof(char));
        if(!Sub->str)
        {
            printf("存储分配失败");
            exit(-1);
        }
        for(i = 0;i < len;i++)
        {
            Sub->str[i] = S.str[i+pos-1];
        }
        Sub->length = len;
        return 1;
    }
}
int StrIndex(HeapString S,int pos,HeapString T)//串的定位操作(在主串S中的第pos个位置开始查找子串T,如果主串S中存在与串T值相等的子串,返回子串在主串第pos个字符后第一次出现的位置)
{
    int i,j;
    if(StrEmpty(T))
    {
        return 0;
    }
    i = pos;
    j = 0;
    while(i < S.length && j < T.length)
    {
        if(S.str[i] == T.str[j])
        {
            i++;
            j++;
        }
        else//如果当前对应位置的字符不相等,则从串S的下一个字符开始,从T的第0个字符开始比较
        {
            i = i-j+1;
            j = 0;
        }
    }
    if(j >= T.length)//如果在串S中找到串T,则返回子串T在主串S中的位置
    {
        return i-j+1;
    }
    else
    {
        return -1;
    }
}
int StrReplace(HeapString *S,HeapString T,HeapString V)//串的替换操作(如果串S中存在子串T,则用V替换串S中的所有子串T)
{
    int flag;
    int i = 0;
    if(StrEmpty(T))
    {
        return 0;
    }
    while(i)
    {
        i = StrIndex(*S,i,T);//利用串的定位操作在串S中查找T的位置
        if(i)
        {
            StrDelete(S,i,StrLength(T));//如果找到子串T,则将S中的串T删除
            flag = StrInsert(S,i,V);//将V插入
            if(!flag)
            {
                return 0;
            }
            i += StrLength(V);
        }
    }
    return 1;
}
void StrClear1(HeapString *S)//清空串操作
{
    if(S->str)
    {
        free(S->str);
    }
    S->str = '\0';
    S->length = 0;
}
void StrClear2(HeapString *S)//摧毁串操作
{
    if(S->str)
    {
        free(S->str);
    }
}
void StrPrint(HeapString S)//串的输出声明
{
    int i;
    for(i = 0;i < S.length ;i++)
    {
        printf("%c",S.str[i]);
    }
    printf("\n");
}  

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-16 23:08:32

堆串的基本运算的相关文章

串---堆串

堆串 字符串包括串名与串值两部分,而串值采用堆串存储方法存储,串名用符号表示存储 存储方法 将一个地址连续,容量很大的存储空间作为字符串的可用空间,执行程序是动态分配 串名符号表 所有串的存储映像构成一个符号表.借助此结构可以在串名与串值之间建立一个对应关系,称为串名的存储映像 插入 #include <stdio.h> #include <stdlib.h> typedef struct { char *str; //一块连续的存储空间 int len; }HString; vo

链串的基本运算

1 #include<stdio.h> 2 #include<string.h> 3 #define CHUNKSIZE 10 4 #define stuff '#' 5 6 //串的结点类型定义 7 typedef struct Chunk 8 { 9 char ch[CHUNKSIZE]; 10 struct Chunk *next; 11 }Chunk; 12 13 //链串的类型定义 14 typedef struct 15 { 16 Chunk *head; 17 Chu

_DataStructure_C_Impl:堆串

#include<stdio.h> #include<stdlib.h> typedef struct{ char *str; int length; }HeapString; //串的赋值操作 void StrAssign(HeapString *S,char cstr[]){ int i=0,len; if(S->str) free(S->str); for(i=0;cstr[i]!='\0';i++); //求cstr字符串的长度 len=i; if(!i){ S

堆串的应用

例如串S1="Welcome to",S2="China",Sub="Xi'an",将串S2连接到串S1末尾,然后将串S1中的S2用Sub替换. #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct { char *str; int length; }HeapString; void InitString(HeapSt

4-2-串的堆存储结构-串-第4章-《数据结构》课本源码-严蔚敏吴伟民版

课本源码部分 第4章  串 - 堆串 ——<数据结构>-严蔚敏.吴伟民版        源码使用说明  链接??? <数据结构-C语言版>(严蔚敏,吴伟民版)课本源码+习题集解析使用说明        课本源码合辑  链接??? <数据结构>课本源码合辑        习题集全解析  链接??? <数据结构题集>习题解析合辑        本源码引入的文件  链接? Status.h        相关测试数据下载  链接? 无数据       文档中源码及

【数据结构】串的堆分配表示与实现

采用堆分配存储表示的串被称为堆串,与顺序串相比,地址空间仍是连续,但空间是在程序执行时动态分配的. 程序中会使用到的realloc函数: //realloc : (void *)reelloc (void *ptr,unsigned newsize); //使用 : char *str; //   str = (char *)realloc(str,20); 代码实现如下: <span style="font-size:18px;">#pragma once #includ

串-第4章-《数据结构题集》答案解析-严蔚敏吴伟民版

习题集解析部分 第4章 串 ——<数据结构题集>-严蔚敏.吴伟民版        源码使用说明  链接??? <数据结构-C语言版>(严蔚敏,吴伟民版)课本源码+习题集解析使用说明        课本源码合辑  链接??? <数据结构>课本源码合辑        习题集全解析  链接??? <数据结构题集>习题解析合辑       本习题文档的存放目录:数据结构\▼配套习题解析\▼04 串       文档中源码的存放目录:数据结构\▼配套习题解析\▼04

数据结构实践项目——串

本文针对数据结构基础系列网络课程(4):串. 1. 串的基本概念及导学 2. 串的顺序存储及其基本操作实现 3. 串的顺序存储应用 4. 串的链式存储及其基本操作实现 5. 串的模式匹配(Brute-Force算法) 6. 串的模式匹配(KMP算法) [项目1 - 建立顺序串的算法库] 定义顺序串的存储结构,实现其基本运算,并完成测试. 要求: 1.头文件sqString.h中定义数据结构并声明用于完成基本运算的函数.对应基本运算的函数包括: void StrAssign(SqString &s

数据结构之自建算法库——链串

本文针对数据结构基础系列网络课程(4):串中第4课时串的链式存储及其基本操作实现. 按照"0207将算法变程序"[视频]部分建议的方法,建设自己的专业基础设施算法库. 链队算法库采用程序的多文件组织形式,包括两个文件: 1.头文件:liString.h,包含定义链队数据结构的代码.宏定义.要实现算法的函数的声明: #ifndef LISTRING_H_INCLUDED #define LISTRING_H_INCLUDED typedef struct snode { char dat