1. 程式人生 > >串的數據結構的C語言實現

串的數據結構的C語言實現

har substring 退回 元素 image type類 string.h src 代碼

技術分享

#include "string.h"
#include "stdio.h"
#include "stdlib.h"
#include "io.h"
#include "math.h"
#include "time.h"

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0

#define MAXSIZE 40 /* 存儲空間初始分配量 */

typedef int Status; /* Status是函數的類型,其值是函數結果狀態代碼,如OK等 */
typedef int ElemType; /* ElemType類型根據實際情況而定,這裏假設為int */

typedef char String[MAXSIZE+1]; /* 0號單元存放串的長度 */

/* 生成一個其值等於chars的串T */
Status StrAssign(String T,char *chars)
{
int i;
if(strlen(chars)>MAXSIZE)
return ERROR;
else
{
T[0]=strlen(chars);
for(i=1;i<=T[0];i++)
T[i]=*(chars+i-1);
return OK;
}
}

/* 由串S復制得串T */
Status StrCopy(String T,String S)
{
int i;
for(i=0;i<=S[0];i++)
T[i]=S[i];
return OK;
}

/* 若S為空串,則返回TRUE,否則返回FALSE */
Status StrEmpty(String S)
{
if(S[0]==0)
return TRUE;
else
return FALSE;
}

/* 初始條件: 串S和T存在 */
/* 操作結果: 若S>T,則返回值>0;若S=T,則返回值=0;若S<T,則返回值<0 */
int StrCompare(String S,String T)
{
int i;
for(i=1;i<=S[0]&&i<=T[0];++i)
if(S[i]!=T[i])
return S[i]-T[i];
return S[0]-T[0];
}

/* 返回串的元素個數 */
int StrLength(String S)
{
return S[0];
}

/* 初始條件:串S存在。操作結果:將S清為空串 */
Status ClearString(String S)
{
S[0]=0;/* 令串長為零 */
return OK;
}

/* 用T返回S1和S2聯接而成的新串。若未截斷,則返回TRUE,否則FALSE */
Status Concat(String T,String S1,String S2)
{
int i;
if(S1[0]+S2[0]<=MAXSIZE)
{ /* 未截斷 */
for(i=1;i<=S1[0];i++)
T[i]=S1[i];
for(i=1;i<=S2[0];i++)
T[S1[0]+i]=S2[i];
T[0]=S1[0]+S2[0];
return TRUE;
}
else
{ /* 截斷S2 */
for(i=1;i<=S1[0];i++)
T[i]=S1[i];
for(i=1;i<=MAXSIZE-S1[0];i++)
T[S1[0]+i]=S2[i];
T[0]=MAXSIZE;
return FALSE;
}
}

/* 用Sub返回串S的第pos個字符起長度為len的子串。 */
Status SubString(String Sub,String S,int pos,int len)
{
int i;
if(pos<1||pos>S[0]||len<0||len>S[0]-pos+1)
return ERROR;
for(i=1;i<=len;i++)
Sub[i]=S[pos+i-1];
Sub[0]=len;
return OK;
}

/* 返回子串T在主串S中第pos個字符之後的位置。若不存在,則函數返回值為0。 */
/* 其中,T非空,1≤pos≤StrLength(S)。 */
int Index(String S, String T, int pos)
{
int i = pos; /* i用於主串S中當前位置下標值,若pos不為1,則從pos位置開始匹配 */
int j = 1; /* j用於子串T中當前位置下標值 */
while (i <= S[0] && j <= T[0]) /* 若i小於S的長度並且j小於T的長度時,循環繼續 */
{
if (S[i] == T[j]) /* 兩字母相等則繼續 */
{
++i;
++j;
}
else /* 指針後退重新開始匹配 */
{
i = i-j+2; /* i退回到上次匹配首位的下一位 */
j = 1; /* j退回到子串T的首位 */
}
}
if (j > T[0])
return i-T[0];
else
return 0;
}


/* T為非空串。若主串S中第pos個字符之後存在與T相等的子串, */
/* 則返回第一個這樣的子串在S中的位置,否則返回0 */
int Index2(String S, String T, int pos)
{
int n,m,i;
String sub;
if (pos > 0)
{
n = StrLength(S); /* 得到主串S的長度 */
m = StrLength(T); /* 得到子串T的長度 */
i = pos;
while (i <= n-m+1)
{
SubString (sub, S, i, m); /* 取主串中第i個位置長度與T相等的子串給sub */
if (StrCompare(sub,T) != 0) /* 如果兩串不相等 */
++i;
else /* 如果兩串相等 */
return i; /* 則返回i值 */
}
}
return 0; /* 若無子串與T相等,返回0 */
}


