[Data structure] represents and implements heap allocation of strings, and heap allocation of data structures

Source: Internet
Author: User

[Data structure] represents and implements heap allocation of strings, and heap allocation of data structures

The strings represented by heap allocation storage are called heap strings. Compared with sequential strings, the address space is still continuous, but the space is dynamically allocated during program execution.

The realloc function used in the program:


// Realloc: (void *) reelloc (void * ptr, unsigned newsize );
// Use: char * str;
// Str = (char *) realloc (str, 20 );





The code is implemented as follows:


<Span style = "font-size: 18px;" ># pragma once # include <iostream> using namespace std; # define MAX_SZIE 30 typedef struct {char * str; int length ;} heapString;/* judge null */bool IsEmpty (HeapString * s);/* initialize */void InintString (HeapString * s ); /* evaluate the Length */int Length (HeapString * s);/* assign a value to the string */bool StrAssign (HeapString * s, char cstr []); /* print */void StrPrint (HeapString * s);/* Insert the string T at the pos position of S */bool StrInsert (HeapString * S, int pos, HeapString * T ); /* Delete the sub-string whose length is len at the pos position of S */bool StrDelete (HeapString * S, int pos, int len ); /* clear string */void ClearString (HeapString * s);/* Destroy */void Destroy (HeapString * s ); /* SubString sub */bool SubString (HeapString * sub, HeapString * S, int pos, int len) from the position of s ); /* splice */bool StringConcat (HeapString * T, HeapString * S, HeapString * S2);/* Copy */bool StrCopy (HeapString * T, HeapString * S ); /* compare */int StrCompare (HeapString * T, HeapString * S ); /* start from the pos position of S to locate the initial position of the substring T */int StrIndex (HeapString * S, int pos, HeapString * T ); /* replace all substring T in S with V */bool StrReplace (HeapString * S, HeapString * T, HeapString * V); </span>


