zoukankan      html  css  js  c++  java
  • 串的动态顺序存储代码

    由于静态顺序存储使用C语言实现有些麻烦,其次容易产生溢出,因此就不对串的静态顺序存储实现了,而是采用

    动态顺序存储,使得存储串的长度可以动态分配存储空间。下面是它的实现代码

    #include <stdio.h>
    #include <stdlib.h>
    
    #define OK 1
    
    typedef int Status;
    typedef struct {
      char *data;
      int  len;
    }String;
    
    Status strInit(String *T){
      T->data = NULL;
      T->len = 0;
      return OK;
    }
    
    //***********************//
    //       基本操作      //
    //***********************//
    
    //赋值
    Status strAssign(String *T,char *str){ if(T->data)free(T->data); int i,j; i=j=0; while(str[i]!='')i++; if(!i){T->data=NULL;T->len=0;} //empty else{ //copy T->data = (char*)malloc(i*sizeof(char)); for(j=0;j<i;j++) T->data[j]=str[j]; T->len = i; } return OK; }
    //拼接 Status strConcat(String
    *T,String S1,String S2){ if(T->data)free(T->data); int j; int i = S1.len + S2.len; T->data = (char*)malloc(i*sizeof(char)); for(j=0;j<S1.len;j++) T->data[j]=S1.data[j]; for(j=S1.len;j<i;j++) T->data[j]=S2.data[j-S1.len]; T->len = i; return OK; }
    //截取 Status subString(String
    *Sub, String S,int pos,int len){ if(pos<0 || pos>=S.len || len<0 || pos+len>S.len){ printf("In Function:subString error "); exit(1); } int i; if(Sub->data)free(Sub->data); if(!len){Sub->data=NULL;Sub->len=0;} else{ Sub->data = (char*)malloc(len * sizeof(char)); Sub->len = len; for(i=0;len>0;len--,i++) Sub->data[i] = S.data[pos++]; Sub->data[Sub->len]=''; } return OK; }
    //复制 Status strCopy(String
    *T, String S,int i){ if(i<0 || i>S.len){ printf("In Function:strCopy error "); exit(2); } int j; if(T->data)free(T->data); if(!i){T->data=NULL;T->len=0;} else{ T->data = (char*)malloc(i*sizeof(char)); for(j=0;j<i;j++) T->data[j]=S.data[j]; T->len = i; } }
    //比较
    int strCompare(String T,String S){ //compare length if(T.len != S.len)return T.len-S.len; //compare ASCII code else{ int i; for(i=0;i<T.len;i++) if(T.data[i]!=S.data[i]) return T.data[i]-S.data[i]; } return 0; } //***************************// //    复合操作    // //***************************//

    //插入
    Status strInsert(String *T,int pos,String S){ int tail_len = T->len-pos; int head_len = pos; //head_string String sub1; strInit(&sub1); strCopy(&sub1,*T,pos); //tail_string String sub2; strInit(&sub2); subString(&sub2,*T,pos,tail_len); //concat_string String tmp; strInit(&tmp); strConcat(&tmp,sub1,S); strConcat(T,tmp,sub2); return OK; }
    //删除 Status strDelete(String
    *T,int pos,int len){ if(pos<0 || pos>=T->len || len<0 || len+pos>T->len){ printf("In Function:strDelete error"); exit(1); } int i=pos; while(T->data[i]){ T->data[i] = T->data[i+len]; //overlap i++; } T->len-=len; return 0; }
    int main(){ String T,S1,S2,tmp,a,b; strInit(&T); strInit(&a); strInit(&b); strInit(&S1); strInit(&S2); strInit(&tmp); //concat strAssign(&S1,"hello "); strAssign(&S2,"world"); strConcat(&T,S1,S2); printf("%s ",T.data); //T is "hello world" //subString int pos = 3; int len = 4; subString(&S1,T,pos,len); printf("%s ",S1.data); //S1 is "lo w" //copy strCopy(&S1,T,7); printf("%s ",S1); //S1 is "hello w" //compare int res; strAssign(&a,"hello worldaCc"); strAssign(&b,"hello worldacc"); if(!(res = strCompare(a,b))) printf("two sentence is eual "); else{ if(res>0)printf("res is %d ",res); else printf("res is %d ",res); } //insert strAssign(&tmp,"III"); strInsert(&T,6,tmp); printf("len=%d:"%s" ",T.len,T.data); //T is "hello IIIworld" //delete strDelete(&T,6,3); printf("len=%d:"%s" ",T.len,T.data); ///T is "hello world" return 0; }






  • 相关阅读:
    spring中bean的高级属性之list, set, map以及props元素(含举例)
    Schema约束与DTD约束
    spring中配置Properties对象的方法
    web.xml 中的listener、 filter、servlet 加载顺序及其详解
    MySql为某个表增加rownumber
    分组取前N记录(转)
    1、Spring MVC的web.xml配置详解(转)
    Spring的PropertyPlaceholderConfigurer应用(转)
    几种任务调度的 Java 实现方法与比较(定时任务)(转)
    Spring定时任务的几种实现(转)
  • 原文地址:https://www.cnblogs.com/demonxian3/p/7471235.html
Copyright © 2011-2022 走看看