当前位置: 代码迷 >> 综合 >> 3-1 Deque (20分)
  详细解决方案

3-1 Deque (20分)

热度:90   发布时间:2023-12-18 12:20:34.0

3-1 Deque (20分)

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 dequeD 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 andEjectmust 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!\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:

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

AC代码:

Deque CreateDeque(){
    		//置空栈 Deque q;struct Node *p;q=(Deque)malloc(sizeof(Deque));p = (struct Node*)malloc(sizeof(struct Node));p->Next=p->Last= NULL;q->Front = p;q->Rear = p;return q;
}
int Push( ElementType X, Deque D ){
    		//入队操作 struct Node *p;p=(struct Node*)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	//如果队列不为空,将X插入到Front后,其他原有队列之前 {
    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;free(p);if(D->Front->Next){
    D->Front->Next->Last=D->Front;}else{
    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;
}
  相关解决方案