鱼C论坛

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

[已解决]线性表顺序存储

[复制链接]
发表于 2023-3-3 10:53:00 | 显示全部楼层 |阅读模式

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

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

x
为什么顺序表里面的数字不是0~9而是0~8?


  1. #include<stdio.h>
  2. #include<stdlib.h>

  3. typedef int ElemType;
  4. typedef struct seqList
  5. {
  6.         int n;
  7.         int maxLength;
  8.         ElemType *element;
  9. } SeqList;

  10. #define ERROR 0
  11. #define OK 1
  12. #define Overflow 2                //Overflow 表示上溢
  13. #define Underflow 3        //Underflow 表示下溢
  14. #define NotPresent 4        //Notpresent 表示元素不存在
  15. #define Duplicate 5        //Duplicate 表示有重复元素
  16. typedef int Status;
  17. Status Init(SeqList *L,int mSize)
  18. {
  19.         L->maxLength = mSize;
  20.         L->n = 0;
  21.         L->element = (ElemType*)malloc(sizeof(ElemType)*mSize);                //动态生成一维数组空间
  22.         if(!L->element)
  23.                 return ERROR;
  24.         return OK;
  25. }

  26. Status Find(SeqList L,int i,ElemType *x)
  27. {
  28.         if(i<0||i>L.n-1)
  29.                 return ERROR;        //判断元素下标i是否越界
  30.         *x = L.element[i];        //取出element[i]的值通过参数x返回
  31.         return OK;
  32. }

  33. Status Insert(SeqList *L,int i,ElemType x)
  34. {
  35.         int j;
  36.         if(i<-1||i>L->n-1)        //判断i下标是否越界
  37.                 return ERROR;
  38.         if(L->n==L->maxLength)        // 判断顺序表存储空间是否已满
  39.                 return ERROR;
  40.         for(j=L->n-1; j>i; j--)
  41.                 L->element[j+1]=L->element[j];        //从后往前逐个后移元素
  42.         L->element[i+1]=x;        //将新元素放入下标为i+1的位置
  43.         L->n=L->n+1;
  44.         return OK;
  45. }

  46. Status Delete(SeqList *L,int i)
  47. {
  48.         int j;
  49.         if(i<0||i>L->n-1)        //小标i是否越界
  50.                 return ERROR;
  51.         if(!L->n)        //顺序表是否为空
  52.                 return ERROR;
  53.         for(j=i+1; j<L->n; j++)
  54.                 L->element[j-1]=L->element[j];        //从前往后逐个前移元素
  55.         L->n--;        //表长减1
  56.         return OK;
  57. }

  58. Status Output(SeqList *L)
  59. {
  60.         int i;
  61.         if(L->n==0)
  62.                 return ERROR;
  63.         for(i=0; i<L->n-1; i++)
  64.                 printf("%d ",L->element[i]);
  65.         printf("\n");
  66.         return OK;
  67. }

  68. void Destroy(SeqList *L)
  69. {
  70.         L->n=0;
  71.         L->maxLength=0;
  72.         free(L->element);
  73. }

  74. int main()
  75. {
  76.         int i;
  77.         SeqList list;
  78.         Init(&list,10);        //初始化线性表
  79.         for(i=0; i<10; i++)
  80.                 Insert(&list,i-1,i);        //线性表中插入新元素
  81.         Output(&list);
  82.         Delete(&list,0);
  83.         Output(&list);
  84.         Destroy(&list);
  85.         return 0;
  86. }
复制代码
最佳答案
2023-3-3 23:03:00
不出所料这应该是自己写出来的代码。
首先猜猜你的想法:n, maxlength在使用时是重点。
  1. typedef struct seqList
  2. {
  3.         int n;                                // 已存元素数量
  4.         int maxLength;                // 能存元素的最大数量
  5.         ElemType *element;
  6. } SeqList;
复制代码


然后是:
Insert();函数,你这只是单单的实现的顺序存放,并没有真正的插入功能吧。头插, 中插,尾插。

