PTA_6-6 Deque(25 分)手动实现双端队列
来源:互联网 发布:外汇行情数据接口 编辑:程序博客网 时间:2024/06/06 13:23
6-6 Deque(25 分)
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 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:
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
/*#include <stdio.h>#include <stdlib.h>#include<cstring>#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(){ Deque p; p = (Deque)malloc(sizeof(struct DequeRecord));//队列开空间 p->Front = (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 tmp->Last = D->Front;//tmp头指针指向头部 D->Rear = tmp;//tmp由rear取代 tmp->Next = NULL;//尾部为空 return 1; } //非空部分 tmp->Next = D->Front->Next;//tmp尾指针=头部尾指针相同 tmp->Last = D->Front;//tmp头指针指向头部 D->Front->Next->Last = tmp;//头部下下一个的头指针=tmp; D->Front->Next = tmp;//头部尾指针=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 = (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; //tmp->Next = NULL; 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;}/*Operation GetOp(){ char a[111]; scanf("%s", a); //push, pop, inject, eject, end if (!strcmp("Push", a)) return push; if (!strcmp("Pop", a)) return pop; if (!strcmp("Inject", a)) return inject; if (!strcmp("Eject", a)) return eject; if (!strcmp("End", a)) return end;}void PrintDeque(Deque D){ while (D->Front != D->Rear) { printf("%d ", Pop(D)); } puts("");}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;}*/
- PTA_6-6 Deque(25 分)手动实现双端队列
- 双端队列(deque)
- 双端队列(Deque)
- 双端队列(deque)链表实现
- 双端队列(deque)数组实现
- JDK7中的双端队列Deque实现
- 双端队列Deque
- 双端队列deque
- 双端队列 (deque)
- 例题6-1 UVa 210(deque 双端队列)
- STL - deque(双端队列)
- 双端队列(deque)的应用
- pat Deque(双端队列)
- STL(四) deque 双端队列
- poj2823 单调队列以及双端队列(deque)
- 双端队列(Deque)的python实现及应用
- STL之deque双端队列的实现
- Deque双端队列容器
- kubernetes中 demo: tomcat + mysql
- 基于jQuery的三级联动控件
- jqGrid
- 杭电ACM OJ 1029 Ignatius and the Princess IV 快速排序 挖坑填埋法
- JS事件流,事件绑定
- PTA_6-6 Deque(25 分)手动实现双端队列
- 【离散数学】2.1什么是命题
- getWriter() has already been called for this response错误
- eclipse安装php报错
- 分布式事物解决方案含视频教程
- Linux下多网卡绑定bonding bond6
- 转移表
- 装载问题--回溯法
- CUDA8.0安装的问题