重學資料結構 串的基本操作(C語言)

2021-10-13 23:15:49 字數 2242 閱讀 3220

#include

"stdafx.h"

#include

"stdlib.h"

#include

"string.h"

#define maxsize 50

#define bool int

#define true 1

#define false 0;

typedef

struct

string;

void

initstring

(string *s)

strassign(&t,chars):賦值操作。把串t賦值為chars。

bool strassign

(string *s,

char

*t)

strcopy(&t,s):複製操作。由串s複製得到串t。

bool strcopy

(string *t,string s)

strempty(s):判空操作。若s為空串,則返回true,否則返回false。

bool strempty

(string s)

void

printstring

(string s)

printf

("%s\n"

,t);

//printf("s->data=%s\n",s->data);

}

strlength(s):求串長。返回串s的元素個數。

int

strlength

(string s)

clearstring(&s):清空操作。將s清為空串。

void

clearstring

(string *s)

concat(&t,s1,s2):串聯接。用t返回由s1和s2聯接而成的新串

bool concat

(string *s,

char

*s1,

char

*s2)

substring(&sub,s,pos,len):求子串。用sub返回串s的第pos個字元起長度為len的子串。

void

substring

(string *sub,string s,

int pos,

int len)

index(s,t):定位操作。若主串s中存在與串t值相同的子串,則返回它在主串s中第一次出現的

位置;否則函式值為0。

//定位操作。若主串s中存在與串t值相同的子串,則返回它在主串s中第一次出現的位置;否則函式值為-1。

intstrindex

(string s,string t)

else

}return result;

}

strcompare(s,t):比較操作。若s>t,則返回值》0;若s=t,則返回值=0;若s//比較操作。若s>t,則返回值》0;若s=t,則返回值=0;若sint

strcompare

(string s,string t)

return s.length-t.length;

}

int

main

(int argc,

char

* ar**)

輸出賦值操作字串:hello world!

串s是否為空:0

串t是否為空:1

串s的長度:12

串t的長度:0

輸出複製賦值操作字串:stay young!

清空操作後串s是否為空:0

輸出串連線操作字串:keepmoving!

擷取字串:stay young!

substring(

&substr,s,5,5)

=young

strcompare(x,y)

=3p:wangdao,q:dao

strindex(p,q)

=4

資料結構 串的基本操作c 實現

include include using namespace std define maxsize 20 void initstr struct stringsq p 初始化 intstrlength struct stringsq p 求字串的長度 void createstr struct s...

資料結構(C語言) 鏈佇列基本操作

文章首發於2020 10 15 知乎文章 資料結構 c語言 鏈佇列基本操作 佇列是一種先進先出 first in first out,fifo 的線性表,是一種常用的資料結構。它只允許在表的前端 front 進行刪除操作,而在表的後端 rear 進行插入操作,和棧一樣,佇列是一種操作受限制的線性表。...

資料結構 C語言 迴圈佇列基本操作

文章首發於 2020 10 15 知乎文章 資料結構 c語言 迴圈佇列基本操作 佇列是一種先進先出 first in first out,fifo 的線性表,是一種常用的資料結構。它只允許在表的前端 front 進行刪除操作,而在表的後端 rear 進行插入操作,和棧一樣,佇列是一種操作受限制的線性...