【问题标题】:How to delete any element at any particular position in array?如何删除数组中任何特定位置的任何元素?
【发布时间】:2014-01-10 22:19:11
【问题描述】:

我遇到了一个编程问题,我必须添加数组的前两个索引,然后它们的总和必须在第一个位置(删除添加的两个节点),这肯定会导致前移休息数组的一个索引位置。 但问题是它的转变非常奇怪。 请仔细看,我只需要使用数组,没有其他数据结构 这是我想要的清晰图片。看例子:

arr[5]= {1,2,3,4,5}
First execution:
{3,3,4,5} //here we added first two elements (1+2=3)
second execution:
(6,4,5) //again added first two elements
third execution:
{10,5}
finally:
{15}

我的代码是:

    Node *temp;
    Node *array[5]; //actually this array is declared like this {1,2,3,4,5}
    int i;
    for (i=0;i<5;i++)
    {
        array[i] = malloc(sizeof(Node));
        array[i]->value = freq[i+1];//this contains {1,2,3,4,5}
        array[i]->sym = arry[i];
        array[i]->left = NULL;
        array[i]->right = NULL;
    printf("%d  ",array[i]->value );
    }
    int j=0,d,t=5;
      while (t>1 )
 {

        temp = array[j];
        printf(" \ncheckOne %d  ",array[j]->value);
        printf("checkTwo %d  \n",array[j+1]->value );
        array[j]->value= temp->value + array[j+1]->value; //
        printf(" checkSumOfOneAndTwo %d  \n",temp->value);
        array[j]=array[j+1];
        array[j]->left=array[j+1];
        array[j]->right=temp;
        array[j]=temp;

            for(d=0;d < t;d++)
        printf(" %d  ",array[d]->value);
        t--; 
        j++;
    }

它的输出是:

  The Frequencies corresponding to alphabets in Input.txt  files are as follows 
: a  b  c  d  e  
1  2  3  4  5   
checkOne 1  checkTwo 2  
 checkSumOfOneAndTwo 3  
 3   2   3   4   5   
checkOne 2  checkTwo 3  
 checkSumOfOneAndTwo 5  
 3   5   3   4   
checkOne 3  checkTwo 4  
 checkSumOfOneAndTwo 7  
 3   5   7   
checkOne 4  checkTwo 5  
 checkSumOfOneAndTwo 9  
 3   5  

我是编程初学者。谁能告诉我错误是什么以及如何解决? (我有义务只使用数组来实现它,所以没有链表、堆等)

【问题讨论】:

  • array[j]-&gt;value= temp-&gt;value + array[j+1]-&gt;value; j+1 --> 5 , 数组越界
  • @BLUEPIXY 那么它的解决方案是什么?请查看我与此输入对应的输出,有什么办法可以解决?谢谢
  • 将数组作为栈处理会很容易。
  • 但是怎么做呢?任何对我有帮助的代码,但我只需要处理数组。谢谢你解释我。

标签: c arrays segmentation-fault huffman-code


【解决方案1】:

这小段代码本身就是一个问题:

    array[j]=array[j+1];
    array[j]->left=array[j+1];

执行上述代码后,您有一个节点,其“左”指针指向自身。

我会先解决这个问题。

更新:

为了有效地构建 Huffman 树,您应该使用二叉堆(顶部最小)而不是数组。在每次迭代中,您应该提取两次根,将两个节点相加,然后将结果推回堆中。

更新 #2:

如果您坚持使用数组而不是二进制堆,那么您的代码中就会出现基本概念错误:

整个执行过程期间,您的数组必须从最小值到最大值进行排序。

您从一个排序数组开始,但您不能确保它在每次迭代结束时保持排序。

简而言之,将前两个元素相加后,您不能简单地将结果作为数组中的第一个节点。相反,您必须将该节点“传播”到数组中的正确位置,以使其从最小到最大排序。

如果不这样做,可能会导致代码正常,但哈夫曼编码/解码的实现不正确。

更新 #3:

如果您坚持使用数组而不是二叉堆,下面的代码只是您应该如何构建 Huffman 树的一般框架。我还没有验证。

    // Create a new node, whose children are the first two nodes in the array
    temp = malloc(sizeof(Node));
    temp->value = array[0]->value + array[1]->value;
    temp->left  = array[0];
    temp->right = array[1];

    array_size--;

    // Push the new node into the array, and move all elements 1 entry backwards
    array[0] = temp;
    for (int k=1; k<array_size; k++)
    {
        array[k] = array[k+1];
    }

    // Move the new node forward, until the array is properly sorted (min-to-max)
    for (int k=1; k<array_size; k++)
    {
        if (array[k-1]->value < array[k]->value)
            break;
        temp = array[k-1];
        array[k-1] = array[k];
        array[k] = temp;
    }

【讨论】:

  • 您要我为您写下代码吗?如果你愿意,我可以给你发一个 C++ 的 Huffman 编码器/解码器……
  • 如果我做 array[j]=array[j+1];数组[j]->left=temp;数组[j]->右=数组[j+1];那么输出也是错误的 :: a b c d e 1 2 3 4 5 checkOne 1 checkTwo 2 checkSumOfOneAndTwo 3 3 2 3 4 5 checkOne 2 checkTwo 3 checkSumOfOneAndTwo 5 3 5 3 4 checkOne 3 checkTwo 4 checkSumOfOneAndTwo 7 3 5 7 checkOne 4 checkTwo 5 checkSumOfOneAndTwo 9 3 5 分段错误(核心转储)
  • 我想用 c 语言(如果你有的话),但我不必使用链表、堆等。只使用数组。即使这样,您也可以通过 shekhar.paris@gmail.com 转发给我,谢谢。
  • 我有自己的 C++ 实现(从上到下)。它包括二进制堆的实现(参见上面我更新的答案),这真的很简单。这是链接:Planet-Source-Code.com/vb/scripts/…。有关文档,请参阅随附的 Word 文件。
  • 你知道我想怎么做吗?实际上我有义务使用数组,请帮助我,我可以给你输出或者我可以通过电子邮件将代码发送给你,你可以更正它并回复我正确的,这可能吗?如果是,请给我您的电子邮件?
【解决方案2】:
#include <stdio.h>
#include <stdlib.h>

int main(){
    int stack_size = 5;
    int sp = stack_size;
    int *stack = malloc(stack_size * sizeof(int));//int stack[5];
    int i;
    for(i=stack_size; i>0 ;--i){
        stack[--sp] = i;//push
    }
    //print stack
    printf("{ " );
    for(i = sp;i<stack_size;++i)
        printf("%d ", stack[i]);
    printf("}\n");

    int t = stack_size;
    for(t=stack_size;t > 1;--t){
        int first, second;
        first = stack[sp++];//pop
        second = stack[sp++];//pop
        stack[--sp] = first + second;//push

        printf("{ " );//now stack print 
        for(i = sp;i<stack_size;++i)
            printf("%d ", stack[i]);
        printf("}\n");
    }

    free(stack);
    return 0;
}
/* result
{ 1 2 3 4 5 }
{ 3 3 4 5 }
{ 6 4 5 }
{ 10 5 }
{ 15 }
*/

【讨论】:

  • 感谢代码,但如果我必须在每次添加两个最小元素时进行排序呢?
  • @user3085082 不知道你说的是什么意思,但是排序,如果需要可以随时数组。
  • @user3085082 我认为如果每次向数组添加元素时都需要排序,它会使用数组实现堆。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-15
  • 1970-01-01
  • 2010-12-23
  • 2011-01-27
相关资源
最近更新 更多