23、数据结构笔记之二十三串的堆分配实现

来源:互联网 发布:qq飞车 知乎 编辑:程序博客网 时间:2024/05/16 15:09

23、数据结构笔记之二十三串的堆分配实现

           “人的价值是由自己决定的。

           接下去是看下如何用C实现串。

 

1.  定义

定义结构体如下,一种字符数组,一个表示数组的长度。以一组地址连续的存储单元存放串值字符序列,但是他们的存储空间是在程序执行过程中动态分配而得。

typedefstruct {

    charch[MAXLEN];

    intlen;

}SString;

2.  复制

将一个串内容复制到另一个结构体中区。

SString StrCopy(SString *s,SStringt){

    inti;

    for(i= 0;i <t.len;i++)s->ch[i]=t.ch[i];

    s->len=t.len;

    return *s;

}

 

3.  字符串显示

代码简单明了

void StrShow(constSStrings){

    inti;

    for(i= 0; i <s.len; ++i)

       printf("%c",s.ch[i]);

    printf("\n");

}

 

4.  字符串插入

在第一个字符串中的指定位置插入字符串。主要是要主要插入字符串的位置,及插入后是否超过了字符串最多支持的上限。

 

void StrInsert(SString *s,intpos,SStringt){

    inti;

    if(pos< 0 )pos = 0;

    if(pos>s->len - 1)pos =s->len;

    if(s->len+t.len <=MAXLEN){// 长度适中

        for(i=s->len +t.len- 1;i >pos;i--)

            s->ch[i]=s->ch[i -t.len];

        for(i= 0;i <t.len;i++)s->ch[i+pos] =t.ch[i];

        s->len+=t.len;

    }

    elseif(pos +t.len>MAXLEN) {// 长度超限1,pos后不再有s的内容

        for(i=pos;i <MAXLEN;++i)

            s->ch[i]=t.ch[i -pos];

        s->len=MAXLEN;

    }

    else {//长度超限2,从pos+ t.len后还有s的部分内容

        for(i= MAXLEN - 1; i >=pos +t.len;--i)

            s->ch[i]=s->ch[i -t.len];

        for(i= 0; i <t.len; ++i)

            s->ch[i+pos] =t.ch[i];

        s->len=MAXLEN;

    }

}

5.  MAIN函数

int main() {

    SStringstra = {"abcdefghijklmnopqrstuvwxyz",26};

    SStringstrb = {"01234567890123456789",20},strc;

   StrCopy(&strc,stra);

    printf("a:\n");

   StrShow(stra);

    printf("b:\n");

   StrShow(strb);

   StrInsert(&stra,22,strb); //

    printf("a:\n");

   StrShow(stra);

    printf("c:\n");

   StrShow(strc);

    return0;

}

 

结果如下图1所示:

 

 

 

 

 

 

 

6.  源码

#include<stdio.h>

#defineMAXLEN40

 

typedefstruct {

    charch[MAXLEN];

    intlen;

}SString;

 

void StrInsert(SString *s,intpos,SStringt){

    inti;

    if(pos< 0 )pos = 0;

    if(pos>s->len - 1)pos =s->len;

    if(s->len+t.len <=MAXLEN){// 长度适中

        for(i=s->len +t.len- 1;i >pos;i--)

            s->ch[i]=s->ch[i -t.len];

        for(i= 0;i <t.len;i++)s->ch[i+pos] =t.ch[i];

        s->len+=t.len;

    }

    elseif(pos +t.len>MAXLEN) {// 长度超限1,pos后不再有s的内容

        for(i=pos;i <MAXLEN;++i)

            s->ch[i]=t.ch[i -pos];

        s->len=MAXLEN;

    }

    else {//长度超限2,从pos+ t.len后还有s的部分内容

        for(i= MAXLEN - 1; i >=pos +t.len;--i)

            s->ch[i]=s->ch[i -t.len];

        for(i= 0; i <t.len; ++i)

            s->ch[i+pos] =t.ch[i];

        s->len=MAXLEN;

    }

}

 

SString StrCopy(SString *s,SStringt){

    inti;

    for(i= 0;i <t.len;i++)s->ch[i]=t.ch[i];

    s->len=t.len;

    return *s;

}

 

void StrShow(constSStrings){

    inti;

    for(i= 0; i <s.len; ++i)

       printf("%c",s.ch[i]);

    printf("\n");

}

 

int main() {

    SStringstra = {"abcdefghijklmnopqrstuvwxyz",26};

    SStringstrb = {"01234567890123456789",20},strc;

   StrCopy(&strc,stra);

    printf("a:\n");

   StrShow(stra);

    printf("b:\n");

   StrShow(strb);

   StrInsert(&stra,22,strb); //

    printf("a:\n");

   StrShow(stra);

    printf("c:\n");

   StrShow(strc);

    return0;

}

阅读全文
0 0
原创粉丝点击