堆串的應用

2021-07-04 02:37:26 字數 3697 閱讀 1856

例如串s1=「welcome to」,s2="china",sub="xi'an",將串s2連線到串s1末尾,然後將串s1中的s2用sub替換。

#include #include #include typedef struct  

heapstring;

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

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

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

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

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

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 *t,heapstring s);//串的連線操作

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);//串的輸出宣告

#include "heapstring.h"

void initstring(heapstring *s)

void strassign(heapstring *s,char cstr)//串的賦值操作(將常量cstr中的字元賦值給串s)

for(i = 0;cstr[i]!='\0';i++)

len = i;

if(!i)

else

for(i = 0;i < len;i++)

s->length = len;

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

else

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

void strcopy(heapstring *t,heapstring s)//串的複製操作(將串s中的每乙個字元賦給t)

for(i = 0;i < s.length ;i++)

t->length = s.length ;

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

} return (s.length - t.length);//如果比較完畢,返回兩個字串的長度的差值

} int strinsert(heapstring *s,int pos,heapstring t)//串的插入操作(在串s的pos個位置插入串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--)

for(i = 0;i < t.length ;i++)

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

return 1;

} int strdelete(heapstring *s,int pos,int len)//串的刪除操作(在串s中刪除pos開始的len個字元,然後將後面的字元向前移動)

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

if(!p)

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

for(i = pos-1;i < s->length-len;i++)//將串第pos+len位置以後的字元複製到p中

s->length = s->length -len;//修改串的長度

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

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

return 1;

} int strconcat(heapstring *t,heapstring s)//串的連線操作(將串s連線在串t的後面)

else

t->length = t->length +s.length ;//修改串t的長度

} return 1;

} int substring(heapstring *sub,heapstring s,int pos,int len)//擷取子串操作(擷取串s中從第pos個字元開始,長度為len的連續字元,並賦值給sub)

if(pos < 0 || len < 0 || pos+len-1 > s.length)

else

for(i = 0;i < len;i++)

sub->length = len;

return 1;

} } int strindex(heapstring s,int pos,heapstring t)//串的定位操作(在主串s中的第pos個位置開始查詢子串t,如果主串s中存在與串t值相等的子串,返回子串在主串第pos個字元後第一次出現的位置)

i = pos;

j = 0;

while(i < s.length && j < t.length)

else//如果當前對應位置的字元不相等,則從串s的下乙個字元開始,從t的第0個字元開始比較

} if(j >= t.length)//如果在串s中找到串t,則返回子串t在主串s中的位置

else

} int strreplace(heapstring *s,heapstring t,heapstring v)//串的替換操作(如果串s中存在子串t,則用v替換串s中的所有子串t)

do

i += strlength(v);

} }while(i);

return 1;

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

s->str = '\0';

s->length = 0;

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

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

printf("\n");

} #include "heapstring.h"

int main(void)

堆以及堆的相關應用

本文中的堆是一種樹形資料結構,可以把堆看成一種特殊的完全二叉樹,再從二叉樹上加上一些限制條件即可以構成堆。即要求父節點元素全部都大於或等於子節點元素,或者小於等於。這就構成了倆種堆 堆的常見應用為 使用堆進行排序,也就是常說的堆排序,時間複雜度為nlgn 這是比較排序時間複雜度的下限,即使用比較的排...

堆以及堆的實際應用

1.堆的概念 堆的儲存可以看成是陣列儲存的變形,不過,堆的儲存又具有二叉樹的結構,堆的儲存按型別可以分為大堆和小堆。小堆 大堆 中 任一節點的關鍵碼均小於 大於 等於它的左右孩子的關鍵碼,位於堆頂節點的關鍵碼最小 最大 從根節點到每個結點的路徑上陣列元素組成的序列都是遞增 遞減 的。2.堆的建立 堆...

堆的簡單應用

一 大資料的處理 給出n個資料,要求找到並輸出這n個數裡面最大的k個數 思路 利用堆,先建乙個開闢乙個大小為k的陣列,從n個資料裡拿出k個資料放到堆裡面,然後再通過向 下調整法把堆調整為最小堆,此時陣列的第乙個元素就是堆裡面最小的元素,然後在剩下的n k個 資料中依次和堆裡面最小的資料進行比較,若比...