繁体   English   中英

全局变量问题

[英]global variable problem

我声明一个全局变量,并在函数中使用和修改其值。 然后我想获取此全局变量的修改后的值,它有一些问题。 谁能帮我?

#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 10
struct link
{
            int freq;
            char value[MAX];
            struct link* right;
            struct link* left;
};
typedef struct link node;
void sort(node *[], int);
node* create(char[], int);
void sright(node *[], int);
void Assign_Code(node*, int [], int);
void Delete_Tree(node *);


int test[720][720];

main()
{
    node* ptr, * head;
    int i, n, total = 0, u, c[256];
    char str[MAX];
    node* a[256];
    int freq;

    printf(  "Huffman Algorithm\n");
    printf("\nEnter the no. of letter to be coded:");
    /*input the no. of letters*/
    scanf("%d", &n);
    for (i = 0; i < n; i++)
    {
        printf("Enter the letter & frequency:");
        /*input the letter & frequency*/
        scanf("%s %d", str, &freq);
        a[i] = create(str, freq);
    }
    while (n > 1)
    {
        sort(a, n);
        u = a[0]->freq + a[1]->freq;
        strcpy(str,a[0]->value);
        strcat(str,a[1]->value);
        ptr = create(str, u);
        ptr->right = a[1];
        ptr->left = a[0];
        a[0] = ptr;
        sright(a, n);
        n--;
    }

    Assign_Code(a[0], c, 0);
   //getch();
    printf("Code: ");
      for (i = 1; i <= n; i++)
        {
            printf("%d", test[0][i]);
        }
        printf("\n");

    Delete_Tree(a[0]);


}

node* create(char a[], int x)
{
    node* ptr;
    ptr = (node *) malloc(256*sizeof(node));
    ptr->freq = x;
    strcpy( ptr->value , a);
    ptr->right = ptr->left = NULL;
    return(ptr);
}
void sort(node* a[], int n)
{
    int i, j;
    node* temp;
    for (i = 0; i < n - 1; i++)
        for (j = i; j < n; j++)
            if (a[i]->freq > a[j]->freq)
            {
                temp = a[i];
                a[i] = a[j];
                a[j] = temp;
            }
}
void sright(node* a[], int n)
{
    int i;
    for (i = 1; i < n - 1; i++)
        a[i] = a[i + 1];
}
void Assign_Code(node* tree, int c[], int n)
{
    int i;
    if ((tree->left == NULL) && (tree->right == NULL))
    {
        printf("%s code: ", tree->value);
        test[0][0]=tree->value;
        for (i = 0; i < n; i++)
        {
             test[0][i+1]=c[i];
            printf("%d", c[i]);
        }
        printf("\n");

    }
    else
    {
        c[n] = 1;
        n++;
        Assign_Code(tree->left, c, n);
              c[n - 1] = 0;
        Assign_Code(tree->right, c, n);
    }
}
void Delete_Tree(node * root)
{
    if(root!=NULL)
    {
        Delete_Tree(root->left);
        Delete_Tree(root->right);
        free(root);
    }
}

让我强调一下问题:

while (n > 1)
{
    ...
    n--;
}
...
for (i = 1; i <= n; i++)
{
    printf("%d", test[0][i]);
}

到第二个循环开始时, n为1,并且printf仅执行一次,因此仅输出test[0][1]的值。

test[0][1]的值在Assign_Code被多次重写(与树中的叶节点数一样多):

void Assign_Code(node* tree, int c[], int n)
{
    if ((tree->left == NULL) && (tree->right == NULL))
    {
        ...
        for (i = 0; i < n; i++)
        {
             test[0][i+1]=c[i];
        }
    }
    ...
}

现在,由于您遍历树的方式,最后一次覆盖test[0][1]是针对第一个字符为“ 0”的霍夫曼代码的。

 you can store the n value to some temporary variables, after you get the 

价值观。

 Then use the temporary variable in your for loop condition. 
scanf("%d", &n); 
int temp = n ; 
for (i = 1; i <= temp ; i++)
{
    printf("%d", test[0][i]);
}

附带说明:

  node* create(char a[], int x)
{
    node* ptr;
    ptr = (node *) malloc(256*sizeof(node));  // <--- This is wrong
    ptr->freq = x;
    strcpy( ptr->value , a);
    ptr->right = ptr->left = NULL;
    return(ptr);
}

没有理由分配256倍的节点大小来存储一个节点。 您正在创建一个节点,并将其存储在指向节点的指针数组中。 在那里分配一个节点,如下所示:

malloc (sizeof (node)); or malloc ((sizeof (*ptr));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM