C语言实现串的堆分配存储

/*
 * heap_string.h
 * 串的堆分配存储实现,用这种实现方法的好处是,能够动态的给
 * 串分配内存空间,而顺序串不能
 *  Created on: 2011-9-7
 *      Author: root
 */

#define ElemType char;
#define TRUE 1
#define FALSE 0

typedef struct {
	char *ch;
	int length;
}hstring;

//初始化串
void inithstring(hstring *s) {
	 s->length = 0;
	 s->ch = NULL;
}
//用字符串常量创建串
int createhstring(hstring *s,char chars[]) {
	if(s->ch) free(s->ch);
	int i=0;
	while(chars[i]) {
		i ++;
	}
	if(i == 0) return FALSE;
	s->length = i;
	s->ch = (char *)malloc(sizeof(char) * s->length);
	if(s->ch == NULL) return FALSE;
	for(i = 0;i < s->length;i ++) {
		s->ch[i] = chars[i];
	}
	s->ch[i ++] = '\0';
	return TRUE;
}
//比较两个串是否相等若s1>s2返回值>0若s1<s2返回值<0若s1=s2返回0
int hstringcompare(hstring s1,hstring s2) {
	int i = 0;
	while(s1.ch[i] && s2.ch[i]) {
		if(s1.ch[i] - s2.ch[i] > 0) return s1.ch[i] -
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值