关闭

串的表示和实现

416人阅读 评论(0) 收藏 举报

 

<iframe style="WIDTH: 89.81%; HEIGHT: 64px" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-4577827332549849&amp;dt=1192819750500&amp;lmt=1192819750&amp;prev_fmts=468x60_as&amp;format=468x15_0ads_al_s&amp;output=html&amp;correlator=1192819750437&amp;channel=1741427766&amp;pv_ch=1741427766%2B&amp;url=http%3A%2F%2Fyzkzoo.5d6d.com%2Fthread-68-1-1.html&amp;color_bg=FFFFFF&amp;color_text=000000&amp;color_link=0000FF&amp;color_url=008000&amp;color_border=336699&amp;ref=http%3A%2F%2Fyzkzoo.5d6d.com%2Fforum-16-1.html&amp;cc=100&amp;ga_vid=1025434795.1192631677&amp;ga_sid=1192817968&amp;ga_hid=1871659779&amp;ga_fc=true&amp;flash=8&amp;u_h=768&amp;u_w=1024&amp;u_ah=738&amp;u_aw=1024&amp;u_cd=32&amp;u_tz=480&amp;u_his=2&amp;u_java=true"></iframe>

<iframe style="WIDTH: 44.33%; HEIGHT: 259px" src="http://pagead2.googlesyndication.com/cpa/ads?client=ca-pub-4577827332549849&amp;cpa_choice=CAEaCB94-nvUZWENUB9QugJQtwRQTVAgULcCUB4&amp;oe=gb2312&amp;dt=1192720966468&amp;lmt=1192720966&amp;format=250x250_as&amp;output=html&amp;correlator=1192720966453&amp;channel=2735220158&amp;url=http%3A%2F%2Fyzkzoo.5d6d.com%2Fthread-67-1-1.html&amp;color_bg=FFFFFF&amp;color_text=000000&amp;color_link=0000FF&amp;color_url=008000&amp;color_border=336699&amp;ad_type=text_image&amp;region=_google_cpa_region_&amp;ref=http%3A%2F%2Fyzkzoo.5d6d.com%2Fforum-16-1.html&amp;cc=100&amp;ga_vid=1025434795.1192631677&amp;ga_sid=1192720940&amp;ga_hid=1155529757&amp;ga_fc=true&amp;flash=8&amp;u_h=768&amp;u_w=1024&amp;u_ah=738&amp;u_aw=1024&amp;u_cd=32&amp;u_tz=480&amp;u_his=2&amp;u_java=true"></iframe>

一、复习串的定义

串的定义

二、定长顺序存储表示

类似于线性表的顺序存储结构,用一组地址连续的存储单元存储串值的字符序列.

#define MAXSTRLEN 255

typedef unsigned char SString[MAXSTRLEN+1] //0号单元存放串长

串的实际长度可在这予定义长度的范围内随意,超过予定义长度的串值则被舍去

串长可用下标为0的数组元素存储,也可在串值后设特殊标记

a[0] a[1] a[2] a[3] a[4] a[5] ... a[n]
3 a b c    pascal
a b c /0    c

1串联接的实现Concat(&T,S1,S2)

假设S1,S2和T都是SString型的串变量,且串T是由串S1联结串S2得到的,即串T的值的前一段和串S1的值相等,串T的值的后一段和串S2的值相等,则只要进行相应的"串值复制"操作即可,对超长部分实施"截断"操作

以下是串联接可能出现的三种情况:

S1 S2 T
4 2 6
a d a
b e b
c   c
d   d
    e
    f
     
     

S1,S2串长和小于最大值
S1 S2 T
6 6 8
a g a
b h b
c i c
d j d
e k e
f l f
    g
    h

S1,S2串长和超过最大串长
S1 S2 T
8 2 8
a i a
b j b
c   c
d   d
e   e
f   f
g   g
h   h

S1串长已等于最大串长


算法描述如下:

Status Concat(SString &T,SString S1,SString S2){

if(S1[0]+S2[0]<=MAXSTRLEN){

T[1..S1[0]]=S1[1..S1[0]];

T[S1[0]+1..S1[0]+S2[0]]=S2[1..S2[0]];

T[0]=S1[0]+S2[0]uncut=TRUE;

}

else if(S1[0]<MAXSTRSIZE){

T[1..S1[0]]=S1[1..S1[0]];

T[S1[0]+1..MAXSTRLEN]=S2[1..MAXSTRLEN-S1[0]];

T[0]=MAXSTRLEN;uncut=FALSE;

}

else{

T[0..MAXSTRLEN]=S1[0..MAXSTRLEN];

uncut=FALSE;

}

return uncut;

}

三、堆分配存储表示

这种存储表示的特点是,仍以一组地址连续的存储单元存放串值字符序列,但它们的存储空间是在程序执行过程中动态分配而得

