标签:blog http io ar sp for on 文件 数据
本次实现的数据结构是关于串的表示与实现,首先讲串的堆分配存储表示与实现,后面的博客将贴出串的另外数据结构的表示和实现
heapstring.h文件存放数据结构体,如下
#ifndef _HEAPSRING_H_ #define _HEAPSRING_H_ typedef struct _HString { char *ch; int length; }HString,*pHString; pHString init_null_string(void); pHString init_heap_string(int length); int init_char_string(pHString ps,char* chead); void print_string(pHString ps); int copy_string(pHString dstring,pHString sstring); int concat_string(pHString dps,pHString ps1,pHString ps2) ; #endif
heapstring.c文件存放数据结构的实现(只实现简单的,给大家开个头,后面复杂一些的大家可以自己编写):
/******************************* 时间:2014.12.12 作者:XIAO_PING_PING 内容:串的堆分配存储表示与实现 功能:学习些数据结构 ********************************/ #include <string.h> #include <stdlib.h> #include "heapstring.h" /*生成一个空串*/ pHString init_null_string(void) { pHString pstring; pstring = (HString *)malloc(sizeof(HString)); pstring->ch = NULL; pstring->length = 0; return pstring; } /*初始化一个给定长度的串*/ pHString init_heap_string(int length) { pHString pstring; //char* pch; pstring = (HString *)malloc(sizeof(HString)); pstring->ch = (char *)malloc(length * sizeof(char)); pstring->length = length; return pstring; } /*生成一个指定字符串的串*/ int init_char_string(pHString ps,char* chead) { //pHString pstring; int size = strlen(chead); int i; if(NULL != ps->ch) { free(ps->ch); } ps->ch = (char *)malloc(size * sizeof(char)); for(i = 0;i < size;i++) { ps->ch[i] = chead[i]; //*(&(ps->ch[0]) + i) = chead[i]; } //ps->ch[i] = ' '; ps->length = size; return 0; } /*复制字符串*/ int copy_string(pHString dstring,pHString sstring) { int size = sstring->length;//strlen(sstring->ch); int i; if(NULL == sstring->ch && 0 == sstring->length) { printf("源串内容为空\n"); return -1; } if(NULL != dstring->ch) { free(dstring->ch); } dstring->ch = (char *)malloc(size * sizeof(char)); for(i = 0;i < size;i++) { dstring->ch[i] = sstring->ch[i]; //*(&(ps->ch[0]) + i) = chead[i]; } dstring->length = size; } /*拼接字符串*/ int concat_string(pHString dps,pHString ps1,pHString ps2) { int size = ps1->length + ps2->length; int i; if(0 == size) { printf("无拼接内容\n"); return -1; } if(NULL != dps->ch) { free(dps->ch); } dps->ch = (char *)malloc(size * sizeof(char)); for(i = 0;i < ps1->length;i++) { dps->ch[i] = ps1->ch[i]; } for(i = 0;i < ps2->length;i++) { dps->ch[ps1->length + i] = ps2->ch[i]; } dps->length = size; return 0; } /*打印串里面的字符串*/ void print_string(pHString ps) { int i; for(i = 0;i < ps->length;i++) { printf("%c",ps->ch[i]); } }
测试文件test.c如下:
<pre class="objc" name="code">#include <string.h> #include <stdlib.h> #include <conio.h> #include "heapstring.h" int main() { pHString p,np,cp; char *ch = "helloword!"; p = init_null_string(); init_char_string(p,ch); printf("打印字符串一:"); print_string(p); printf("\n"); np = init_null_string(); copy_string(np,p); printf("打印复制的字符串二:"); print_string(np); printf("\n"); cp = init_null_string(); concat_string(cp,p,np); printf("打印拼接一和二的字符串:"); print_string(cp); printf("\n"); getch(); return 0; }
运行结果如下:
标签:blog http io ar sp for on 文件 数据
原文地址:http://blog.csdn.net/xiao_ping_ping/article/details/41916205