PTA 雙端佇列 資料結構

qy流火發表於2020-10-19

雙端佇列(deque,即double-ended queue的縮寫)是一種具有佇列和棧性質的資料結構,即可以(也只能)線上性表的兩端進行插入和刪除。若以順序儲存方式實現雙端佇列,請編寫例程實現下列操作:

Push(X,D):將元素X插入到雙端佇列D的頭;
Pop(D):刪除雙端佇列D的頭元素,並返回;
Inject(X,D):將元素X插入到雙端佇列D的尾部;
Eject(D):刪除雙端佇列D的尾部元素,並返回。

函式介面定義:

bool Push( ElementType X, Deque D );
ElementType Pop( Deque D );
bool Inject( ElementType X, Deque D );
ElementType Eject( Deque D );

其中Deque結構定義如下:

typedef int Position;
typedef struct QNode *PtrToQNode;
struct QNode {
    ElementType *Data;      /* 儲存元素的陣列   */
    Position Front, Rear;   /* 佇列的頭、尾指標 */
    int MaxSize;            /* 佇列最大容量     */
};
typedef PtrToQNode Deque; 

注意:Push和Inject應該在正常執行完操作後返回true,或者在出現非正常情況時返回false。當Front和Rear相等時佇列為空,Pop和Eject必須返回由裁判程式定義的ERROR。
裁判程式

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

#define ERROR -1
typedef int ElementType;
typedef enum { push, pop, inject, eject, end } Operation;
typedef enum { false, true } bool;
typedef int Position;
typedef struct QNode *PtrToQNode;
struct QNode {
    ElementType *Data;      /* 儲存元素的陣列   */
    Position Front, Rear;   /* 佇列的頭、尾指標 */
    int MaxSize;            /* 佇列最大容量     */
};
typedef PtrToQNode Deque; 

Deque CreateDeque( int MaxSize )
{   /* 注意:為區分空佇列和滿佇列,需要多開闢一個空間 */
    Deque D = (Deque)malloc(sizeof(struct QNode));
    MaxSize++;
    D->Data = (ElementType *)malloc(MaxSize * sizeof(ElementType));
    D->Front = D->Rear = 0;
    D->MaxSize = MaxSize;
    return D;
}

bool Push( ElementType X, Deque D );
ElementType Pop( Deque D );
bool Inject( ElementType X, Deque D );
ElementType Eject( Deque D );

Operation GetOp();          /* 裁判實現,細節不表 */
void PrintDeque( Deque D ); /* 裁判實現,細節不表 */

int main()
{
    ElementType X;
    Deque D;
    int N, done = 0;

    scanf("%d", &N);
    D = CreateDeque(N);
    while (!done) {
        switch(GetOp()) {
        case push: 
            scanf("%d", &X);
            if (!Push(X, D)) printf("Deque is Full!\n");
            break;
        case pop:
            X = Pop(D);
            if ( X==ERROR ) printf("Deque is Empty!\n");
            else printf("%d is out\n", X);
            break;
        case inject: 
            scanf("%d", &X);
            if (!Inject(X, D)) printf("Deque is Full!\n");
            break;
        case eject:
            X = Eject(D);
            if ( X==ERROR ) printf("Deque is Empty!\n");
            else printf("%d is out\n", X);
            break;
        case end:
            PrintDeque(D);
            done = 1;
            break;
        }
    }
    return 0;
}

/* 你的程式碼將被嵌在這裡 */

解題思路:
push和pop屬於在頭部進行操作
inject和eject屬於在尾部進行操作
push和inject需要考慮佇列是否滿了
(D->Front - D->Rear + D->MaxSize) % D->MaxSize == 1
然後注意因為相當於迴圈佇列所以head和rear插元素時要注意移動方式
pop和eject需要考慮是否為空,這比較簡單直接判斷收尾指標是否相等即可
另一方面注意讀題:為區分空佇列和滿佇列,需要多開闢一個空間
個人認為難點在於位置的移動與情況的判斷
附上程式:

bool Push(ElementType X, Deque D)
{
    if ((D->Front - D->Rear + D->MaxSize) % D->MaxSize == 1)
    {
        return false;
    }
    D->Front = (D->Front - 1 + D->MaxSize) % D->MaxSize;
    D->Data[D->Front] = X;
    return true;
}
ElementType Pop(Deque D)
{
    int num;
    if (D->Front == D->Rear)
    {
        return ERROR;
    }
    num = D->Data[D->Front];
    D->Front = (D->Front + 1) % D->MaxSize;
    return num;
}
bool Inject(ElementType X, Deque D)
{
    if ((D->Front - D->Rear + D->MaxSize) % D->MaxSize == 1)
    {
        return false;
    }
    D->Data[D->Rear] = X;
    D->Rear = (D->Rear + 1) % D->MaxSize;
    return true;
}
ElementType Eject(Deque D)
{
    int num;
    if (D->Front == D->Rear)
    {
        return ERROR;
    }
    num = D->Data[(D->Rear - 1 + D->MaxSize) % D->MaxSize];
    D->Rear = (D->Rear - 1 + D->MaxSize) % D->MaxSize;
    return num;
}

相關文章