6-2シーケンステーブル操作セット

7426 ワード

https://pintia.cn/problem-sets/15/problems/725
この問題では、シーケンステーブルの操作セットを実装する必要があります.
関数インタフェースの定義:
List MakeEmpty(); 
Position Find( List L, ElementType X );
bool Insert( List L, ElementType X, Position P );
bool Delete( List L, Position P );

ここで、List構造は以下のように定義される.
typedef int Position;
typedef struct LNode *List;
struct LNode {
    ElementType Data[MAXSIZE];
    Position Last; /*                 */
};

各アクション関数は次のように定義されます.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に戻ります.
審判試験プログラムのサンプル:
#include 
#include 

#define MAXSIZE 5
#define ERROR -1
typedef enum {false, true} bool;
typedef int ElementType;
typedef int Position;
typedef struct LNode *List;
struct LNode {
    ElementType Data[MAXSIZE];
    Position Last; /*                 */
};

List MakeEmpty(); 
Position Find( List L, ElementType X );
bool Insert( List L, ElementType X, Position P );
bool Delete( List L, Position P );

int main()
{
    List L;
    ElementType X;
    Position P;
    int N;

    L = MakeEmpty();
    scanf("%d", &N);
    while ( N-- ) {
        scanf("%d", &X);
        if ( Insert(L, X, 0)==false )
            printf(" Insertion Error: %d is not in.
", X); } scanf("%d", &N); while ( N-- ) { scanf("%d", &X); P = Find(L, X); if ( P == ERROR ) printf("Finding Error: %d is not in.
", X); else printf("%d is at position %d.
", X, P); } scanf("%d", &N); while ( N-- ) { scanf("%d", &P); if ( Delete(L, P)==false ) printf(" Deletion Error.
"); if ( Insert(L, 0, P)==false ) printf(" Insertion Error: 0 is not in.
"); } return 0; } /* */

サンプルを入力:
6
1 2 3 4 5 6
3
6 5 1
2
-1 6

出力サンプル:
FULL Insertion Error: 6 is not in.
Finding Error: 6 is not in.
5 is at position 0.
1 is at position 4.
POSITION -1 EMPTY Deletion Error.
FULL Insertion Error: 0 is not in.
POSITION 6 EMPTY Deletion Error.
FULL Insertion Error: 0 is not in.

提交代码
List MakeEmpty()
{
    List tmp = (List)malloc(sizeof(struct LNode));
    tmp->Last = -1;
    return tmp;
}

Position Find( List L, ElementType X )
{
    for(int i=0;i<=L->Last;i++)
    {
        if(L->Data[i] == X)
            return i;        
    }
    return ERROR;
}

bool Insert( List L, ElementType X, Position P )
{
    if(L->Last+1 == MAXSIZE)
    {
        printf("FULL");
        return false;
    }
    else if(P<0 || P>L->Last+1)
    {
        printf("ILLEGAL POSITION");
        return false;
    }
    else
    {
        for(int i=L->Last+1;i>P;i--)
        {
            L->Data[i] = L->Data[i-1];
        }
        L->Data[P] = X;
        L->Last++;
    }
    return true;
}

bool Delete( List L, Position P )
{
    if(P<0 || P>L->Last)
    {
        printf("POSITION %d EMPTY",P);
        return false;
    }
    else
    {
        for(int i=P;iLast;i++)
        {
            L->Data[i] = L->Data[i+1];
        }
        L->Last--;
    }
    return true;
}