鱼C论坛

 找回密码
 立即注册
查看: 3350|回复: 3

[已解决]顺序表

[复制链接]
发表于 2016-4-11 14:42:18 | 显示全部楼层 |阅读模式

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

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

x
我根据书本打的,为什么是错的#include <stdio.h>
typedef int datatype;
#define maxsize 1024
void main()
{
typedef struct
        {
        datatype data[maxsize];
        int last;
        }sequenlist;
}
int INSERT(L,x,i);
sequenlist *L;
int i;
{int j;
if(((*L).last)>=maxsize-1)
        {print("overflow");return NULL;}
        else
        if((i<1)||((*L).last)+1)
        {print("error");return null;}
else
{for(j=(*L).last;j>=i-1;j--)
(*L).data[j+1]=(*L).data[j];
(*L).data[i-1]=x;
(*L).last=(*L).last+1;
}
return(1);
}
int DELETE(L,i)
sequenlist *L;
int i;
{
int j;
if((i<1)||(i>(*L).last+1))
{printf("error");return null;}
else
        {
        for(j=i;j<=(*L).last;j++)
                (*L).data[j-1]=(*L).data[j];
        (*L).last--;
        }
return(1);
}

最佳答案
2023-11-30 08:42:21
首先,函数的定义应该在main函数的外部,而不是在内部。

其次,在函数的参数列表中,变量的类型应该在变量名之前,而不是在后面。

另外,函数体内的打印语句应该使用printf函数而不是print函数。

最后,当返回值为整数时,应该使用关键字"return"而不是"return null"。

参考代码:

  1. #include <stdio.h>

  2. typedef int datatype;
  3. #define maxsize 1024

  4. typedef struct
  5. {
  6.     datatype data[maxsize];
  7.     int last;
  8. } sequenlist;

  9. int INSERT(sequenlist *L, datatype x, int i)
  10. {
  11.     int j;
  12.     if (L->last >= maxsize - 1)
  13.     {
  14.         printf("overflow");
  15.         return 0;
  16.     }
  17.     else if (i < 1 || i > L->last + 1)
  18.     {
  19.         printf("error");
  20.         return 0;
  21.     }
  22.     else
  23.     {
  24.         for (j = L->last; j >= i - 1; j--)
  25.         {
  26.             L->data[j + 1] = L->data[j];
  27.         }
  28.         L->data[i - 1] = x;
  29.         L->last = L->last + 1;
  30.     }
  31.     return 1;
  32. }

  33. int DELETE(sequenlist *L, int i)
  34. {
  35.     int j;
  36.     if (i < 1 || i > L->last + 1)
  37.     {
  38.         printf("error");
  39.         return 0;
  40.     }
  41.     else
  42.     {
  43.         for (j = i; j <= L->last; j++)
  44.         {
  45.             L->data[j - 1] = L->data[j];
  46.         }
  47.         L->last--;
  48.     }
  49.     return 1;
  50. }

  51. int main()
  52. {
  53.     sequenlist L;
  54.     // 其他操作...
  55.     return 0;
  56. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-4-11 19:35:00 From FishC Mobile | 显示全部楼层
首先你打的。。顺序完全不对对sequenlist结构应放在main函数外面然后main函数放最下面或者是在main函数前面放定义
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-4-11 19:35:40 | 显示全部楼层
首先你打的。。顺序完全不对对sequenlist结构应放在main函数外面然后main函数放最下面或者是在main函数前面放定义
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-11-30 08:42:21 | 显示全部楼层    本楼为最佳答案   
首先,函数的定义应该在main函数的外部,而不是在内部。

其次,在函数的参数列表中,变量的类型应该在变量名之前,而不是在后面。

另外,函数体内的打印语句应该使用printf函数而不是print函数。

最后,当返回值为整数时,应该使用关键字"return"而不是"return null"。

参考代码:

  1. #include <stdio.h>

  2. typedef int datatype;
  3. #define maxsize 1024

  4. typedef struct
  5. {
  6.     datatype data[maxsize];
  7.     int last;
  8. } sequenlist;

  9. int INSERT(sequenlist *L, datatype x, int i)
  10. {
  11.     int j;
  12.     if (L->last >= maxsize - 1)
  13.     {
  14.         printf("overflow");
  15.         return 0;
  16.     }
  17.     else if (i < 1 || i > L->last + 1)
  18.     {
  19.         printf("error");
  20.         return 0;
  21.     }
  22.     else
  23.     {
  24.         for (j = L->last; j >= i - 1; j--)
  25.         {
  26.             L->data[j + 1] = L->data[j];
  27.         }
  28.         L->data[i - 1] = x;
  29.         L->last = L->last + 1;
  30.     }
  31.     return 1;
  32. }

  33. int DELETE(sequenlist *L, int i)
  34. {
  35.     int j;
  36.     if (i < 1 || i > L->last + 1)
  37.     {
  38.         printf("error");
  39.         return 0;
  40.     }
  41.     else
  42.     {
  43.         for (j = i; j <= L->last; j++)
  44.         {
  45.             L->data[j - 1] = L->data[j];
  46.         }
  47.         L->last--;
  48.     }
  49.     return 1;
  50. }

  51. int main()
  52. {
  53.     sequenlist L;
  54.     // 其他操作...
  55.     return 0;
  56. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-28 22:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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