串 堆串的基本操作

2021-09-14 03:34:16 字數 1280 閱讀 3011

順序串為靜態順序儲存表示,在串的插入,連線過程中如果長度超過了maxsize就會截掉一部分。

因此可以使用動態分配儲存表示串

#pragma once

#include#include#includetypedef struct

heapstring;

void initsring(heapstring *s)

/*初始化*/

void strassign(heapstring *s, char cstr)

/*賦值*/

else }

}void strcopy(heapstring *t, heapstring s)

/*複製串*/

t->length = s.length; }

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

/*在第pos個的位置拆入乙個字串,成功返回1*/

else

for (i = pos - 1; i < t->length; i++)//複製後半段

for (i = pos - 1; i < pos + s.length; i++) // 賦值

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

t->length += s.length;

return 1; }}

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

/*刪除pos開始的len個字元,pos為位置*/

p = (char*)malloc((s->length - len) * sizeof(char));

if (!p)

exit(-1);

for (i = 0; i < pos - 1; i++) //將pos前面的賦值到p中

for (i = pos - 1 + len; i < s->length; i++)//將pos後面的賦值到p中

s->length -= len;

free(s->str);//釋放空間

s->str = p;

return 1;

}int strcat(heapstring *t, heapstring s)

/*將s連線在t的後面,成功返回1*/

t->length += s.length;

return 1;

}void strdestroy(heapstring *s)

//銷毀串

堆串的基本運算

include include include include heapstring.h void main heapstring.h include include includetypedef struct heapstring void initstring heapstring s 串的初始...

字串 堆分配基本操作

堆分配的儲存特點 1 依然是以一組位址連續的儲存單元存放串值字串行 2 儲存單元由動態分配 儲存結構typedef struct hstring 初始化 在這裡,使用for i 0,c chars c c i 這是乙個單獨的語句,沒有 塊。相當於for i 0,c chars c c i 但是在最開...

串的基本操作

include include include include define maxn 50 define ok 1 define error 0 typedef struct strnode snode void creat snode char 建立串 int getsubstr snode i...