在C语言中,存在一个称之为堆的自由存储区,并由C语言的动态分配函数malloc()和free()来管理.利用函数malloc()为每个新产生的串分配一块实际串长所需存储空间,为处理方便,约定串长也作为存储结构的一部分

typedef struct{

char *ch;//若是非空串,则按串长分配存储区,否则ch为NULL

int length; //串长度

}HString

Status StrInsert(HString &S,int pos,HString T){

if(pox<1||pos>S.length+1) return ERROR;

if(T.length){

if(!(S.ch=(char *)realloc(S.ch,(S.length+T.length)*sizeof(char))))

exit(OVERFLOW);

for(i=S.length-1;i>=pos-1;--i)

S.ch[i+T.length]=S.ch;

S.ch[pos-1..pos+T.lenght-2]=T.ch[0..T.length-1];

S.length+=T.length;

}

return OK;

}

四、总结

思考两种存储表示方法的优缺点

<iframe style="WIDTH: 52.39%; HEIGHT: 263px" src="http://pagead2.googlesyndication.com/cpa/ads?client=ca-pub-4577827332549849&amp;cpa_choice=CAEaCKcC4yuPlq5lUDRQDVAtUK4BUENQCA&amp;oe=gb2312&amp;dt=1192819388296&amp;lmt=1192819388&amp;format=300x250_as&amp;output=html&amp;correlator=1192819388281&amp;channel=2735220158&amp;url=http%3A%2F%2Fyzkzoo.5d6d.com%2Fviewthread.php%3Ftid%3D70%26page%3D1%26extra%3Dpage%253D1&amp;color_bg=FFFFFF&amp;color_text=000000&amp;color_link=0000FF&amp;color_url=008000&amp;color_border=336699&amp;ad_type=text_image&amp;region=_google_cpa_region_&amp;ref=http%3A%2F%2Fyzkzoo.5d6d.com%2Fpost.php%3Faction%3Dedit%26fid%3D16%26tid%3D70%26pid%3D70%26page%3D1%26extra%3Dpage%253D1&amp;cc=100&amp;ga_vid=1025434795.1192631677&amp;ga_sid=1192817968&amp;ga_hid=1527165455&amp;ga_fc=true&amp;flash=8&amp;u_h=768&amp;u_w=1024&amp;u_ah=738&amp;u_aw=1024&amp;u_cd=32&amp;u_tz=480&amp;u_his=5&amp;u_java=true"></iframe>

<iframe style="WIDTH: 82.23%; HEIGHT: 74px" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-4577827332549849&amp;dt=1192818497343&amp;lmt=1192818497&amp;format=468x60_as&amp;output=html&amp;correlator=1192818497343&amp;channel=1741427766&amp;url=http%3A%2F%2Fyzkzoo.5d6d.com%2Fthread-68-1-1.html&amp;color_bg=FFFFFF&amp;color_text=000000&amp;color_link=0000FF&amp;color_url=008000&amp;color_border=336699&amp;ad_type=text_image&amp;ref=http%3A%2F%2Fyzkzoo.5d6d.com%2Fforumdisplay.php%3Ffid%3D16%26page%3D1&amp;ui=rc%3A6&amp;cc=100&amp;ga_vid=1025434795.1192631677&amp;ga_sid=1192817968&amp;ga_hid=1000484876&amp;ga_fc=true&amp;flash=8&amp;u_h=768&amp;u_w=1024&amp;u_ah=738&amp;u_aw=1024&amp;u_cd=32&amp;u_tz=480&amp;u_his=6&amp;u_java=true"></iframe>

<iframe style="WIDTH: 58.41%; HEIGHT: 156px" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-4577827332549849&amp;dt=1192819750656&amp;lmt=1192819750&amp;prev_fmts=468x60_as%2C468x15_0ads_al_s%2C234x60_as&amp;format=200x90_0ads_al_s&amp;output=html&amp;correlator=1192819750437&amp;channel=1741427766&amp;pv_ch=1741427766%2B&amp;url=http%3A%2F%2Fyzkzoo.5d6d.com%2Fthread-68-1-1.html&amp;color_bg=FFFFFF&amp;color_text=000000&amp;color_link=0000FF&amp;color_url=008000&amp;color_border=336699&amp;ref=http%3A%2F%2Fyzkzoo.5d6d.com%2Fforum-16-1.html&amp;cc=100&amp;ga_vid=1025434795.1192631677&amp;ga_sid=1192817968&amp;ga_hid=1871659779&amp;ga_fc=true&amp;flash=8&amp;u_h=768&amp;u_w=1024&amp;u_ah=738&amp;u_aw=1024&amp;u_cd=32&amp;u_tz=480&amp;u_his=2&amp;u_java=true"></iframe> 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场