習題3.13 雙端隊列(25 分)浙大版《數據結構(第2版)》題目集


雙端隊列(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;

注意:PushInject應該在正常執行完操作后返回true,或者在出現非正常情況時返回false。當FrontRear相等時隊列為空,PopEject必須返回由裁判程序定義的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;
}

/* 你的代碼將被嵌在這里 */

輸入樣例:

3
Pop
Inject 1
Pop
Eject
Push 2
Push 3
Eject
Inject 4
Inject 5
Inject 6
Push 7
Pop
End

輸出樣例:

Deque is Empty!
1 is out
Deque is Empty!
2 is out
Deque is Full!
Deque is Full!
3 is out
Inside Deque: 4 5


樣例沒啥用,編譯器編譯過不了所以就干寫了四個函數,要注意尾指針需要減一。

代碼:

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM