zoukankan      html  css  js  c++  java
  • C-哈夫曼编码

    /*author:windy_2*/
    /*修正版*/
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    typedef struct slink
    {
        char data;
        int wp;
        int bm;
        int mark;
        struct slink *left;
        struct slink *right;
    }*link;
    struct hfm
    {
        char data;
        int wp;
    };
    struct T
    {
        int data;
        struct T *next;
    };
    struct te
    {
        int data;
        struct te *next;
    };
    struct slink* create_tree(struct hfm arr[],int len);
    void print(struct slink *root);
    int pop();
    void push(int k);
    struct T *T;
    struct te *te;
    int pop_s(struct T *strack);
    void push_t(int k);
    int pop_t();
    int main()
    {
        struct slink *root;
        int i,k=0;
        char str[20];
        char a[20];
        int b[20];
        int count,j;
        struct hfm *arr;
        T = (struct T*)malloc(sizeof(struct T));
        T->data = 0;
        T->next = NULL;
        te = (struct te*)malloc(sizeof(struct te));
        te->data = 0;
        te->next = NULL;
        printf("请输入你要进行编码的字符串: 
    ");
        scanf("%s",str);
        if(str[strlen(str)] == '')
        {
            str[strlen(str)]=NULL;
        }
        for(i = 0;i < strlen(str);i++)
        {
            if(str[i]!='#')
            {
                a[k] = str[i];
                b[k] = 0;
                for(j = 0;j<strlen(str);j++)
                {
                    if(str[j] == str[i])
                    {
                        b[k]++;
                    }
                }
                for(j = 0;j<strlen(str);j++)
                {
                    if(str[j] == a[k])
                    {
                        str[j] = '#';
                    }
                }
                k++;
            }
            count = k;
        }
        arr = (struct hfm*)malloc(sizeof(struct hfm)*count);
        for(i = 0;i < count;i++)
        {
            arr[i].data = a[i];
            arr[i].wp = b[i];
        }
        root = (struct slink*)malloc(sizeof(struct slink));
        root = create_tree(arr,count);
        print(root);
    }
    struct slink* create_tree(struct hfm arr[],int len)
    {
        link ptrarr[20];
        link ptr,root = NULL;
        int i,k1 = -1,k2,j;
        for(i = 0;i < len;i++)
        {
            ptr = (link)malloc(sizeof(struct slink));
            ptr->data = arr[i].data;
            ptr->wp = arr[i].wp;
            ptr->mark = 0;
            ptr->left = NULL;
            ptr->right = NULL;
            ptrarr[i] = ptr;
        }
        for(i = 1;i < len;i++)
        {
            for(j = 0;j < len;j++)
            {
                if(ptrarr[j]!=NULL&&k1==-1)
                {
                    k1 = j;
                    continue;
                }
                if(ptrarr[j]!=NULL)
                {
                    k2 = j;
                    break;
                }
            }
            for(j = k2;j < len;j++)
            {
                if(ptrarr[j] != NULL)
                {
                    if(ptrarr[j]->wp > ptrarr[k1]->wp)
                    {
                        k2 = k1;
                        k1 = j;
                    }
                    else if(ptrarr[j]->wp < ptrarr[k2]->wp)
                    {
                        k2 = j;
                    }
                }
            }
            root = (link)malloc(sizeof(struct slink));
            ptrarr[k1]->bm = 0;
            ptrarr[k2]->bm = 1;
            root->wp = ptrarr[k1]->wp + ptrarr[k2]->wp;
            root->data = '!';
            root->mark = 0;
            root->left = ptrarr[k1];
            root->right = ptrarr[k2];
            ptrarr[k1] = root;
            ptrarr[k2] = NULL;
        }
        return root;
    }
    void print(struct slink *root)
    {
        int temp;
        struct T *p;
        if(root == NULL)
        {
            return;
        }
        else
        {
            if(root->bm == 0||root->bm == 1)
            {
                push(root->bm);
            }
            if(root->data != '!')
            {
                printf("%c的哈夫曼编码为: ",root->data);
                root->mark = 1;
                temp = pop();
                push_t(temp);
                p = T->next;
                while(p!=NULL)
                {
                    temp = pop_s(p);
                    push_t(temp);
                    p = p->next;
                }
                while(te->next!=NULL)
                {
                    temp = pop_t();
                    printf("%d",temp);
                }
                printf("
    ");
                if(root->bm == 1)
                {
                    temp = pop();
                }
            }
            print(root->left);
            print(root->right);
        }
    }
    int pop()
    {
        struct T *p;
        int n;
        if(T->next == NULL)
        {
            return 0;
        }
        else
        {
            p = T->next;
            n = p->data;
            T->next = p->next;
            free(p);
            return n;
        }
    }
    void push(int k)
    {
        struct T *strack;
        strack = (struct T*)malloc(sizeof(struct T));
        strack->data = k;
        strack->next = T->next;
        T->next = strack;
    }
    int pop_s(struct T *strack)
    {
        struct T *p;
        int n;
        if(T->next == NULL)
        {
            return 0;
        }
        else
        {
            p = strack;
            n = p->data;
            return n;
        }
    }
    void push_t(int k)
    {
        struct te *strack;
        strack = (struct te*)malloc(sizeof(struct te));
        strack->data = k;
        strack->next = te->next;
        te->next = strack;
    }
    int pop_t()
    {
        struct te *p;
        int n;
        if(te->next == NULL)
        {
            return -1;
        }
        else
        {
            p = te->next;
            n = p->data;
            te->next = p->next;
            free(p);
            return n;
        }
    }
  • 相关阅读:
    矩阵树定理 / 生成树计数
    NC20811 蓝魔法师 (树形DP/树上01背包)
    Xor-MST学习/ 2020牛客暑假多校(五)B.Graph
    HDU-6820 Tree (2020杭电多校(五) 1007)
    Flipping Coins (概率DP)
    宝石装箱 容斥+dp
    Rabbit的工作(1) (dP)
    Codeforces-1350 E.Orac and Game of Life
    HDU-6563 Strength (贪心)
    HDU-6558 The Moon (期望DP)
  • 原文地址:https://www.cnblogs.com/aWxvdmVseXc0/p/10099506.html
Copyright © 2011-2022 走看看