This presentation is the property of its rightful owner.
Sponsored Links
1 / 47

数据结构 PowerPoint PPT Presentation


  • 44 Views
  • Uploaded on
  • Presentation posted in: General

数据结构. 主讲人:张云雷. 信息工程学院. 授课内容 串的基本概念 串的存储结构 教学目标 理解掌握串的定义与特性 理解掌握串的存储结构 教学重点 串的存储结构 教学难点 串的存储结构. 4.1 串的基本概念. 串(或字符串),是由 零个 或 多个字符 组成的 有穷序列 。含零个字符的串称为空串,用Ф表示。 串中所含字符的个数称为该串的长度(或串长)。 通常将一个串表示成" a 1 a 2 … a n "的形式。其中,最外边的双引号本身不是串的内容,它们是串的标志,以便将串与标识符(如变量名等)加以区别。每个a i (1≤i≤n)代表一个字符。.

Download Presentation

数据结构

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


6532716


6532716


6532716

4.1

(),,

()

"a1a2an",,,()ai(1in)


6532716

,

(,),aababcabcdabcde


6532716

4.1 : abcde?

::1

1:5

2:4

3:3

4:2

1+2+3+4+5=15


6532716

:

(1) StrAssign(&s,chars):s,charss

(2) StrCopy(&s,t)::ts

(3) StrEqual(s,t)::st

(4) StrLength(s)::s


6532716

(5) Concat(s,t)::st

(6)SubStr(s,i,j)::si(1iStrLength(s))j

(7)InsStr(s1,i,s2):s2s1i(1iStrLength(s)+1),s2s1i,


6532716

(8)DelStr(s,i,j):si(1iStrLength(s))j,

(9)RepStr(s,i,j,t)::s,i(1iStrLength(s))jt,

(10) DispStr(s)::s


6532716

4.2

4.2.1

,,,,,


6532716

,:

#define MaxSize 100

typedef struct

{

char data[MaxSize];

int len;

} SqString;

,data,len,MaxSizeC'\0'


6532716

:

(1) StrAssign(str,cstr)

str,cstrs

void StrAssign(SqString &str,char cstr[])

{ int i;

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

str.data[i]=cstr[i];

str.len=i;

}


6532716

(2) StrCopy(s,t)

ts

void StrCopy(SqString &s,SqString t) /**/

{ int i;

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

s.data[i]=t.data[i];

s.len=t.len;

}


6532716

(3) StrEqual(s,t)

:st(1)(0)

int StrEqual(SqString s,SqString t)

{ int same=1,i;

if (s.len!=t.len)

same=0; /*0*/

else

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

if (s.data[i]!=t.data[i])

/*0*/

{ same=0; break; }

return same;

}


6532716

(4) StrLength(s)

:s

int StrLength(SqString s)

{

return s.len;

}


6532716

(5) Concat(s,t)

st

SqString Concat(SqString s,SqString t)

{ SqString str; int i;

str.len=s.len+t.len;

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

str.data[i]=s.data[i];

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

str.data[s.len+i]=t.data[i];

return str;

}


6532716

(6) SubStr(s,i,j)

si(1iStrLength(s))j

SqString SubStr(SqString s,int i,int j)

{SqString str;int k;str.len=0;

if (i<=0 || i>s.len || j<0 || i+j-1>s.len)

{ printf("\n");

return str; /**/

}

for(k=i-1;k<i+j-1;k++)

str.data[k-i+1]=s.data[k];

str.len=j;

return str;

}


6532716

(7) InsStr(s1,i,s2)

s2s1i,s2s1i,

SqStringInsStr(SqString s1,int i,SqString s2)

{ int j;SqString str; str.len=0;

if (i<=0 || i>s1.len+1)/**/

{ printf("\n");return s1;}


6532716

for (j=0;j<i-1;j++)

str.data[j]=s1.data[j];

for(j=0;j<s2.len;j++)

str.data[i+j-1]=s2.data[j];

for (j=i-1;j<s1.len;j++)

str.data[s2.len+j]=s1.data[j];

str.len=s1.len+s2.len;

return str;

}


6532716

(8) DelStr(s,i,j)

si(1iStrLength(s))j,

SqString DelStr(SqString s,int i,int j)

