ADT String is
operations
String createNullStr(void)
int isNullStr(String s)
int length(String s)
String concat(String s1,String s2)
String subStr_seq(String s,int i, int j)
int index(String s1,String s2)
end ADT String
struct SeqString{
int MAXNUM;
int n;
char *c;
};
typedef struct SeqString *PSeqString;
PSeqString createNullStr(int m)
{
PSeqString pstr = (PSeqString)malloc(sizeof(struct SeqString);
if(pstr != NULL){
pstr->c = (char *)malloc(sizeof(char) *m);
if(pstr->c){
pstr->n = 0;
pstr->MAXNUM = m;
return pstr;}
else free(pstr);
}
printf("Out of space!\n");
return NULL;
}
PSeqString subStr_seq(PSeqString s, int i, int j)
{
PSeqString s1;
int k;
s1 = createNullStr(j);
if(s1 == NULL)
return NULL;
if(i > 0 && i<s->n && j>0){ //判断 i,j是否合法
if(s->n<i+j-1) //i+j超出n的处理范围
j=s->n-i+1; //把i到顺序表尾的数值标记为j
for(k=0;k<j;k++) //j个
s1->c[k] = s->c[i+k-1]; //从下标为i-1即i个开始赋值
s1->n = j; //不能忘记把s1的数赋值为j
}
return s1;
}
//----------------------------链接表示------------------------------------
struct StrNode;
typedef struct StrNode *PStrNode;
struct StrNode{
char c;
PStrNode link;
};
typedef struct StrNode * LinkString;
LinkString createNullStr_link(void)
{
LinkString pst;
pst = (LinkString)malloc(sizeof(struct StrNode));
if(pst!=NULL)
pst->link = NULL;
else
printf("Out of space!\n");
return pst;
}
LinkString subStr_Link(LinkString s,int i, int j)
{
LinkString s1;
PStrNode p,q,t;
int k;
s1 = createNullStr_link();
if(c1 == NULL)
printf("Out of space!\n");
return NULL;
if(i<1 || j<1)
return s1;
p=s;
for(k=1;k<=i;k++)
if(p!=NULL)
p=p->link;
else
return s1;
if(p == NULL)
return s1;
t=s1;
for(k=1;k <= j;k++)
if(p!=NULL)
{
q = (PStrNode)malloc(sizeof(struct StrNode));
if(q = NULL)
printf("Out of space!\n");
return s1;
q->c = p->c;
q->link = NULL;
t->link = q;
t = q;
p = p->link;
}
return s1;
}