<Span style = "font-size: 18px;"> # include "string. h "/* heap string */void InintString (HeapString * s) {s-> length = 0; s-> str = '\ 0 ';} bool IsEmpty (HeapString * s) {return s-> length = 0;} int Length (HeapString * s) {return s-> length ;} void PrintString (char T []) {cout <T <endl;} bool StrAssign (HeapString * s, char cstr []) {int I = 0; int len = 0; if (s-> str) free (s-> str); for (I = 0; cstr [I]! = '\ 0'; I ++); len = I; if (! I) {s-> str = '\ 0'; s-> length = 0;} else {s-> str = (char *) malloc (sizeof (char) * len); if (s-> str = NULL) return false; for (I = 0; I <len; I ++) {s-> str [I] = cstr [I];} s-> length = len;} s-> str [I] = cstr [I]; s-> length = I;} void StrPrint (HeapString * S) {int I = 0; for (I = 0; I <S-> length; I ++) {cout <S-> str [I];} cout <endl;} void ClearString (HeapString * s) {if (s-> str) free (s-> str); s-> str = '\ 0'; s-> length = 0;} voi D Destroy (HeapString * s) {if (s-> str) free (s-> str); s-> str = NULL;} bool SubString (HeapString * sub, heapString * s, int pos, int len) {if (sub-> str) free (sub-> str ); if (pos <0 | pos> s-> length | len <= 0 | len> s-> length-pos + 1) return false; sub-> str = (char *) malloc (sizeof (char) * len); if (sub-> str = NULL) return false; int I = 0; (; I <len; I ++) {sub-> str [I] = s-> str [I + pos-1];} sub-> str [len] = '\ 0'; sub-> length = len;} bool StringConcat (HeapString * T, HeapString * S1, HeapString * S2) {T-> str = (char *) realloc (T-> str, sizeof (char) * (S1-> length + S2-> length); if (T-> str = NULL) return false; int I = 0; for (; I <S1-> length; I ++) {T-> str [I] = S1-> str [I];} for (I = 0; I <S2-> length; I ++) {T-> str [S1-> length + I] = S2-> str [I];} t-> str [S1-> length + S2-> length] = '\ 0'; T-> length = S1-> length + S2-> le Ngth; return true;} bool StrCopy (HeapString * T, HeapString * S) {if (S-> str = '\ 0') return false; t-> str = (char *) realloc (T-> str, sizeof (char) * (S-> length); if (T-> str = NULL) return false; for (int I = 0; I <S-> length; I ++) T-> str [I] = S-> str [I]; t-> length = S-> length; return true;} int StrCompare (HeapString * T, HeapString * S) {int I = 0; (; I <T-> length & I <T-> length; I ++) {if (T-> str [I]! = S-> str [I]) {if (T-> str [I]> S-> str [I]) return 1; return-1 ;}} if (T-> length> T-> length) return 1; else if (T-> length <T-> length) return-1; return 0 ;} bool StrDelete (HeapString * S, int pos, int len) {int I = 0; char * p; if (pos <0 | pos> S-> length | len <0 | len> S-> length-pos + 1) return false; p = (char *) malloc (S-> length-len); if (p = NULL) return false; for (I = 0; I <pos-1; I ++) {p [I] = S-> str [I] ;}For (I = pos-1; I <S-> length-len; I ++) {p [I] = S-> str [I + len];} S-> length = S-> length-len; S-> str = p; return true ;} int StrIndex (HeapString * S, int pos, HeapString * T) {if (pos <0 | pos> S-> length | IsEmpty (T) return 0; int I = pos-1; int j = 0; while (I <S-> length & j <T-> length) {if (S-> str [I] = T-> str [j]) {I ++; j ++;} else {I = I-j + 1; j = 0 ;}} if (j >=t-> length) return I-j + 1; elseret Urn-1;} bool StrInsert (HeapString * S, int pos, HeapString * T) {int I = 0; int s_len = S-> length; int t_len = T-> length; if (pos <0 | pos-1> = S-> length) return false; S-> str = (char *) realloc (S-> str, (S-> length + T-> length) * sizeof (char); if (S-> str = NULL) {cout <"memory allocation failed" <endl; return false ;}for (I = S-> length-1; I >= pos-1; I --) {S-> str [I + T-> length] = S-> str [I];} for (I = 0; I <T-> length; I ++) {S-> str [pos-1 + I] = T-> str [I];} S-> length + = T-> length; // S [I] = '\ 0'; return true;} bool StrReplace (HeapString * S, HeapString * T, HeapString * V) {int I = 1; int flag; if (IsEmpty (T) return false; do {I = StrIndex (S, I, T); if (I) {StrDelete (S, I, Length (T )); int flag = StrInsert (S, I, V); if (! Flag) return false; I + = Length (V) ;}} while (I); return true ;}</span>


<Span style = "font-size: 18px;"> # include "string. h "void main () {HeapString s1, s2, sub, T; char ch [MAX_SZIE + 1]; char a [] =" abcdefdeg "; char B [] = "de"; char c [] = "XY"; InintString (& s1); // cout <"enter 1st strings: "<endl; // gets_s (ch); // StrAssign (& s1, ch); StrAssign (& s1, a); cout <" output string s1: "<endl; StrPrint (& s1); // SubString (& sub, s1, 2, 3); // StrPrint (& sub); InintString (& s2 ); // cout <"Enter 2nd strings:" <endl; // gets_s (ch); StrAssign (& s2, B); cout <"output string s2: "<endl; StrPrint (& s2); cout <endl; // InintString (& T); // StringConcat (& T, & s1, & s2 ); // StrPrint (& T); // StrCopy (& s1, & s2); // StrPrint (& s1); // cout <StrCompare (& s1, & s2) <endl; // StrDelete (& s1, 2, 3); cout <StrIndex (& s1, 2, & s2) <endl; InintString (& sub ); strAssign (& sub, c); StrPrint (& sub); StrReplace (& s1, & s2, & sub); // StrInsert (& s1, 2, & sub ); strPrint (& s1); getchar () ;}</span>



The running result is as follows:





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.