This implementation of the data structure is about the expression and implementation of the string, first of all, the heap allocation storage representation and implementation, the following blog will post a string of additional data structure representation and implementation
Heapstring.h file data structure body, as follows
#ifndef _heapsring_h_#define _heapsring_h_typedef struct _hstring{ char *ch;
HEAPSTRING.C File data structure implementation (simple, for everyone to open a head, behind the more complex people can write their own):
/******************************* Time: 2014.12.12xiao_ping_ping content: Heap allocation storage representation and implementation of strings: learning some data structures ************************** /#include <string.h> #include <stdlib.h> #include "heapstring.h"/* Generate an empty string */phstring Init_null_ String (void) {phstring pstring; pstring = (HString *) malloc (sizeof (HString)); Pstring->ch = NULL; pstring->length = 0; return pstring; }/* Initializes a given length of string */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;} /* Generate a String */int init_char_string (phstring ps,char* chead) {//phstring pstring) of the specified strings; 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; }/* Copy string */int copy_string (phstring dstring,phstring sstring) {int size = Sstring->length;//strlen (sstring->ch); int i; if (NULL = = Sstring->ch && 0 = = sstring->length) {printf ("The source string contents are empty \ 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;} /* Stitching string */int concat_string (phstring dps,phstring ps1,phstring ps2) {int size = Ps1->length + ps2->length; int i; if (0 = = size) {printf ("no splicing content \ 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; }/* the string inside the print string */void print_string (phstring PS) {int i; for (i = 0;i < ps->length;i++) {printf ("%c", Ps->ch[i]); } }
The test file test.c is as follows:
<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 One:"); Print_string (p); printf ("\ n"); NP = Init_null_string (); Copy_string (np,p); printf ("Print copied string two:"); Print_string (NP); printf ("\ n"); CP = Init_null_string (); Concat_string (CP,P,NP); printf ("Print concatenation one and two strings:"); Print_string (CP); printf ("\ n"); Getch (); return 0;}
The results of the operation are as follows:
Heap allocation storage representation and implementation of strings--writing data structures by themselves