zoukankan      html  css  js  c++  java
  • pat Deque(双端队列)

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

    Push(X,D): Insert item X on the front end of deque D.
    Pop(D): Remove the front item from deque D and return it.
    Inject(X,D): Insert item X on the rear end of deque D.
    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 );

    where Deque is defined as the following:

    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.
    Sample program of judge:

    #include <stdio.h>
    #include <stdlib.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 );
    
    Operation GetOp();          /* details omitted */
    void PrintDeque( Deque D ); /* 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!
    ");
                break;
            case pop:
                X = Pop(D);
                if ( X==ERROR ) printf("Deque is Empty!
    ");
                break;
            case inject: 
                scanf("%d", &X);
                if (!Inject(X, D)) printf("Memory is Full!
    ");
                break;
            case eject:
                X = Eject(D);
                if ( X==ERROR ) printf("Deque is Empty!
    ");
                break;
            case end:
                PrintDeque(D);
                done = 1;
                break;
            }
        }
        return 0;
    }
    
    /* Your function will be put here */

    Sample Input:

    Pop
    Inject 1
    Pop
    Eject
    Push 1
    Push 2
    Eject
    Inject 3
    End

    Sample Output:

    Deque is Empty!
    Deque is Empty!
    Inside Deque: 2 3

    思路:
    双端队列,头节点保持不动,然后添加和删除的时候注意队列为空和有一个节点的情况就行了。一定注意时刻保持链表的连贯性。

    代码:

    
    Deque CreateDeque()
    {
        PtrToNode p = (PtrToNode)malloc(sizeof(struct Node));
        p->Next = NULL;
        p->Last = NULL;
        Deque re = (Deque)malloc(sizeof(struct DequeRecord));
        re->Front = p;
        re->Rear = p;
        return re;
    }
    
    int Push( ElementType X, Deque D )
    {
        PtrToNode p = (PtrToNode)malloc(sizeof(struct Node));
        if(p == NULL)return 0;
        p->Next = D->Front->Next;
        p->Last = D->Front;
        p->Element = X;
        if(D->Front->Next)D->Front->Next->Last = p;
        D->Front->Next = p;
        if(D->Front == D->Rear)D->Rear = p;
        return 1;
    }
    
    ElementType Pop( Deque D )
    {
        if(D->Front == D->Rear)return ERROR;
        D->Front = D->Front->Next;
        D->Front->Last = NULL;
        return D->Front->Element;
    }
    
    int Inject( ElementType X, Deque D )
    {
        PtrToNode p = (PtrToNode)malloc(sizeof(struct Node));
        if(p == NULL)return 0;
        p->Element = X;
        p->Last = D->Rear;
        p->Next = NULL;
        D->Rear->Next = p;
        D->Rear = p;
        return 1;
    }
    
    ElementType Eject( Deque D )
    {
        if(D->Front == D->Rear)return ERROR;
        int re = D->Rear->Element;
        D->Rear = D->Rear->Last;
        D->Rear->Next = NULL;
        return re;
    }
    
  • 相关阅读:
    yii1.0 yii2.0 ar
    php 函数
    整除理论,1.1数的整除性定理总结
    设M=5^2003+7^2004+9^2005+11^2006,求证8|M。(整除理论,1.1.8)
    已知整数m,n,p,q适合(m-p)|(mn+pq)证明:(m-p)|(mq+np)(整除理论1.1.5)
    证明:一个整数a若不能被6整除,则a2+24必能被24整除。(整除理论,1.1.4)
    ural 1073.Square Country(动态规划)
    10个男孩和n个女孩共买了n2+8n+2本书,已知他们每人买的书本的数量是相同的,且女孩人数多于南海人数,问女孩人数是多少?(整除原理1.1.3)
    设正整数n的十进制表示为n=ak……a1a0(0<=ai<=9,0<=i<=k,ak!=0),n的个位为起始数字的数字的正负交错之和T(n)=a0+a1+……+(-1)kak,证明:11|n的充分必要条件是11|T(n);(整除理论1.1.2))
    设n是奇数,证明:16|(n4+4n2+11)(整除原理1.1.1)
  • 原文地址:https://www.cnblogs.com/vocaloid01/p/9514210.html
Copyright © 2011-2022 走看看