随后我关键改动几处,自已对比的函数之间的差别。
  1. #include<stdio.h>
  2. #include<stdlib.h>

  3. typedef int ElemType;
  4. typedef struct seqList
  5. {
  6.         int n;                                // 已存元素数量
  7.         int maxLength;                // 能存元素的最大数量
  8.         ElemType *element;
  9. } SeqList;

  10. #define ERROR 0
  11. #define OK 1
  12. #define Overflow 2                //Overflow 表示上溢
  13. #define Underflow 3        //Underflow 表示下溢
  14. #define NotPresent 4        //Notpresent 表示元素不存在
  15. #define Duplicate 5        //Duplicate 表示有重复元素
  16. typedef int Status;
  17. Status Init(SeqList *L,int mSize)
  18. {
  19.         L->maxLength = mSize;
  20.         L->n = 0;
  21.         L->element = (ElemType*)malloc(sizeof(ElemType)*mSize);                //动态生成一维数组空间
  22.         if(!L->element)
  23.                 return ERROR;
  24.         return OK;
  25. }


  26. Status Insert(SeqList *L,int i, ElemType x)
  27. {

  28.         if(L->n==L->maxLength)        // 判断顺序表存储空间是否已满
  29.                 return ERROR;

  30.         L->element[i]=x;        
  31.                 L->n=L->n+1;
  32.         return OK;
  33. }


  34. Status Output(SeqList *L)
  35. {
  36.         int i;
  37.         if(L->n==0)
  38.                 return ERROR;
  39.         for(i=0; i<L->n; i++)
  40.                 printf("%d ",L->element[i]);
  41.         printf("\n");
  42.         return OK;
  43. }


  44. int main()
  45. {
  46.         int i;
  47.         SeqList list;
  48.         Init(&list,10);        //初始化线性表
  49.         for(i=0; i<10; i++)
  50.                 Insert(&list,i,i);        //线性表中插入新元素
  51.                 printf("%d\n", list.n);
  52.         Output(&list);

  53.         return 0;
  54. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-3-3 23:03:00 | 显示全部楼层    本楼为最佳答案   
不出所料这应该是自己写出来的代码。
首先猜猜你的想法:n, maxlength在使用时是重点。
  1. typedef struct seqList
  2. {
  3.         int n;                                // 已存元素数量
  4.         int maxLength;                // 能存元素的最大数量
  5.         ElemType *element;
  6. } SeqList;
复制代码


然后是:
Insert();函数,你这只是单单的实现的顺序存放,并没有真正的插入功能吧。头插, 中插,尾插。

随后我关键改动几处,自已对比的函数之间的差别。
  1. #include<stdio.h>
  2. #include<stdlib.h>

  3. typedef int ElemType;
  4. typedef struct seqList
  5. {
  6.         int n;                                // 已存元素数量
  7.         int maxLength;                // 能存元素的最大数量
  8.         ElemType *element;
  9. } SeqList;

  10. #define ERROR 0
  11. #define OK 1
  12. #define Overflow 2                //Overflow 表示上溢
  13. #define Underflow 3        //Underflow 表示下溢
  14. #define NotPresent 4        //Notpresent 表示元素不存在
  15. #define Duplicate 5        //Duplicate 表示有重复元素
  16. typedef int Status;
  17. Status Init(SeqList *L,int mSize)
  18. {
  19.         L->maxLength = mSize;
  20.         L->n = 0;
  21.         L->element = (ElemType*)malloc(sizeof(ElemType)*mSize);                //动态生成一维数组空间
  22.         if(!L->element)
  23.                 return ERROR;
  24.         return OK;
  25. }


  26. Status Insert(SeqList *L,int i, ElemType x)
  27. {

  28.         if(L->n==L->maxLength)        // 判断顺序表存储空间是否已满
  29.                 return ERROR;

  30.         L->element[i]=x;        
  31.                 L->n=L->n+1;
  32.         return OK;
  33. }


  34. Status Output(SeqList *L)
  35. {
  36.         int i;
  37.         if(L->n==0)
  38.                 return ERROR;
  39.         for(i=0; i<L->n; i++)
  40.                 printf("%d ",L->element[i]);
  41.         printf("\n");
  42.         return OK;
  43. }


  44. int main()
  45. {
  46.         int i;
  47.         SeqList list;
  48.         Init(&list,10);        //初始化线性表
  49.         for(i=0; i<10; i++)
  50.                 Insert(&list,i,i);        //线性表中插入新元素
  51.                 printf("%d\n", list.n);
  52.         Output(&list);

  53.         return 0;
  54. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-23 03:53

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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