堆串

作者: star_night | 来源:发表于2017-10-11 21:13 被阅读0次
//堆串
#include<stdio.h>
#include<stdlib.h>
typedef struct{
  char *ch;
  int len;
}HString;

HString *InitHStr();//初始化
int HStrAssign(HString *S, char *chars);//赋值
int HStrInsert(HString *s, int pos, HString *t);//插入
int HStrDelete(HString *s, int pos, int len);//删除
int HStrCat(HString *s, HString *t);//连接
int SubHString(HString *t, HString *s, int pos, int len);//子串
void HStrPrint(HString *s);//打印堆串

int main(){
  HString *h = InitHStr();
  char strA[]="hello world!";
  //赋值
  HStrAssign(h,strA);
  HStrPrint(h);
  printf("\n");
  //插入
  HStrInsert(h,7,h);
  HStrPrint(h);
  printf("\n");
  //删除
  HStrDelete(h,1,6);
  HStrPrint(h);
  printf("\n");
  //连接
  char strB[] = " and hello C!";
  HString *h2 = InitHStr();
  HStrAssign(h2,strB);
  HStrCat(h,h2);
  HStrPrint(h);
  printf("\n");
  //子串
  HString *h3 = InitHStr();
  SubHString(h3,h,7,5);
  HStrPrint(h);
  printf("\n");
  HStrPrint(h3);
  printf("\n");
  return 0;
}

HString *InitHStr(){
  HString *p = (HString *)malloc(sizeof(HString));
  p->ch = NULL;
  p->len = 0;
  return p;
}

int HStrAssign(HString *S, char *chars){
  int i = 0;
  if (chars == NULL || S == NULL) return 0;
  while (chars[i] != '\0') i++;//记录chars字符串的长度
  S->len = i;
  if (S->len != 0){
    if (S->ch != NULL) free(S->ch);
    S->ch = (char*)malloc((S->len + 1) * sizeof(char));
    if (S->ch == NULL) return 0;
    for (i = 1; i <= S->len; i++)
      S->ch[i] = chars[i - 1];
  }else{
    S->ch = NULL;
  }
  return 1;
}

int HStrInsert(HString *s, int pos, HString *t){
    int i;
    char *temp;
    if (s == NULL || s->ch == NULL || t->ch == NULL || pos > s->len || pos < 1)
      return 0;
    temp = (char*)malloc((s->len + t->len + 1) * sizeof(char));
    if(temp == NULL) return 0;
    for (i = 1; i < pos; i++)//插入点之前的字符串
      temp[i] = s->ch[i];
    for (i = pos; i < pos + t->len; i++)//要插入的字符串
      temp[i] = t->ch[i - pos + 1];
    for (i = pos + t->len; i <= s->len + t->len; i++)//剩余的原字符串
      temp[i] = t->ch[i - t->len];
    free(s->ch);
    s->ch = temp;
    s->len = s->len + t->len;
    return 1;
}

int HStrDelete(HString *s, int pos, int len){
  int i;
  char *temp;
  if (s == NULL || s->ch == NULL || len < 0 || pos < 1 || len > s->len - pos + 1)
    return 0;
  temp = (char*)malloc((s->len - len + 1) * sizeof(char));
  if(temp == NULL) return 0;
  for(i = 1; i < pos; i++)
    temp[i] = s->ch[i];
  for(i = pos; i < s->len - len; i++)
    temp[i] = s->ch[i + len];
  free(s->ch);
  s->ch = temp;
  s->len = s->len - len;
  return 1;
}

int HStrCat(HString *s, HString *t){
  int i;
  if(s == NULL || s->ch == NULL || t->ch == NULL)
    return 0;
  s->ch = (char*)realloc(s->ch, (s->len + t->len + 1) * sizeof(char));
  if(s->ch == NULL) return 0;
  for(i = s->len + 1; i <= t->len + s->len; i++)
    s->ch[i] = t->ch[i - s->len];
  s->len = s->len + t->len;
  return 1;
}

int SubHString(HString *t, HString *s, int pos, int len){
  int i;
  if(t == NULL || s->ch == NULL || len < 0 || len > s->len - pos + 1 || pos < 1 || pos > s->len)
    return 0;
  t->len = len;
  if(t->ch != NULL) free(t->ch);
  t->ch = (char*)malloc((t->len + 1) * sizeof(char));
  if(t->ch == NULL) return 0;
  for(i = 1; i <= t->len; i++){
    t->ch[i] = s->ch[pos + i - 1];
  }
  return 1;
}

void HStrPrint(HString *s){
  int i;
  for(i = 1; i <= s->len; i++)
    printf("%c", s->ch[i]);
}

相关文章

  • 堆串

  • JVM知识

    java字符串在内存中:存放于堆中或者字符串常量区 堆内存 堆内存分为Permanent Space(持久代) 和...

  • 三. 串与矩阵

    一. 串 字符串大多使用堆分配存储, 堆由C语言的动态分配函数malloc和free来管理. 串的模式匹配 问题:...

  • 堆和字符串

    堆上 结果: 栈上 结果:

  • NSString篇

    1.常量字符串与堆字符串 常量区中的字符串只要内容一致, 不会重复创建,与C语言中的一致 堆中字符串对象 2.字符...

  • Java 字符串的坑

    字符串一旦生成就不能改变。 字符串本身在堆中,而字符串常量池中有指向他的引用,字符串常量池也在堆中! 当字面量方式...

  • 数据结构有哪些

    数组、链表、哈希、队列、堆、栈、图、树、字符串

  • 三、字符串和矩阵

    三、字符串和矩阵 1. 字符串 1.1 字符串的按需(堆)存储结构 实现: HString 类中存储字符串的方式和...

  • 数据结构-串(用T返回由堆分配顺序串的两个字符串组成的新串)

    //用T返回由堆分配顺序串的两个字符串组成的新串 void StrConcat(HString &T,HStrin...

  • 同是枇杷味不同

    枇杷熟了,黄澄澄的,一堆堆的,一串串的,满枝头都是。今年的枇杷应该是个丰收年吧。 看着路边、小区里...

网友评论

      本文标题:堆串

      本文链接:https://www.haomeiwen.com/subject/ccjgyxtx.html