{int k;SqString str;

str.len=0;

if (i<=0 || i>s.len || i+j>s.len+1) /**/

{ printf("\n");return str;}


6532716

for (k=0;k<i-1;k++)

str.data[k]=s.data[k];

for (k=i+j-1;k<s.len;k++)

str.data[k-j]=s.data[k];

str.len=s.len-j;

return str;

}


6532716

(9) RepStr(s,i,j,t)

s,i(1iStrLength(s))jt,

SqString RepStr(SqString s,int i,int j,SqString t)

{ int k;SqString str;

str.len=0;

if (i<=0 || i>s.len || i+j-1>s.len) /**/

{ printf("\n"); return str;}


6532716

for (k=0;k<i-1;k++)

str.data[k]=s.data[k];

for (k=0;k<t.len;k++)

str.data[i+k-1]=t.data[k];

for (k=i+j-1;k<s.len;k++)

str.data[t.len+k-j]=s.data[k];

str.len=s.len-j+t.len;

return str;

}


6532716

(10) DispStr(s)

s

void DispStr(SqString s)

{ int i;

if (s.len>0)

{ for (i=0;i<s.len;i++)

printf("%c",s.data[i]);

printf("\n");

}

}


6532716

4.2 Strcmp(s,t)

::

(1)st:

st,1

st,-1

s=t,

(2)(1),s1s2:

,0

st,1

st,-1


6532716

int Strcmp(SqString s,SqString t)

{ int i,comlen;

if (s.len<t.len)

comlen=s.len;/*st*/

else comlen=t.len;

for (i=0;i<comlen;i++) /**/

if(s.data[i]<t.data[i]) return -1;

elseif (s.data[i]>t.data[i])return 1;

if (s.len==t.len) return 0;/*s==t*/

else if (s.len<t.len)/*s<t*/

return -1;

else return 1; /*s>t*/

}


6532716

4.2.2

,

:

typedef struct snode

{ char data;

struct snode *next;

} LiString;


6532716

data,next,1


6532716

(1) StrAssign(s,t)

ts,ts

void StrAssign(LiString *&s,char t[])

{ int i;

LiString *r,*p;/*r*/

s=(LiString *)malloc(sizeof(LiString));

r=s;

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

{ p=(LiString *)malloc(sizeof(LiString));

p->data=t[i];r->next=p;r=p;

}

r->next=NULL;

}


6532716

(2) StrCopy(s,t)

tss

void StrCopy(LiString *&s,LiString *t)

{ LiString *p=t->next,*q,*r;

s=(LiString *)malloc(sizeof(LiString));

r=s;/*r*/

while (p!=NULL)/*ts*/

{ q=(LiString *)malloc(sizeof(LiString));

q->data=p->data;r->next=q;r=q;

p=p->next;

}

r->next=NULL;

}


6532716

(3) StrEqual(s,t)

:st(1)(0)

int StrEqual(LiString *s,LiString *t)

{ LiString *p=s->next,*q=t->next;

while(p!=NULL&&q!=NULL&&p->data==q->data)

{ p=p->next;q=q->next; }

if (p==NULL && q==NULL) return 1;

else return 0;

}


6532716

(4) StrLength(s)

:s

int StrLength(LiString *s)

{ int i=0;

LiString *p=s->next;

while (p!=NULL)

{ i++;

p=p->next;

}

return i;

}


6532716

(5) Concat(s,t)

st

LiString *Concat(LiString *s,LiString *t)

{ LiString *str,*p=s->next,*q,*r;

str=(LiString *)malloc(sizeof(LiString));

r=str;

while(p!=NULL)/*sstr*/

{ q=(LiString *)malloc(sizeof(LiString));

q->data=p->data;

r->next=q;r=q;

p=p->next;

}


6532716

p=t->next;

while(p!=NULL)/*tstr*/

{q=(LiString *)malloc(sizeof(LiString));

q->data=p->data;

r->next=q;r=q;

p=p->next;

}

r->next=NULL;

return str;

}


6532716

(6) SubStr(s,i,j)

si(1iStrLength(s))j

LiString *SubStr(LiString *s,int i,int j)

{ int k;

LiString *str,*p=s->next,*q,*r;

str=(LiString *)malloc(sizeof(LiString));

r=str;

if(i<=0||i>StrLength(s)||j<0||i+j-1>StrLength(s))

{ printf("\n");

return str;/**/

}


6532716

for (k=0;k<i-1;k++) /*i-1,p*/

p=p->next;

for (k=1;k<=j;k++) /*s[i]j=>str*/

{ q=(LiString *)malloc(sizeof(LiString));

q->data=p->data;q->next=NULL;

r->next=q;r=q;

p=p->next;

}

r->next=NULL;

return str;

}


