PTA--双向链表模拟Deque
2016-11-23 11:06
435 查看
//最重要的是细心
#include <stdio.h> #include <stdlib.h> #include <cstring> #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() { 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; }
相关文章推荐
- 用数组模拟双向链表
- Java模拟单向链表和双向链表的实现
- 【数组模拟链表(双向)】UVA - 12657 Boxes in a Line
- PAT L2-022 重排链表 (deque双向队列)
- C++模拟双向链表的基本操作
- UVA12657 Boxes in a Line【双向链表】【数组模拟】
- Java模拟单向链表和双向链表的实现
- HDU 4286 Data Handler 第37届ACM/ICPC 天津赛区网络赛1009题 (双向链表模拟)
- STL--> list 双向循环链表容器 接口使用及介绍。 模拟实现 STL list容器
- Java模拟单向链表和双向链表的实现
- HDU 5510 Bazinga(模拟双向链表或kmp或AC自动机)
- 用动态数组模拟双向循环链表
- java数据结构之LinkedDeque(用链表实现的双端(即头尾两个哨兵节点)双向(node里两个指向)队列)
- C 语言 实现双向链表 模拟ArrayList功能 可追加,插入,移除,得到指定index 的元素值
- 玲珑OJ 1045 - I. Quailty and LRU Algorithm 模拟双向链表
- Java模拟单向链表和双向链表的实现
- (双向链表之数组模拟)Boxes in a Line,第九届湖南省赛,B题
- Java模拟单向链表和双向链表的实现
- csu 1365 双向链表模拟超时
- Java模拟单向链表和双向链表的实现