当前位置: 移动技术网 > IT编程>开发语言>C/C++ > 数据结构之---C语言实现串的顺序存储

数据结构之---C语言实现串的顺序存储

2018年03月31日  | 移动技术网IT编程  | 我要评论

孙宇翱,爆爆球,美国十次l

//C语言串的顺序存储表示
//串的堆分配存储表示
//杨鑫
#include 
#include 
#include 
#define MAXSTRLEN 255
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2

//定义数据元素结的构
typedef int Status;
typedef struct 
{
		char *ch;
		int length;
}HString;

//生成一个其值等于串常量chars的串T
 Status StrAssign(HString *T,char *chars)
 { 
   int i,j;
   if((*T).ch)
     free((*T).ch); 
   i=strlen(chars); 
   if(!i)
   { 
     (*T).ch=NULL;
     (*T).length=0;
   }
   else
   { 
     (*T).ch=(char*)malloc(i*sizeof(char));
     if(!(*T).ch)
       exit(OVERFLOW);
     for(j=0;jS.length||lenS.length-pos+1)
     return ERROR;
   if((*Sub).ch)
     free((*Sub).ch); 
   if(!len) 
   {
     (*Sub).ch=NULL;
     (*Sub).length=0;
   }
   else
   { 
     (*Sub).ch=(char*)malloc(len*sizeof(char));
     if(!(*Sub).ch)
       exit(OVERFLOW);
     for(i=0;i0)
   {
     n=StrLength(S);
     m=StrLength(T);
     i=pos;
     while(i(*S).length+1)
     return ERROR;
   if(T.length) 
   {
     (*S).ch=(char*)realloc((*S).ch,((*S).length+T.length)*sizeof(char));
     if(!(*S).ch)
       exit(OVERFLOW);
     for(i=(*S).length-1;i>=pos-1;--i) 

       (*S).ch[i+T.length]=(*S).ch[i];
     for(i=0;i';
   printf("串s%c串t\n",c);
   Concat(&r,t,s);
   printf("串t联接串s产生的串r为: ");
   StrPrint(r);
   StrAssign(&s,"oo");
   printf("串s为: ");
   StrPrint(s);
   StrAssign(&t,"o");
   printf("串t为: ");
   StrPrint(t);
   Replace(&r,t,s);
   printf("把串r中和串t相同的子串用串s代替后,串r为:\n");
   StrPrint(r);
   ClearString(&s);
   printf("串s清空后,串长为 : %d 是否为空?%d ( 1 : 空 0 : 否)\n",StrLength(s),StrEmpty(s));
   SubString(&s,r,6,4);
   printf("串s为从串r的第6个字符起的4个字符,长度为 %d 串s为: ",s.length);
   StrPrint(s);
   StrCopy(&t,r);
   printf("复制串t为串r,串t为: ");
   StrPrint(t);
   StrInsert(&t,6,s);
   printf("在串t的第6个字符前插入串s后,串t为: ");
   StrPrint(t);
   StrDelete(&t,1,5);
   printf("从串t的第1个字符起删除5个字符后,串t为: ");
   StrPrint(t);
   printf("%d是从串t的第1个字符起,和串s相同的第1个子串的位置\n",Index(t,s,1));
   printf("%d是从串t的第2个字符起,和串s相同的第1个子串的位置\n",Index(t,s,2));
   return 0;
}



如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网