//堆串
#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]);
}
堆串
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
- 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事?!?“怎么了?”我有些...
- 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
- 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
推薦閱讀更多精彩內容
- 堆的數據結構能夠使得堆頂總是維持最大(對于大根堆)或最?。▽τ谛「眩?,給定一個數組,對這個數組進行建堆,則平均復...
- 轉自: Imcache :一個Java新的緩存框架堆Heap是內存中動態分配對象居住的地方。如果使用new一個對象...
- 【作品】 【演示文稿】 中心圖像 心智圖 主支 1.中心圖 2.主支 3.關鍵詞 4.色彩 5.符號 6.大腦 細...