• 6-2 顺序表操作集分数 20


    本题要求实现顺序表的操作集。

    函数接口定义:

    1. List MakeEmpty();
    2. Position Find( List L, ElementType X );
    3. bool Insert( List L, ElementType X, Position P );
    4. bool Delete( List L, Position P );

    其中List结构定义如下:

    1. typedef int Position;
    2. typedef struct LNode *List;
    3. struct LNode {
    4. ElementType Data[MAXSIZE];
    5. Position Last; /* 保存线性表中最后一个元素的位置 */
    6. };

    各个操作函数的定义为:

    List MakeEmpty():创建并返回一个空的线性表;

    Position Find( List L, ElementType X ):返回线性表中X的位置。若找不到则返回ERROR;

    bool Insert( List L, ElementType X, Position P ):将X插入在位置P并返回true。若空间已满,则打印“FULL”并返回false;如果参数P指向非法位置,则打印“ILLEGAL POSITION”并返回false;

    bool Delete( List L, Position P ):将位置P的元素删除并返回true。若参数P指向非法位置,则打印“POSITION P EMPTY”(其中P是参数值)并返回false。

    裁判测试程序样例:

    1. #include
    2. #include
    3. #define MAXSIZE 5
    4. #define ERROR -1
    5. typedef enum {false, true} bool;
    6. typedef int ElementType;
    7. typedef int Position;
    8. typedef struct LNode *List;
    9. struct LNode {
    10. ElementType Data[MAXSIZE];
    11. Position Last; /* 保存线性表中最后一个元素的位置 */
    12. };
    13. List MakeEmpty();
    14. Position Find( List L, ElementType X );
    15. bool Insert( List L, ElementType X, Position P );
    16. bool Delete( List L, Position P );
    17. int main()
    18. {
    19. List L;
    20. ElementType X;
    21. Position P;
    22. int N;
    23. L = MakeEmpty();
    24. scanf("%d", &N);
    25. while ( N-- ) {
    26. scanf("%d", &X);
    27. if ( Insert(L, X, 0)==false )
    28. printf(" Insertion Error: %d is not in.\n", X);
    29. }
    30. scanf("%d", &N);
    31. while ( N-- ) {
    32. scanf("%d", &X);
    33. P = Find(L, X);
    34. if ( P == ERROR )
    35. printf("Finding Error: %d is not in.\n", X);
    36. else
    37. printf("%d is at position %d.\n", X, P);
    38. }
    39. scanf("%d", &N);
    40. while ( N-- ) {
    41. scanf("%d", &P);
    42. if ( Delete(L, P)==false )
    43. printf(" Deletion Error.\n");
    44. if ( Insert(L, 0, P)==false )
    45. printf(" Insertion Error: 0 is not in.\n");
    46. }
    47. return 0;
    48. }
    49. /* 你的代码将被嵌在这里 */

    输入样例:

    1. 6
    2. 1 2 3 4 5 6
    3. 3
    4. 6 5 1
    5. 2
    6. -1 6

    输出样例:

    1. FULL Insertion Error: 6 is not in.
    2. Finding Error: 6 is not in.
    3. 5 is at position 0.
    4. 1 is at position 4.
    5. POSITION -1 EMPTY Deletion Error.
    6. FULL Insertion Error: 0 is not in.
    7. POSITION 6 EMPTY Deletion Error.
    8. FULL Insertion Error: 0 is not in.

    代码如下:

    1. List MakeEmpty() {
    2. List temp = (List)malloc(sizeof(struct LNode));
    3. temp->Last = -1;
    4. return temp;
    5. }
    6. Position Find( List L, ElementType X ) {
    7. int i;
    8. for (i = 0; i <= L->Last; i ++) {
    9. if (L->Data[i] == X) {
    10. return i;
    11. }
    12. }
    13. return ERROR;
    14. }
    15. bool Insert( List L, ElementType X, Position P ) {
    16. int i;
    17. if (L->Last >= MAXSIZE - 1) {
    18. printf("FULL");
    19. return false;
    20. }
    21. if (P < 0 || P > L->Last + 1) {
    22. printf("ILLEGAL POSITION");
    23. return false;
    24. }
    25. L->Last ++;
    26. for (i = L->Last; i > P; i --) {
    27. L->Data[i] = L->Data[i - 1];
    28. }
    29. L->Data[P] = X;
    30. return true;
    31. }
    32. bool Delete( List L, Position P ) {
    33. int i;
    34. if (P < 0 || P > L->Last) {
    35. printf("POSITION %d EMPTY", P);
    36. return false;
    37. }
    38. for (i = P; i < L->Last; i ++) {
    39. L->Data[i] = L->Data[i + 1];
    40. }
    41. L->Last --;
    42. return true;
    43. }

    注意一下输出的格式,我在一个输出里多了一个换行,看了半天

  • 相关阅读:
    adb shell命令
    【Ubuntu-MySQL8安装与主从复制】
    leetcode(力扣) 221. 最大正方形(动态规划)
    【Android知识笔记】UI体系(四)
    索引!索引!!索引!!!到底什么是索引?
    java基础 API Calendar类
    使用集成开发环境来开发Go项目
    Qt学习总结之QComboBox
    vscode中Emmet语法的使用
    Transformer的上下文学习能力
  • 原文地址:https://blog.csdn.net/m0_65406049/article/details/126137238