串 string 的順序方式實現 線性結構

2021-10-25 15:26:40 字數 3950 閱讀 2240

昨天把串(string)資料結構的基本知識和堆的實現方式為大家分享了,今天再來為大家分享一下串(string)的順序實現方式

如果小夥伴們對串的概念存在疑惑,請看上篇文章的介紹。

串(string)的順序實現**如下:

字串的定義

#include

#include

#define len sizeof(node)

#define maxsize 100

#define ok 1

#define error 0

#define true 1

#define false 0

typedef

struct node

*string;

所有方法的宣告
int

strassign

(string s,

char

*chars)

;//初始化串操作

intstrinsert

(string s,

int pos, string p)

;//串的插入操作

intstrdelete

(string s,

int pos,

int len)

;// 串的刪除操作

intstrcompare

(string s, string p)

;//串的比較操作

intstrcopy

(string s, string p)

;//串的複製操作

intstremply

(string s)

;//判斷空串的操作

intstrlength

(string s)

;//求串的長度操作

intstrclear

(string s)

;//清空串的內容

intstrcat

(string s, string p)

;//串的連線操作

intstrindex

(string s,

int pos, string p)

;//索引字串在主串的位置操作

intsubstring

(string sub, string s,

int pos,

int len)

;//獲取字串的操作

intstrreplace

(string s, string t, string v)

;//字串的替換操作

intstrdestroy

(string s)

;//銷毀串

void

printstr

(string s)

;//列印串

初始化字串操作
//初始化串操作,生成乙個值等於 chars 的串 s 

intstrassign

(string s,

char

*chars)

return ok;

}

插入子串
//順序串的插入函式, 將字串 t 插入下標為 pos 前的 s 串中 

intstrinsert

(string s,

int pos, string p)

//pos 後的串後移, 為插入的串空出位置

for(

int i =

0; i < p->len; i++

)//新串進行插入

s->len = s->len + p->len;

//長度變化

}else

if(pos + p->len <= maxsize)

for(

int i =

0; i < p->len; i++

) s->len = maxsize;

//串的長度改變為最大長度

}else

//此條件說明從 pos 位置插入的新串超出串的總長度

s->len = maxsize;

//串的長度變為最大長度

}return ok;

}

刪除子串
//在串 s 中刪除從下標 pos 起的 len 個字元 

intstrdelete

(string s,

int pos,

int len)

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

//串的長度改變

return ok;

}

索引子串
//串的簡單模式匹配 brute-force(布魯特-福斯)演算法 

//最壞時間複雜度較高, 為 o(s->len * p->len)

intstrindex

(string s,

int pos, string p)

else

//否則從主串的下乙個位置繼續開始匹配 }if

(j >= p->len)

return start;

else

return-1

;}

擷取子串
//用 sub 返回串 s 的第 pos 個字元起長度為 len 的字串 

intsubstring

(string sub, string s,

int pos,

int len)

sub->len = len;

for(

int i = pos; i < pos + len; i++

)//複製串

return ok;

}

替換子串
//串 s、t 和 v 存在且 t 是非空串 ,用 串 v 代替 s 的字串 t 

intstrreplace

(string s, string t, string v)

else

//進行 s 串的查詢,找到 t 執行以下

if(j >= t->len)

}return ok;

}

字串比較
//字串比較函式, s > p 返回 >0 , s = p, 返回 =0, s < p, 返回 <0 

intstrcompare

(string s, string p)

return s->len - p->len;

}

字串拷貝
//字串拷貝, 將 p 字串的內容拷貝至 s 串 

intstrcopy

(string s, string p)

字串的連線
//串的連線, 將串 p 連線至 串 s 

intstrcat

(string s, string p)

s->len = maxsize;

}else

s->len = s->len + p->len;

}return ok;

}

獲取串的長度,判空、列印、清空、銷毀串
//獲取串的長度 

intstrlength

(string s)

//判斷串是否為空

intstremply

(string s)

void

printstr

(string s)

//輸出串 s

//將串 s 置為空串

intstrclear

(string s)

//銷毀串 s;

intstrdestroy

(string s)

順序串的實現

package com.lovely.string author echo lovely 2020年6月9日下午6 44 31 串的介面描述 public inte ce istring package com.lovely.string author echo lovely 2020年6月9日下午...

實現string類的幾種方式

淺拷貝 淺拷貝的意思就是在拷貝建構函式 賦值運算子函式時並沒有給新物件開闢一塊空間,而是直接二者公用一塊空間。這個問題就很麻煩了。s1,s2共用一塊空間,此時不論s1怎樣修改,都會影響到s2,而且析構s1後,s2就無法繼續使用。簡單 實現如下 class string string string s...

順序串(陣列實現)

include using namespace std define max 100 typedef struct sstring void in sstring sa 初始化 cout ddw sstring totwo sstring sb,sstring sc 合併兩個串,返回合成後的串 el...