串的塊鏈存儲

Common.h

#ifndef COMMON_H_INCLUDED
#define COMMON_H_INCLUDED

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2

typedef int Status;
typedef int ElemType;

#endif // COMMON_H_INCLUDED


String_Heap.h

#ifndef STRING_HEAP_H_INCLUDED
#define STRING_HEAP_H_INCLUDED

#include "Common.h"

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

/* 串的塊鏈存儲
#define CHUNKSIZE 80

typedef struct Chunk {
    char ch[CHUNKSIZE];
    struct Chunk * next;
} Chunk;

typedef struct {
    Chunk * head, * tail;
    int curlen;
} LString;
*/

Status StrAssign_Heap(HString * s, char * chars);
Status StrCopy_Heap(HString * s, HString * t);
Status StrEmpty_Heap(HString * s);
int StrCompare_Heap(HString * s, HString * t);
int StrLength_Heap(HString * s);
Status ClearString_Heap(HString * s);
Status Concat_Heap(HString * t, HString * s1, HString * s2);
Status SubString_Heap(HString * sub, HString * s, int pos, int len);
int Index_Heap(HString * s, HString * t, int pos);
Status Replace_Heap(HString * s, HString * t, HString * v);
Status StrInsert_Heap(HString * s, int pos, HString * t);
Status StrDelete_Heap(HString * s, int pos, int len);
Status DestroyString_Heap(HString * s);
void StringPrint_Heap(HString * s);

#endif // STRING_HEAP_H_INCLUDED


String_Heap.c

#include "String_Heap.h"

Status StrAssign_Heap(HString * s, char * chars)
{
    int length = strlen(chars);
    if(s->ch)
        free(s->ch);
    s->ch = (char *)malloc(length*sizeof(char));
    int i;
    for(i=0; i<length; i++)
        s->ch[i] = chars[i];
    s->length = length;
    return OK;
}

Status StrCopy_Heap(HString * s, HString * t)
{
    if(s->ch)
        free(s->ch);
    s->ch = (char *)malloc(t->length*sizeof(char));
    if(!s->ch)
        exit(OVERFLOW);
    int i;
    for(i=0; i<t->length; i++)
    {
        s->ch[i] = t->ch[i];
    }
    s->length = t->length;
    return OK;
}

Status StrEmpty_Heap(HString * s)
{
    if(s->length == 0)
        return TRUE;
    else
        return FALSE;
}

int StrCompare_Heap(HString * s, HString * t)
{
    int i;
    for(i=0; i<s->length && i<t->length; i++)
    {
        if(s->ch[i] != t->ch[i])
            return s->ch[i] - t->ch[i];
    }
    return s->length - t->length;
}

int StrLength_Heap(HString * s)
{
    return s->length;
}
Status ClearString_Heap(HString * s)
{
    if(s->ch)
    {
        free(s->ch);
        s->ch = NULL;
    }
    s->length = 0;
    return OK;
}

Status Concat_Heap(HString * t, HString * s1, HString * s2)
{
    if(t->ch)
        free(t->ch);
    t->length = s1->length+s2->length;
    t->ch = (char *)malloc(t->length*sizeof(char));
    if(!t->ch)
        exit(OVERFLOW);
    int i;
    for(i=0; i<s1->length; i++)
    {
        t->ch[i] = s1->ch[i];
    }
    for(i=0; i<s2->length; i++)
    {
        t->ch[i+s1->length] = s2->ch[i];
    }
    return OK;
}

Status SubString_Heap(HString * sub, HString * s, int pos, int len)
{
    if(pos<1 || pos>StrLength_Heap(s) || len<0 || len>StrLength_Heap(s)-pos+1)
        return ERROR;
    if(sub->ch)
        free(sub->ch);
    sub->length = len;
    sub->ch = (char *)malloc(len * sizeof(char));
    if(!sub->ch)
        exit(OVERFLOW);
    int i;
    for(i=0; i<len; i++)
    {
        sub->ch[i] = s->ch[i+pos-1];
    }
    return OK;
}
int Index_Heap(HString * s, HString * t, int pos)
{
    if(pos < 1 || pos>StrLength_Heap(s))
        return ERROR;
    int i = pos;
    int tlen = StrLength_Heap(t);
    int slen = StrLength_Heap(s);
    HString * sub = (HString *)malloc(sizeof(HString));

    while(i <= slen-tlen+1)
    {

        SubString_Heap(sub, s, i, tlen);
        if(StrCompare_Heap(sub, t)!= 0)
            i++;
        else
            return i;
    }
    return 0;
}

Status Replace_Heap(HString * s, HString * t, HString * v)
{
    int i=1;
    if(StrEmpty_Heap(t))
        return ERROR;
    do
    {
        i=Index_Heap(s,t,i);
        if(i)
        {
            StrDelete_Heap(s,i,StrLength_Heap(t));
            StrInsert_Heap(s,i,v);
            i+=StrLength_Heap(v);
        }
    }while(i);
    return OK;
}

Status StrInsert_Heap(HString * s, int pos, HString * t)
{
    char * temp = (char *)malloc(s->length*sizeof(char));
    int j;
    for(j=0; j<s->length; j++)
        temp[j] = s->ch[j];
    free(s->ch);
    s->length += t->length;
    s->ch = (char *)malloc(s->length*sizeof(char));
    if(!s->ch)
        exit(OVERFLOW);
    int i;
    for(i=0; i<pos-1; i++)
    {
        s->ch[i] = temp[i];
    }
    for(i=0; i<t->length; i++)
    {
        s->ch[i+pos-1] = t->ch[i];
    }
    for(i=pos+t->length-1; i<s->length; i++)
    {
        s->ch[i] = temp[i-t->length];
    }
    free(temp);

    return OK;
}

Status StrDelete_Heap(HString * s, int pos, int len)
{
    if(pos < 1 || pos>StrLength_Heap(s)-len+1)
        return ERROR;
    char * temp = (char *)malloc(s->length*sizeof(char));
    int j;
    for(j=0; j<s->length; j++)
        temp[j] = s->ch[j];
    free(s->ch);
    s->ch = (char *)malloc((s->length-len)*sizeof(char));
    if(!s->ch)
        exit(OVERFLOW);
    s->length -= len;
    int i;
    for(i=0; i<pos-1; i++)
    {
        s->ch[i] = temp[i];
    }
    for(i=pos-1; i<=s->length; i++)
    {
        s->ch[i] = temp[i+len];
    }
    free(temp);
    return OK;
}
Status DestroyString_Heap(HString * s)
{
    ClearString_Heap(s);
    return OK;
}
void StringPrint_Heap(HString * s)
{
    int i;
    for(i=0; i<s->length; i++)
        printf("%c", s->ch[i]);
    printf("\n");
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章