1. 程式人生 > >6-2 雙端佇列 (25 分)

6-2 雙端佇列 (25 分)

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

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

輸入樣例:

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+1)%(D->MaxSize)==D->Front)
		return false;
	D->Front--;
	D->Front=(D->MaxSize+D->Front)%(D->MaxSize);
	D->Data[D->Front]=X;
	return true;
} 
ElementType Pop( Deque D )
{
	if(D->Front==D->Rear)
		return ERROR;
	ElementType d=D->Data[D->Front];
	D->Front++;
	D->Front=D->Front%(D->MaxSize);
	return d;
} 
bool Inject( ElementType X, Deque D )
{
	if((D->Rear+1)%(D->MaxSize)==D->Front)
		return false;
	D->Data[D->Rear]=X;
	D->Rear=(D->Rear+1)%D->MaxSize;
	return true;
} 
ElementType Eject( Deque D )
{
	if(D->Front==D->Rear)
		return ERROR;
	if(!D->Rear)
		D->Rear=D->MaxSize;
	D->Rear--;
	ElementType d=D->Data[D->Rear];
	return d;
}