4-1-Deque

来源:互联网 发布:虎嗅商学院 淘宝客 编辑:程序博客网 时间:2024/04/29 12:20
4-1 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 Frontand 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 returnERROR which is defined by the judge program.

Sample program of judge:

#include <stdio.h>#include <stdlib.h>#define ElementType int#define ERROR 1e5typedef 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!\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;}/* Your function will be put here */

Sample Input:

PopInject 1PopEjectPush 1Push 2EjectInject 3End

Sample Output:

Deque is Empty!Deque is Empty!Inside Deque: 2 3
Deque CreateDeque(){PtrToNode head=NULL;Deque D;D = (struct DequeRecord *)malloc(sizeof(struct DequeRecord));D->Front = (struct Node *)malloc(sizeof(struct Node));D->Rear = D->Front;D->Front->Last = NULL;D->Rear->Next = NULL;return D;}int Push( ElementType X, Deque D ){PtrToNode temp;temp = (struct Node *)malloc(sizeof(struct Node));if(temp == NULL)return 0;temp->Element = X;if(D->Front == D->Rear){ /* 空队时,rear有特殊情况 */D->Front->Next = temp;temp->Last == D->Front;D->Rear = temp;D->Rear->Last = NULL;}temp->Next = D->Front->Next;D->Front->Next->Last = temp; /* mark */temp->Last = D->Front;D->Front->Next = temp;return 1;}ElementType Pop( Deque D ){int retvalue;PtrToNode tmp;if(D->Front == D->Rear)return ERROR;tmp = D->Front->Next;retvalue = tmp->Element;if(D->Front->Next == D->Rear){ /* 队列里只有一个元素时,rear有特殊情况 */D->Rear = D->Front;D->Front->Next = NULL;free(tmp);return retvalue;}D->Front->Next = tmp->Next;tmp->Next->Last = D->Front; /* mark */free(tmp);return retvalue;}int Inject( ElementType X, Deque D ){PtrToNode temp;temp = (struct Node *)malloc(sizeof(struct Node));if(temp == NULL)return 0;temp->Element = X;D->Rear->Next = temp;temp->Last = D->Rear;D->Rear = D->Rear->Next;D->Rear->Next = NULL;return 1;}ElementType Eject( Deque D ){int retvalue;PtrToNode tmp;if(D->Front == D->Rear)return ERROR;retvalue = D->Rear->Element;tmp = D->Rear;D->Rear = tmp->Last;D->Rear->Next = NULL;free(tmp);return retvalue;}
1 0