堆串的基本運算

2021-07-24 18:01:13 字數 3283 閱讀 6108

#include#include#include#include "heapstring.h"

void main()

/*heapstring.h*/

#include#include#includetypedef struct

heapstring;

void initstring(heapstring *s);//串的初始化操作

void strassign(heapstring *s, char cstr);//串的賦值操作

int strempty(heapstring *s);//判斷串是否為空

int strlength(heapstring *s);//求串的長度操作

void strcopy(heapstring *s, heapstring *t);//串的複製操作

int strcompare(heapstring *s, heapstring *t);//串的比較操作

int strinsert(heapstring *s, int pos, heapstring *t);//串的插入操作

int strdelete(heapstring *s, int pos, int len);//串的刪除操作

int strconcat(heapstring *s, heapstring *t);//串的連線操作

int substring(heapstring *sub, heapstring *s, int poos, int len);//擷取子串操作

int strreplace(heapstring *s, heapstring *t, heapstring *v);//串的替換操作

int strindex(heapstring *s, int pos, heapstring *t);//串的定位操作

void strclear(heapstring *s);//清空串操作

void strdestory(heapstring *s);//摧毀串操作

void strprint(heapstring *s);//串的輸出宣告

/*串的初始化*/

void initstring(heapstring *s)

/*串的賦值操作*/

void strassign(heapstring *s, char cstr)

else }

/*判斷串是否為空*/

int strempty(heapstring *s)

/*求串的長度*/

int strlength(heapstring *s)

/*串的複製操作*/

void strcopy(heapstring *s, heapstring *t)

/*串的比較操作*/

int strcompare(heapstring *s, heapstring *t)

/*串的插入操作*/

int strinsert(heapstring *s, int pos, heapstring *t)

s->str = (char*)realloc(s->str,(s->length+t->length)*sizeof(char));

if (!s->str)

for (i = s->length - 1; i >= pos -1; i--)

/*將串s中第pos個位置的字元往後移動t->length個位置*/

s->str[i + t->length] = s->str[i];

for (i = 0; i < t->length; i++)

s->str[pos + i - 1] = t->str[i];

s->length = s->length + t->length;

return 1;

}/*串的刪除操作*/

int strdelete(heapstring *s, int pos, int len)

p = (char *)malloc(s->length - len);/*p指向動態分配的記憶體單元*/

if (!p)

exit(-1);

for (i = 0; i < pos - 1; i++)/*將串第pos位置之前的字元複製到p中*/

p[i] = s->str[i];

for (i = pos - 1; i <= s->length - len; i++)

/*將串第pos+len位置以後的字元複製到p*/

p[i] = s->str[i + len];

s->length = s->length - len;

free(s->str); /*釋放原來的串s的記憶體空間*/

s->str = p; /*將串的str指向p字元*/

return 1;

}/*串的連線操作*/

int strconcat(heapstring *s, heapstring *t)

else

return 1;

}/*擷取子串操作*/

int substring(heapstring *sub, heapstring *s, int pos, int len)

else

for (i = 0; i < len; i++) /*將串s的第pos個位置長度為len的字元賦值給sub*/

sub->str[i] = s->str[i + pos - 1];

sub->length = len;

return 1; }}

/*串的替換操作*/

int strreplace(heapstring *s, heapstring *t, heapstring *v)

} while (i);

return 1;

}/*串的定位操作*/

int strindex(heapstring *s, int pos, heapstring *t)

else

}if (j >= t->length)

return (i - j + 1);

else

return -1;

}/*清空串的操作*/

void strclear(heapstring *s)

/*串的銷毀*/

void strdestory(heapstring *s)

/*串的輸出*/

void strprint(heapstring *s)

printf("\n");

}

串 堆串的基本操作

順序串為靜態順序儲存表示,在串的插入,連線過程中如果長度超過了maxsize就會截掉一部分。因此可以使用動態分配儲存表示串 pragma once include include includetypedef struct heapstring void initsring heapstring s...

鏈串的基本運算

include include include define chunksize 10 define stuff typedef struct chunk chunk 串的結點型別定義 typedef struct linkstring void initstring linkstring s 初始...

順序串基本運算

順序串基本運算 include define maxsize 50 int strlength char s 求串長 int strcat char s1,char s2 串連線 int substr char s,char t,int i,int len 求子串 int strcmp char s...