PTA--双向链表模拟Deque

1 篇文章 0 订阅

//最重要的是细心

#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#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()
{
    Deque q=(Deque)malloc(sizeof(struct DequeRecord));
    q->Front=(PtrToNode)malloc(sizeof(struct Node));
    q->Front->Last=NULL;
    q->Rear=q->Front;//初始状态头和尾指向同一个位置
    q->Rear->Next=NULL;
    return q;
}
//Insert item X on the front end of deque D.
int Push( ElementType X, Deque D )
{
    struct Node* tmp=(struct Node *)malloc(sizeof(struct Node));
    tmp->Element=X;
    if(D->Front==D->Rear)
    {//第一个元素,特殊处理,front->元素(更新为rear)
        D->Front->Next=tmp;
        tmp->Last=D->Front;
        D->Rear=tmp;
        tmp->Next=NULL;
        return 1;
    }
    //front->元素->...->元素(新的rear)
    tmp->Next=D->Front->Next;
    D->Front->Next->Last=tmp;
    D->Front->Next=tmp;
    tmp->Last=D->Front;
    return 1;
}
//Remove the front item from deque D and return it.
ElementType Pop( Deque D )
{
    if(D->Front==D->Rear) return ERROR;
    int tmp=D->Front->Next->Element;
    struct Node* pre=D->Front->Next;
    if(D->Front->Next==D->Rear)
    {//一个元素的特殊情况
        D->Rear=D->Front;
        D->Rear->Next=NULL;
        free(pre);
        return tmp;
    }
    D->Front->Next->Next->Last=D->Front;
    D->Front->Next=D->Front->Next->Next;
    free(pre);
    return tmp;
}
// Insert item X on the rear end of deque D.
int Inject( ElementType X, Deque D )
{
    struct Node* tmp=(struct Node *)malloc(sizeof(struct Node));
    tmp->Element=X;
    if(D->Rear==D->Front)
    {//第一个元素,特殊处理,front->元素(更新为rear)
        D->Front->Next=tmp;
        tmp->Last=D->Front;
        D->Rear=tmp;
        tmp->Next=NULL;
        return 1;
    }
    tmp->Last=D->Rear;
    D->Rear->Next=tmp;
    tmp->Next=NULL;
    D->Rear=tmp;
    return 1;
}
//Remove the rear item from deque D and return it. Write routines to support the deque that take O(1) time per operation.
ElementType Eject( Deque D )
{
    if(D->Front==D->Rear) return ERROR;
    int tmp=D->Rear->Element;
    struct Node* pre=D->Rear;
    D->Rear=D->Rear->Last;
    D->Rear->Next=NULL;
    free(pre);
    return tmp;
}
Operation GetOp()
{
    char op[11];
    scanf("%s",op);
    if(strcmp(op,"Push")==0) return push;
    if(strcmp(op,"Pop")==0) return pop;
    if(strcmp(op,"Inject")==0) return inject;
    if(strcmp(op,"Eject")==0) return eject;
    return end;
}/* details omitted */
void PrintDeque( Deque D )
{
    struct Node* tmp=D->Front->Next;
    printf("Inside Deque: ");
    if(tmp) printf("%d",tmp->Element),tmp=tmp->Next;
    else return;
    while(tmp)
    {
        printf(" %d",tmp->Element);
        tmp=tmp->Next;
    }
}/* details omitted */
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;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值