zoukankan      html  css  js  c++  java
  • 《串---堆存储结构》

    //堆分配存储表示
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<malloc.h>
    #define TRUE 1
    #define FALSE 0
    #define OK 1
    #define ERROR 0
    #define INFEASIBLE -1
    #define OVERFLOW -2
    typedef int Status;
    typedef struct
    {
        char *ch;    //若是非空串,则按串长分配存储区,否则ch为NULL
        int length;    //串长度
    }HString;
    
    //初始化产生空串T
    void InitString(HString &T)
    {
        T.ch = NULL;
        T.length = 0;
    }
    //若S为空串,则返回OK,否则返回ERROR
    Status StrEmpty(HString T)
    {
        if((T.length==0) && (T.ch = NULL))
            return OK;
        else
            return ERROR;
    }
    //生成一个其值等于串常量chars的串T
    Status StrAssign(HString &T,char *chars)
    {
        int i,j;
        if(T.ch)    free(T.ch);        //释放T原有空间
        i = strlen(chars);            //求chars的长度
        if(!i)
        {//chars的长度为0
            T.ch = NULL;
            T.length = 0;
        }
        else
        {//chars的长度不为0
            T.ch = (char *)malloc(i*sizeof(char));    //分配串空间
            if(!T.ch)    exit(OVERFLOW);                //分配串空间失败
            for(j = 0;j<i;j++)                        //拷贝串
                T.ch[j] = chars[j];
            T.length = i;
        }
        return OK;
    }
    //求串的长度
    int StrLength(HString S)
    {
        return S.length;
    }
    //输出字符串
    void StrPrint(HString T)
    {
        int i;
        for(i=0;i<T.length;i++)
            printf("%c",T.ch[i]);
        printf("
    ");
    }
    //串比较
    int StrCompare(HString S,HString T)
    {
        int i;
        for(i=0;i<S.length&&i<T.length;++i)
            if(S.ch[i]!=T.ch[i])
                return S.ch[i] - T.ch[i];
        return S.length - T.length;
    }
    //复制字符串
    int StrCopy(HString &T,HString S)
    {
        int i;
        if(T.ch)
            free(T.ch);    //释放T原有空间
        T.ch = (char *)malloc(S.length*sizeof(char));    //分配串空间
        if(!T.ch)
            exit(0);    //分配串空间失败
        for(i=0;i<S.length;i++)
            T.ch[i] = S.ch[i];
        T.length = S.length;
        return OK;
    }
    //将S清空为空串
    Status ClearString(HString &S)
    {
        if(S.ch)
        {
            free(S.ch);
            S.ch = NULL;
        }
        S.length = 0;
        return OK;
    }
    //用T返回由S1和S2连接而成的新串
    Status Concat(HString &T,HString S1,HString S2)
    {
        int i;
        if(T.ch)    free(T.ch);        //释放T原有的空间
        T.length = S1.length+S2.length;
        T.ch = (char*)malloc(T.length*sizeof(char));
        if(!T.ch)    exit(OVERFLOW);
        for(i=0;i<S1.length;i++)
            T.ch[i] = S1.ch[i];
        for(i=0;i<S2.length;i++)
            T.ch[S1.length+i] = S2.ch[i];
        return OK;
    }
    //求子串
    Status SubString(HString &Sub,HString S,int pos,int len)
    {
        int i;
        if(pos<1 || pos>S.length || len<0 || len>S.length+pos+1)
            return ERROR;
        if(Sub.ch)    free(Sub.ch);    //释放旧空间
        if(!len)
        {//空子串
            Sub.ch = NULL;
            Sub.length = 0;
        }
        else
        {//完整子串
            Sub.ch = (char*)malloc(len*sizeof(char));
            if(!Sub.ch)        exit(OVERFLOW);        //子串空间分配失败
            for(i=0;i<len;i++)
                Sub.ch[i] = S.ch[pos-1+i];
            Sub.length = len;
        }
        return OK;
    }
    //算法4.1
    //T为非空串,若主串S中第pos个字符之后存在于T相等的子串
    //则返回第一个这样的子串在S中的位置,否则返回0
    int Index(HString S,HString T,int pos)
    {
        int m,n,i;
        HString sub;
        InitString(sub);
        if(pos>0)
        {
            n = StrLength(S);
            m = StrLength(T);
            i = pos;
            while(i<n-m+1)
            {
                SubString(sub,S,i,m);
                if(StrCompare(sub,T)!=0)
                    ++i;
                else
                    return i;
            }
        }
        return 0;
    }
    //串插入操作
    //在串S的第pos个字符之前插入串T
    Status StrInsert(HString &S,int pos,HString T)
    {
        int i;
        if(pos<1 || pos>S.length+1)
            return ERROR;
        if(T.length)
        {//T非空,则重新分配空间,插入T
            if(!(S.ch=(char *)realloc(S.ch,(S.length+T.length)+sizeof(char))))
                exit(OVERFLOW);
            for(i=S.length-1;i>=pos-1;--i)        //为插入T腾出位置
                S.ch[i+T.length] = S.ch[i];
            for(i=0;i<T.length;i++)
                S.ch[pos-1+i] = T.ch[i];
            S.length+=T.length;
        }
        return OK;
    }
    //从串S中删除第pos个字符起长度为len的子串
    int StrDelete(HString &S,int pos,int len)
    {
        int i;
        if(S.length<pos+len-1)
            exit(0);
        for(i=pos-1;i<=S.length-len;i++)
            S.ch[i] = S.ch[i+len];
        S.length -=len;
        S.ch=(char*)realloc(S.ch,S.length*sizeof(char));
        return OK;
    }
    //用V替换主串S中出现的所有与T相等的不重叠的子串
    int Replace(HString &S,HString T,HString V)
    {
        int i = 1;    //从串S的第一个字符起查找串T
        if(StrEmpty(T))    //T是空串
            return ERROR;
        do
        {
            i = Index(S,T,i);
            if(i)    //串S中存在串T
            {
                StrDelete(S,i,StrLength(T));    //删除该串T
                StrInsert(S,i,V);    //在原串T的位置插入串V
                i+=StrLength(V);
            }
        }while(i);
            return OK;
    }
    
    
    
    
    
    int main()
    {
        int i;
        char c,*p="God bye!",*q = "God luck!";
        HString t,s,r;
    
        InitString(t);
        InitString(s);
        InitString(r);
    
        StrAssign(t,p);
        printf("串t为:");
        StrPrint(t);
        printf("串长为%d 串空否?%d(1:空 0:否)
    ",StrLength(t),StrEmpty(t));
        StrAssign(s,q);
        printf("串s为:");
        StrPrint(s);
        i = StrCompare(s,t);
        if(i<0)
            c='<';
        else if(i==0)
            c='=';
        else
            c='>';
        printf("串s%c串t
    ",c);
        Concat(r,t,s);
        printf("串t连接串s产生的串r为:");
        StrPrint(r);
        StrAssign(s,"oo");
        printf("串s为:");
        StrPrint(s);
        StrAssign(t,"o");
        printf("串t为:");
        StrPrint(t);
        Replace(r,t,s);
        printf("把串r中和串t中相同的子串用s代替后,串r为:
    ");
        StrPrint(r);
        ClearString(s);
        printf("串s清空后,串长为%d 空否?%d(1:空 0:否)
    ",StrLength(s),StrEmpty(s));
        SubString(s,r,6,4);
        printf("串s为从串r的第6个字符起的4个字符,长度为%d串s为:",s.length);
        StrPrint(s);
        StrCopy(t,r);
        printf("复制串t为串r,串t为:");
        StrPrint(t);
        StrInsert(t,6,s);
        printf("在串t的第6个字符插入串s后,串t为:");
        StrPrint(t);
        StrDelete(t,1,5);
        printf("从串t的第1个字符起删除5个字符后,串t为:");
        StrPrint(t);
        printf("%d是从串t的第1个字符起,和串s相同的第1个子串的位置
    ",Index(t,s,1));
        printf("%d是从串t的第2个字符起,和串s相同的第1个子串的位置
    ",Index(t,s,2));
        system("pause");
        return 0;
    }

     

  • 相关阅读:
    CABasicAnimation 使用
    CABasicAnimation(CAKeyframeAnimation)keypath 取值
    c++的应用领域
    QT 状态机详解 statemachine (转)
    C++默认实参
    String隐式共享
    可重入函数与不可重入函数
    堆和栈的区别(转过无数次的文章)
    Qt Model/View(转)
    C++虚函数和纯虚函数
  • 原文地址:https://www.cnblogs.com/sun-/p/4923070.html
Copyright © 2011-2022 走看看