PTA--双向链表模拟Deque
来源:互联网 发布:汇率软件 编辑:程序博客网 时间:2024/05/21 18:41
//最重要的是细心
#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 q=(Deque)malloc(sizeof(struct DequeRecord)); q->Front=(PtrToNode)malloc(sizeof(struct Node)); q->Front->Last=NULL; q->Rear=q->Front;//初始状态头和尾指向同一个位置 q->Rear->Next=NULL; return q;}//Insert item X on the front end of deque D.int Push( ElementType X, Deque D ){ struct Node* tmp=(struct Node *)malloc(sizeof(struct Node)); tmp->Element=X; if(D->Front==D->Rear) {//第一个元素,特殊处理,front->元素(更新为rear) D->Front->Next=tmp; tmp->Last=D->Front; D->Rear=tmp; tmp->Next=NULL; return 1; } //front->元素->...->元素(新的rear) tmp->Next=D->Front->Next; D->Front->Next->Last=tmp; D->Front->Next=tmp; tmp->Last=D->Front; return 1;}//Remove the front item from deque D and return it.ElementType Pop( Deque D ){ if(D->Front==D->Rear) return ERROR; int tmp=D->Front->Next->Element; struct Node* pre=D->Front->Next; if(D->Front->Next==D->Rear) {//一个元素的特殊情况 D->Rear=D->Front; D->Rear->Next=NULL; free(pre); return tmp; } D->Front->Next->Next->Last=D->Front; D->Front->Next=D->Front->Next->Next; free(pre); return tmp;}// Insert item X on the rear end of deque D.int Inject( ElementType X, Deque D ){ struct Node* tmp=(struct Node *)malloc(sizeof(struct Node)); tmp->Element=X; if(D->Rear==D->Front) {//第一个元素,特殊处理,front->元素(更新为rear) D->Front->Next=tmp; tmp->Last=D->Front; D->Rear=tmp; tmp->Next=NULL; return 1; } tmp->Last=D->Rear; D->Rear->Next=tmp; tmp->Next=NULL; D->Rear=tmp; return 1;}//Remove the rear item from deque D and return it. Write routines to support the deque that take O(1) time per operation.ElementType Eject( Deque D ){ if(D->Front==D->Rear) return ERROR; int tmp=D->Rear->Element; struct Node* pre=D->Rear; D->Rear=D->Rear->Last; D->Rear->Next=NULL; free(pre); return tmp;}Operation GetOp(){ char op[11]; scanf("%s",op); if(strcmp(op,"Push")==0) return push; if(strcmp(op,"Pop")==0) return pop; if(strcmp(op,"Inject")==0) return inject; if(strcmp(op,"Eject")==0) return eject; return end;}/* details omitted */void PrintDeque( Deque D ){ struct Node* tmp=D->Front->Next; printf("Inside Deque: "); if(tmp) printf("%d",tmp->Element),tmp=tmp->Next; else return; while(tmp) { printf(" %d",tmp->Element); tmp=tmp->Next; }}/* 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;}
0 0
- PTA--双向链表模拟Deque
- PTA 4-1 Deque
- PAT L2-022 重排链表 (deque双向队列)
- 用数组模拟双向链表
- UVA-133 双向链表模拟题
- UVA 12657(双向链表模拟)
- 【数组模拟链表(双向)】UVA
- NOIP2017赛前模拟 Table(双向链表)
- deque双向队列
- C++ Deque(双向队列)
- Deque(双向队列)
- deque 双向排序
- deque双向队列
- C++ Deque 双向队列
- deque双向队列
- deque双向队列
- deque容器(双向队列)
- deque 双向队列
- [精华] 初识Tornado:一个简单例子
- SecureCRT安装与使用
- 对元音个数进行统计,按下回车符退出
- poj 1082 Calendar Game 博弈递推
- cocoa各种图片格式转换
- PTA--双向链表模拟Deque
- AlertDialog最基本应用
- 远行
- android ScrollByScrollTo以及平移动画以及重新LayoutParams的区别
- Collection与Collections
- 【框架-MFC】获取当前鼠标的ICON图标(光标)
- Spring task quartz 定时任务的几种实现
- HDU5882——Balanced Game(2016年青岛区域赛网络赛)
- Fast特征点检测