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

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值