6532716

(7) InsStr(s1,i,s2)

s2s1i(1iStrLength(s)+1),s2s1i,

LiString *InsStr(LiString *s,int i,LiString *t)

{int k;

LiString *str,*p=s->next,*p1=t->next,*q,*r;

str=(LiString *)malloc(sizeof(LiString));

r=str;


6532716

  • if (i<=0 || i>StrLength(s)+1)

  • { printf("\n");

  • return str; /**/

  • }

    for(k=1;k<i;k++)/*sistr*/

    { q=(LiString *)malloc(sizeof(LiString));

    q->data=p->data;q->next=NULL;

    r->next=q;r=q;p=p->next;

    }

    while (p1!=NULL) /*tstr*/

    { q=(LiString *)malloc(sizeof(LiString));

    q->data=p1->data;q->next=NULL;

    r->next=q;r=q; p1=p1->next;

    }


6532716

  • while(p!=NULL)/**pstr*/

  • { q=(LiString *)malloc(sizeof(LiString));

  • q->data=p->data;q->next=NULL;

  • r->next=q;r=q; p=p->next;

  • }

  • r->next=NULL;

  • return str;

  • }


6532716

(8) DelStr(s,i,j)

si(1iStrLength(s))j,

LiString *DelStr(LiString *s,int i,int j)

{ int k;

LiString *str,*p=s->next,*q,*r;

str=(LiString *)malloc(sizeof(LiString));

r=str;

if(i<=0||i>StrLength(s) || j<0 || i+j-1>StrLength(s))

{ printf("\n");

return str; /**/

}


6532716

for(k=0;k<i-1;k++)/*si-1str*/

{ q=(LiString *)malloc(sizeof(LiString));

q->data=p->data;q->next=NULL;

r->next=q;r=q;p=p->next;

}

for (k=0;k<j;k++) /*pnextj*/

p=p->next;

while(p!=NULL)/**pstr*/

{ q=(LiString *)malloc(sizeof(LiString));

q->data=p->data;q->next=NULL;

r->next=q;r=q;p=p->next;

}

r->next=NULL;

return str;

}


6532716

(9) RepStr(s,i,j,t)

s,i(1iStrLength(s))jt,

LiString *RepStr(LiString *s,int i,int j,LiString *t)

{ int k;

LiString *str,*p=s->next,*p1=t->next,*q,*r;

str=(LiString *)malloc(sizeof(LiString));

r=str;

if(i<=0||i>StrLength(s) || j<0 || i+j-1>StrLength(s))

{ printf("\n");

return str; /**/

}


6532716

for (k=0;k<i-1;k++) /*si-1str*/

{ q=(LiString *)malloc(sizeof(LiString));

q->data=p->data;q->next=NULL;

r->next=q;r=q;p=p->next;

}

for (k=0;k<j;k++) /*pnextj*/

p=p->next;

while (p1!=NULL) /*tstr*/

{ q=(LiString *)malloc(sizeof(LiString));

q->data=p1->data;q->next=NULL;

r->next=q;r=q;p1=p1->next;

}


6532716

while (p!=NULL) /**pstr*/

{ q=(LiString *)malloc(sizeof(LiString));

q->data=p->data;q->next=NULL;

r->next=q;r=q;

p=p->next;

}

r->next=NULL;

return str;

}


6532716

(10) DispStr(s)

s

void DispStr(LiString *s)

{ LiString *p=s->next;

while (p!=NULL)

{ printf("%c",p->data);

p=p->next;

}

printf("\n");

}


6532716

4.3 ,"ab""xyz"

:s"ab",pdata'a',data'b'data'x''z',data'y',*p:


6532716

void Repl(LiString *&s)

{LiString *p=s->next,*q;int find=0;

while (p->next!=NULL && find==0)

{ if(p->data==a&& p->next->data==b) /**/

{ p->data='x';p->next->data='z';

/*xyz*/

q=(lstring *)malloc(sizeof(lstring));

q->data='y';q->next=p->next;

p->next=q;

find=1;

}

else p=p->next;

}

}


6532716

(1)

(2)


  • Login