/* 初始條件: 串S和T存在,1≤pos≤StrLength(S)+1 */
/* 操作結果: 在串S的第pos個字符之前插入串T。完全插入返回TRUE,部分插入返回FALSE */
Status StrInsert(String S,int pos,String T)
{
int i;
if(pos<1||pos>S[0]+1)
return ERROR;
if(S[0]+T[0]<=MAXSIZE)
{ /* 完全插入 */
for(i=S[0];i>=pos;i--)
S[i+T[0]]=S[i];
for(i=pos;i<pos+T[0];i++)
S[i]=T[i-pos+1];
S[0]=S[0]+T[0];
return TRUE;
}
else
{ /* 部分插入 */
for(i=MAXSIZE;i<=pos;i--)
S[i]=S[i-T[0]];
for(i=pos;i<pos+T[0];i++)
S[i]=T[i-pos+1];
S[0]=MAXSIZE;
return FALSE;
}
}

/* 初始條件: 串S存在,1≤pos≤StrLength(S)-len+1 */
/* 操作結果: 從串S中刪除第pos個字符起長度為len的子串 */
Status StrDelete(String S,int pos,int len)
{
int i;
if(pos<1||pos>S[0]-len+1||len<0)
return ERROR;
for(i=pos+len;i<=S[0];i++)
S[i-len]=S[i];
S[0]-=len;
return OK;
}

/* 初始條件: 串S,T和V存在,T是非空串(此函數與串的存儲結構無關) */
/* 操作結果: 用V替換主串S中出現的所有與T相等的不重疊的子串 */
Status Replace(String S,String T,String V)
{
int i=1; /* 從串S的第一個字符起查找串T */
if(StrEmpty(T)) /* T是空串 */
return ERROR;
do
{
i=Index(S,T,i); /* 結果i為從上一個i之後找到的子串T的位置 */
if(i) /* 串S中存在串T */
{
StrDelete(S,i,StrLength(T)); /* 刪除該串T */
StrInsert(S,i,V); /* 在原串T的位置插入串V */
i+=StrLength(V); /* 在插入的串V後面繼續查找串T */
}
}while(i);
return OK;
}

/* 輸出字符串T */
void StrPrint(String T)
{
int i;
for(i=1;i<=T[0];i++)
printf("%c",T[i]);
printf("\n");
}


int main()
{

int i,j;
Status k;
char s;
String t,s1,s2;
printf("請輸入串s1: ");

k=StrAssign(s1,"abcd");
if(!k)
{
printf("串長超過MAXSIZE(=%d)\n",MAXSIZE);
exit(0);
}
printf("串長為%d 串空否?%d(1:是 0:否)\n",StrLength(s1),StrEmpty(s1));
StrCopy(s2,s1);
printf("拷貝s1生成的串為: ");
StrPrint(s2);
printf("請輸入串s2: ");

k=StrAssign(s2,"efghijk");
if(!k)
{
printf("串長超過MAXSIZE(%d)\n",MAXSIZE);
exit(0);
}
i=StrCompare(s1,s2);
if(i<0)
s=‘<‘;
else if(i==0)
s=‘=‘;
else
s=‘>‘;
printf("串s1%c串s2\n",s);
k=Concat(t,s1,s2);
printf("串s1聯接串s2得到的串t為: ");
StrPrint(t);
if(k==FALSE)
printf("串t有截斷\n");
ClearString(s1);
printf("清為空串後,串s1為: ");
StrPrint(s1);
printf("串長為%d 串空否?%d(1:是 0:否)\n",StrLength(s1),StrEmpty(s1));
printf("求串t的子串,請輸入子串的起始位置,子串長度: ");

i=2;
j=3;
printf("%d,%d \n",i,j);

k=SubString(s2,t,i,j);
if(k)
{
printf("子串s2為: ");
StrPrint(s2);
}
printf("從串t的第pos個字符起,刪除len個字符,請輸入pos,len: ");

i=4;
j=2;
printf("%d,%d \n",i,j);


StrDelete(t,i,j);
printf("刪除後的串t為: ");
StrPrint(t);
i=StrLength(s2)/2;
StrInsert(s2,i,t);
printf("在串s2的第%d個字符之前插入串t後,串s2為:\n",i);
StrPrint(s2);
i=Index(s2,t,1);
printf("s2的第%d個字母起和t第一次匹配\n",i);
SubString(t,s2,1,1);
printf("串t為:");
StrPrint(t);
Concat(s1,t,t);
printf("串s1為:");
StrPrint(s1);
Replace(s2,t,s1);
printf("用串s1取代串s2中和串t相同的不重疊的串後,串s2為: ");
StrPrint(s2);


return 0;
}

串的數據結構的C語言實現