3-1 Deque

A “deque” is a data structure consisting of a list of items, on which the following operations are possible:

  1. Push(X,D): Insert item X on the front end of deque D.
  2. Pop(D): Remove the front item from deque D and return it.
  3. Inject(X,D): Insert item X on the rear end of deque D.
  4. Eject(D): Remove the rear item from deque D and return it.
    Write routines to support the deque that take O(1) time per operation.
    简单来说实现五个函数创建、头部出入、尾部出入。
    注意为双向链表
    Format of functions:
Deque CreateDeque();
int Push( ElementType X, Deque D );
ElementType Pop( Deque D );
int Inject( ElementType X, Deque D );
ElementType Eject( Deque D );

结构体定义

typedef struct Node *PtrToNode;
struct Node {
    ElementType Element;
    PtrToNode Next, Last;
};
typedef struct DequeRecord *Deque;
struct DequeRecord {
    PtrToNode Front, Rear;
};

Here the deque is implemented by a doubly linked list with a header. Front and Rear point to the two ends of the deque respectively. Front always points to the header. The deque is empty when Front and Rear both point to the same dummy header. Note: Push and Inject are supposed to return 1 if the operations can be done successfully, or 0 if fail. If the deque is empty, Pop and Eject must return ERROR which is defined by the judge program.
裁判程序(函数已补充完整)

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

#define ElementType int
#define ERROR 1e5
typedef enum { push, pop, inject, eject, end } Operation;

typedef struct Node *PtrToNode;
struct Node {
    ElementType Element;
    PtrToNode Next, Last;
};
typedef struct DequeRecord *Deque;
struct DequeRecord {
    PtrToNode Front, Rear;
};
Deque CreateDeque();
int Push( ElementType X, Deque D );
ElementType Pop( Deque D );
int Inject( ElementType X, Deque D );
ElementType Eject( Deque D );

void PrintDeque( Deque D )
{
    if(D -> Front == D -> Rear)
    {
        printf("Deque is Empty!\n");
        return 0;
    }
    printf("Inside Deque:");
    PtrToNode p = D -> Front -> Next;
    while(p!=NULL)
    {
        printf(" %d",p -> Element);
        p = p -> Next;
    }
}
Operation GetOp()
{
    char s[10];
    scanf("%s",s);
    if(!strcmp(s,"Pop"))return pop;
    else if(!strcmp(s,"Push"))return push;
    else if(!strcmp(s,"Inject"))return inject;
    else if(!strcmp(s,"Eject"))return eject;
    else return end;
}
int main()
{
    ElementType X;
    Deque D;
    int done = 0;

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

接口

Deque CreateDeque()
{
    PtrToNode p=(PtrToNode)malloc(sizeof(struct Node));
    Deque que=(Deque)malloc(sizeof(struct DequeRecord));
    p->Next=p->Last=NULL;
    que->Front=que->Rear=p;
    return que;
}
int Push( ElementType X, Deque D )
{
    PtrToNode p;
    p=(PtrToNode)malloc(sizeof(struct Node));
    p->Element=X;
    if(D->Front==D->Rear)//注意为空时操作不同
    {
        D->Front->Next=p;
        p->Last=D->Front;
        p->Next=NULL;
        D->Rear=p;
    }
    else
    {
    p->Next=D->Front->Next;
    p->Next->Last=p;//注意指回去
    D->Front->Next=p;
    p->Last=D->Front;
    }
    return 1;
}
ElementType Pop( Deque D )
{
    ElementType n;
    if(D->Front==D->Rear)
    {
        return ERROR;
    }
    PtrToNode p=D->Front->Next;
    n=p->Element;
    D->Front->Next=p->Next;
    if(p->Next)
    {
        p->Next->Last=D->Front;
    }
    free(p);
    if(D->Front->Next==NULL)
    {
        D->Rear=D->Front;
    }
    return n;
}
int Inject( ElementType X, Deque D )
{
    PtrToNode p=(PtrToNode)malloc(sizeof(struct Node));
    p->Element=X;
    D->Rear->Next=p;
    p->Last=D->Rear;
    p->Next=NULL;
    D->Rear=p;
    return 1;
}
ElementType Eject( Deque D )
{
    PtrToNode p=D->Rear;
    ElementType n;
    if(D->Front==D->Rear)
    {
        return ERROR;
    }
    n=p->Element;
    D->Rear=p->Last;
    D->Rear->Next=NULL;
    free(p);
    return n;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值