3-4 双端队列 (20 分)

本文介绍了如何使用顺序存储方式实现双端队列,包括Push、Pop、Inject和Eject四个操作。在处理过程中需要注意数组下标的特殊处理,如Push和Inject操作中队头和队尾指针的移动。文章还提到了在实现过程中遇到的问题,如队列满和空的判断以及自定义输出函数可能带来的影响。
摘要由CSDN通过智能技术生成

3-4 双端队列 (20 分)
双端队列(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
这道题是顺序存储的双向队列,和那个链式存储的相比我感觉要难了,因为那个直接移动指针就好,但是这个数组下标有些混,要注意的是这道题采用的方式是队尾指向一直是空的,而以前正常的都是队头指向为空,这点要注意,因为一开始队尾和队头都是指向零的,所以一定要有一个指针是先动再放,我们用了队头,先让队头减1,再放元素,所以队头指向的都是有元素的,而队尾是先放再加1,所以队尾指向都是空,最后取出的时候也要注意。

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.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);
bool Push(ElementType X,Deque D)
{
    if((D->Rear+1)%D->MaxSize==D->Front)
        return false;
   D->Front=((D->Front-1)+D->MaxSize)%D->MaxSize;//循环数组,从front入的时候是先减再入
   D->Data[D->Front]=X;
   return true;
}
ElementType Pop(Deque D);
ElementType Pop(Deque D)
{
    if(D->Rear==D->Front)
        return ERROR;
     ElementType a=D->Data[D->Front];
     D->Front=(D->Front+1)%D->MaxSize;
     return a;
}
bool Inject(ElementType X,Deque 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);
ElementType Eject(Deque D)
{
    if(D->Front==D->Rear)
        return ERROR;
    else
    {
                D->Rear=(D->Rear-1+D->MaxSize)%D->MaxSize;
                return D->Data[D->Rear];
    }

}
Operation  GetOp();
Operation  GetOp()
{
    const char*ope[]={"push","pop","inject","eject","end"};
    char a[10];
    scanf("%s",a);
    if(isupper(a[0]))
        a[0]=a[0]+32;
        Operation i;
    for( i=push;i<=end;i++)
    {
        if(!strcmp(a,ope[i]))
            return i;
    }
    return ERROR;
}
void PrintDeque(Deque D);
void PrintDeque(Deque D)
{
    printf("Inside Deque:");
    while(D->Front!=D->Rear)
    {
        printf("%d ",D->Data[D->Front]);
        D->Front=(D->Front+1+D->MaxSize)%D->MaxSize;
    }
}
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;
}

我用rear指数据,front指空的时候明明输出是对的,但是测试却不对,不知道为啥。
ok,知道了,因为这个输出函数是我自己写的,但是它有自己的输出函数,顺序不对就都不对了
拜拜喽,明天好运哦

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值