鱼C论坛

 找回密码
 立即注册
查看: 1445|回复: 1

[已解决]malloc was not declared in this scope

[复制链接]
发表于 2019-12-13 11:52:49 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
红色的地方编译有错误,不知道哪里有问题
#include <stdio.h>
#define MAXSIZE 5
typedef int ElementType;
typedef int Position;
typedef struct LNode *List;
struct LNode{
        ElementType data[MAXSIZE];
        Position Last;/*保存数组Data[]中最后一个元素的位置*/
};


Position BinarySearch(List, ElementType);
int main()
{
        List L;
        L = (List)malloc(sizeof(struct LNode));
        L->data        = {12, 31, 55, 89, 101};
        L->Last = sizeof(L->data);
        ElementType X = 31;
        Position ret;
        ret = BinarySearch(L, X);
        printf("%d is %d", X, ret);
        return 0;
}

Position BinarySearch(List L, ElementType X)
{
        Position Left, Right, Mid;
       
        Left = 1;
        Right = L->Last;
        while(Left <= Right)
        {
                Mid = (Left + Right) / 2;
                if(L->data [Mid] > X)
                        Right = Mid - 1;
                else if(L->data [Mid] < X)
                        Left = Mid + 1;
                else
                        return Mid;
        }
}
最佳答案
2019-12-13 12:17:28
本帖最后由 superbe 于 2019-12-13 22:33 编辑

少头文件了吧#include <malloc.h>
BinarySearch的逻辑有点问题,修改的代码:

  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #define MAXSIZE 5
  4. typedef int ElementType;
  5. typedef int Position;
  6. typedef struct LNode *List;
  7. struct LNode {
  8.     ElementType data[MAXSIZE];
  9.     Position Last;
  10. };

  11. Position BinarySearch(List, ElementType);
  12. int main()
  13. {
  14.     List L;
  15.     L = (List)malloc(sizeof(struct LNode));
  16.     //L->data = { 12, 31, 55, 89, 101 };
  17.     L->data[0] = 12;
  18.     L->data[1] = 31;
  19.     L->data[2] = 55;
  20.     L->data[3] = 89;
  21.     L->data[4] = 101;

  22.     L->Last = sizeof(L->data) / sizeof(ElementType);
  23.     ElementType X = 31;
  24.     Position ret;
  25.     ret = BinarySearch(L, X);
  26.     (ret > 0) ? printf("%d is %d\n", X, ret) : printf("Not found\n");
  27.     return 0;
  28. }

  29. Position BinarySearch(List L, ElementType X)
  30. {
  31.     Position Left, Right, Mid;

  32.     Left = 1;
  33.     Right = L->Last;
  34.     while (Left <= Right)
  35.     {
  36.         Mid = (Left + Right) / 2;
  37.         if (L->data[Mid - 1] == X)
  38.             return Mid;
  39.         if (L->data[Mid - 1] > X)
  40.             Right = Mid - 1;
  41.         else if (L->data[Mid - 1] < X)
  42.             Left = Mid + 1;
  43.     }
  44.     return -1;
  45. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-12-13 12:17:28 From FishC Mobile | 显示全部楼层    本楼为最佳答案   
本帖最后由 superbe 于 2019-12-13 22:33 编辑

少头文件了吧#include <malloc.h>
BinarySearch的逻辑有点问题,修改的代码:

  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #define MAXSIZE 5
  4. typedef int ElementType;
  5. typedef int Position;
  6. typedef struct LNode *List;
  7. struct LNode {
  8.     ElementType data[MAXSIZE];
  9.     Position Last;
  10. };

  11. Position BinarySearch(List, ElementType);
  12. int main()
  13. {
  14.     List L;
  15.     L = (List)malloc(sizeof(struct LNode));
  16.     //L->data = { 12, 31, 55, 89, 101 };
  17.     L->data[0] = 12;
  18.     L->data[1] = 31;
  19.     L->data[2] = 55;
  20.     L->data[3] = 89;
  21.     L->data[4] = 101;

  22.     L->Last = sizeof(L->data) / sizeof(ElementType);
  23.     ElementType X = 31;
  24.     Position ret;
  25.     ret = BinarySearch(L, X);
  26.     (ret > 0) ? printf("%d is %d\n", X, ret) : printf("Not found\n");
  27.     return 0;
  28. }

  29. Position BinarySearch(List L, ElementType X)
  30. {
  31.     Position Left, Right, Mid;

  32.     Left = 1;
  33.     Right = L->Last;
  34.     while (Left <= Right)
  35.     {
  36.         Mid = (Left + Right) / 2;
  37.         if (L->data[Mid - 1] == X)
  38.             return Mid;
  39.         if (L->data[Mid - 1] > X)
  40.             Right = Mid - 1;
  41.         else if (L->data[Mid - 1] < X)
  42.             Left = Mid + 1;
  43.     }
  44.     return -1;
  45. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-25 07:41

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表