首页 > 编程 > C++ > 正文

C语言数据结构之动态分配实现串

2020-05-23 13:38:45
字体:
来源:转载
供稿:网友

C语言数据结构之动态分配实现串

说明:堆分配存储实现串时,串并不是以'/0‘, 而是用数据项int length来表示的,所以和传统的c语言操作字符串有所不同。

头文件

#ifndef PILEHEAD_H_INCLUDED#define PILEHEAD_H_INCLUDED#include <stdio.h>#include <stdlib.h>typedef struct{  char* ch ;  int len ;}HString ;int StrAssign( HString* pstr , char* pch ) ;int StrLength( HString* pstr ) ;int StrCompaer( HString* pstr1 , HString* pstr2 ) ;int StrClear( HString* pstr ) ;int StrConcat( HString* pstr , HString str1 , HString str2 ) ;int StrSub( HString* sub , HString pstr , int pos , int length ) ;int StrPrint( HString* pstr ) ;#endif // PILEHEAD_H_INCLUDED

函数实现

#include "pilehead.h"int StrAssign( HString* pstr , char* pch ){  if( !pstr->len )  {    free( pstr->ch ) ;  }  int i ;  for( i = 0 ; pch[i] != '/0' ; i++ )  {    ;  }  if( i == 0 )  {    pstr->ch = NULL ;    pstr->len = 0 ;  }  else  {    pstr->ch = ( char* )malloc( i * sizeof( char ) ) ;    pstr->len = i ;  }  i = 0 ;  while( i < pstr->len )  {    pstr->ch[i] = pch[i] ;    i++ ;  }  return 0 ;}int StrPrint( HString* pstr ){  int i = 0 ;  while( i < pstr->len )  {    printf( "%c" , pstr->ch[i] ) ;    i++ ;  }  printf( "/n" ) ;  return 0 ;}int StrLength( HString* pstr ){  return pstr->len ;}int StrCompaer( HString* pstr1 , HString* pstr2 ){  int i = 0 ;  int ret = 0 ;  while( i < pstr1->len && i <pstr2->len )  {    ret = pstr1->ch[i] - pstr2->ch[i] ;    if( ret < 0 )    {      return -1 ;    }    else if( ret > 0 )    {      return 1 ;    }    else    {      i++ ;    }  }  int diff = pstr1->len - pstr2->len ;  if( diff < 0 )  {    return -1 ;  }  else if( diff == 0 )  {    return 0 ;  }  return 1 ;}int StrClear( HString* pstr ){  if( pstr->ch )  {    free( pstr->ch ) ;    pstr->ch = NULL ;  }  pstr->len = 0 ;  return 0 ;}int StrConcat( HString* pstr , HString str1 , HString str2 ){  StrClear( pstr ) ;  StrAssign( pstr , str1.ch ) ;  int len = str1.len ;  int len2 = 0 ;  while( len2 < str2.len )  {    pstr->ch[len++] = str2.ch[len2++] ;  }  pstr->len = str1.len + str2.len ;  return 0 ;}int StrSub( HString* sub , HString pstr , int pos , int length ){  if( pos < 1 || pos > pstr.len || length < 1 || length > pstr.len - pos + 1 )  {    printf( "ERROR!/n" ) ;    exit( 1 ) ;  }  StrClear( sub ) ;  sub->ch = ( char* )malloc( length * sizeof( char ) ) ;  sub->len = length ;  int i = 0 ;  while( i < length )  {    sub->ch[i] = pstr.ch[pos - 1 + i] ;    i++ ;  }  return 0 ;}

测试函数

#include "pilehead.h"int main(){  HString str ;  HString str1 ;  HString str2 ;  char* pch = "abcd" ;  StrAssign( &str , pch ) ;  StrAssign( &str1 , "abcd" ) ;  StrAssign( &str2 , "fhasdifhos" ) ;  StrPrint( &str ) ;  StrPrint( &str1 ) ;  /*StrClear( &str ) ;  StrPrint( &str ) ;  int i = StrCompaer( &str , &str1 ) ;  printf( "%d" , i ) ;*/  StrConcat( &str , str1 , str2 ) ;  StrPrint( &str ) ;  HString s ;  StrSub( &s ,str , 4 , 5 ) ;  StrPrint( &s ) ;  return 0;}

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表