【问题标题】:calling function inside another function (but not recursion)在另一个函数中调用函数(但不是递归)
【发布时间】:2017-05-22 16:55:14
【问题描述】:

我在 C 中编写了合并排序的代码,但是当我在 mergesort() 中调用函数 merge() 时,编译器会显示错误。有什么原因吗? 我正在尝试编写归并排序算法 对于堆栈溢出的该死情况,我已经没有更多的细节了。

            #include<stdio.h>
            #include<stdlib.h>
            int mergesort(int* A,int n)
            {
                if(n==1)
                   return 0;
                int ln,rn;//to calculate length of left and right arrays
                int *L,*R;//pointer to left and right arrays
                L=(int*)malloc(ln*sizeof(int));
                R=(int*)malloc(rn*sizeof(int));
                ln=n/2;
                rn=n-ln;
                for(int i=0;i<ln;i++)
                {
                    L[i]=A[i];
                }   
                for(int i=0;i<rn;i++)
                {
                    L[i]=A[i+rn];
                }
                mergesort(L,ln);
                mergesort(R,rn);
                merge(A,L,R,n,ln,rn);
                return 0;
            }
            int merge(int*A,int*L,int*R,int n,int ln,int rn)
            {
                int i,j,k;//to access elements of A,L,R respectively
                i=j=k=0;
                while(j<ln&&k<rn)
                {
                    if(L[j]<=R[k])
                    {
                        A[i]=L[j];
                        i++;
                        j++;
                    }
                    else
                    {
                        A[i]=L[k];
                        i++;
                        k++;    
                    }
                }
                while(j<ln)//means R is fully copied to A,but not L
                {
                        A[i]=L[j];
                        i++;
                        j++;    
                }
                while(k<ln)//means L is fully copied to A,but not R
                {
                        A[i]=L[k];
                        i++;
                        k++;    
                }
                return 0;
            }
            int main()
            {
                int* A,i,n;
                //scanf("array size: %d",&n);//dunno it's valid or not
                scanf("%d",&n);
                A=(int*)malloc(n*sizeof(int));
                printf("\n");
                for(i=0;i<n;i++)
                {
                    scanf("%d",&A[i]);
                }
                mergesort(A,n);
                for(i=0;i<n;i++)
                {
                    printf("%d ",A[i]);
                }
                return 0;
            }

【问题讨论】:

  • 你需要转发声明你的合并函数。或者把merge的定义放在归并排序之前。
  • 要么将 merge() 移到 mergesort() 之上,要么在 mergesort() 之上添加 merge() 声明。

标签: c mergesort


【解决方案1】:

merge 函数在 mergesort 的范围内仍然未知,因为它只出现在文件中。

在文件开头(包含后)添加原型(前向声明):

static int merge(int*A,int*L,int*R,int n,int ln,int rn);

【讨论】:

    【解决方案2】:

    您需要对 merge() 函数进行原型设计,或者在 mergeSort() 之前定义 merge()

    这是你需要在 mergeSort() 函数之前添加的内容:

    int merge(int*,int*,int*,int,int,int);
    

    在原型定义中,参数不需要变量名,只需要参数定义。

    【讨论】:

      【解决方案3】:

      在 C 中,函数头的顺序很重要。

      只需将 merge() 放在 mergesort() 之上。

      或者尝试在顶部声明merge()函数头在in​​cludes之后添加这一行

      int merge(int*A,int*L,int*R,int n,int ln,int rn) ;

      【讨论】:

        猜你喜欢
        • 2015-07-09
        • 2021-11-29
        • 2022-01-03
        • 1970-01-01
        • 1970-01-01
        • 2017-11-26
        • 1970-01-01
        • 2021-01-20
        • 1970-01-01
        相关资源
        最近更新 更多