3-1 Deque

Deque CreateDeque()
{
    Deque p;
    p=(Deque)malloc(sizeof (struct DequeRecord));
    p->Front=(PtrToNode)malloc(sizeof (struct Node));
     p->Rear=(PtrToNode)malloc(sizeof (struct Node));
    p->Front->Last=NULL;
    p->Rear=p->Front;
    p->Rear->Next=NULL;
    return p;
}
int Push(ElementType X,Deque D)
{
    struct Node*tmp;
    tmp=(struct Node*)malloc(sizeof(struct Node));
    if(!tmp)return 0;
    tmp->Element=X;
    if(D->Front==D->Rear)
    {
        D->Front->Next=tmp;
        tmp->Last=D->Front;
        D->Rear=tmp;
        D->Rear->Next=NULL;
        return 1;
    }
    tmp->Next=D->Front->Next;
    tmp->Last=D->Front;
    D->Front->Next->Last=tmp;
    D->Front->Next=tmp;
    return 1;
}
ElementType Pop(Deque D)
{
    if(D->Front==D->Rear)
        return ERROR;
    int num=D->Front->Next->Element;
    struct Node *tmp=D->Front->Next;
    if(D->Front->Next==D->Rear)
    {
        D->Rear=D->Front;
        D->Rear->Next=NULL;
        free(tmp);
        return num;
    }
    D->Front->Next->Next->Last=D->Front;
    D->Front->Next=D->Front->Next->Next;
    free(tmp);
    return num;
}
int Inject(ElementType X,Deque D)
{
    struct Node* tmp;
    tmp=(struct Node*)malloc(sizeof(struct Node));
    if(!tmp)return 0;
    tmp->Element=X;
    if(D->Front==D->Rear)
    {
        D->Front->Next=tmp;
        tmp->Last=D->Front;
        D->Rear=tmp;
        return 1;
    }
    D->Rear->Next=tmp;
    tmp->Last=D->Rear;
    tmp->Next=NULL;
    D->Rear=tmp;
    return 1;
}
ElementType Eject(Deque D)
{
    if(D->Front==D->Rear)
        return ERROR;
    int num=D->Rear->Element;
    struct Node*tmp=D->Rear;
    D->Rear=D->Rear->Last;
    D->Rear->Next=NULL;
    free(tmp);
    return num;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值