堆串
2017-10-11 本文已影响0人
star_night
//堆串
#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]);
}