6-3 Deque (10 分)

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 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

Solution

Deque CreateDeque()
{Deque a=(Deque)malloc(sizeof(struct DequeRecord));PtrToNode h=(PtrToNode)malloc(sizeof(struct Node));    h->Next=NULL;h->Last=NULL;a->Front=h;a->Rear=h;return a;
}int Push(ElementType X,Deque D)
{PtrToNode n=(PtrToNode)malloc(sizeof(struct Node));n->Element=X;if(D->Front==D->Rear){D->Front->Next=n;n->Last=D->Front;D->Rear=n;n->Next=NULL;}else{n->Next = D->Front->Next; n->Last = D->Front;D->Front->Next->Last=n;D->Front->Next=n;}return 1;
}ElementType Pop(Deque D)
{if(D->Front==D->Rear)return ERROR;PtrToNode a;    int b;a=D->Front->Next;b=a->Element;if(a->Next==NULL){D->Front->Next=NULL;D->Rear=D->Front;free(a);}else{D->Front->Next=a->Next;a->Next->Last=D->Front;free(a);}return b;
}int Inject(ElementType X,Deque D)
{PtrToNode n=(PtrToNode)malloc(sizeof(struct Node));n->Next=NULL;n->Element=X;if(D->Front==D->Rear){D->Front->Next=n;n->Last=D->Front;D->Rear=n;}else {D->Rear->Next=n;n->Last=D->Rear;D->Rear=n;}return X;
}ElementType Eject(Deque D)
{if (D->Front==D->Rear)return ERROR;PtrToNode a;int b;a=D->Rear;b=a->Element;D->Rear=a->Last;a->Last->Next=NULL;free(a);return b;
}

数据结构之C语言Dequeue相关推荐

  1. 数据结构(C语言版) 第 三 章 栈与队列 知识梳理 + 作业习题详解

    目录 一.栈 0.栈的基本概念 1.栈的实现 2.栈与递归 3.Hanoi塔问题 二.队列 0.队列的基本概念 1.队列的实现 2.循环队列 2.1循环队列的相关条件和公式: 3.链队列 4.链队列完 ...

  2. 算法和数据结构(golang语言实现)

    算法和数据结构(golang语言实现) 第1节 选择.冒泡.插入.复杂度 选择排序 选择排序 时间复杂度为O(N^2) 额外空间复杂度O(1) 过程: arr[0-N-1]范围上,找到最小值所在的位置 ...

  3. 公交线路图查询系统c语言,公交路线查询系统(基于数据结构和C语言)完整

    公交路线查询系统(基于数据结构和C语言)完整 #include #include #include #include #define max 30 #define len 20 #define MAX ...

  4. 资料分享:送你一本《数据结构(C语言版)》电子书!

    要想写出可复用.可扩展.易维护.灵活性好的代码,「数据结构」这一关必须要过啊! 在数据结构与算法的众多教材中,奉为经典的当属清华大学严蔚敏老师的著作.很多学校也选择这本书作为考研指定教材. 正在学习数 ...

  5. 资料分享:送你一本《数据结构(C#语言版)》电子书!

    对于信息类专业的学生而言,数据结构与算法是一门必修的课程.只有学好这门课程,熟练掌握线性表.栈.队列.树.图等基本结构,以及在这些结构上的各种算法,才能利用计算机去解决实际问题. 如何学好这门课程呢, ...

  6. 数据结构(C语言版) 第 八 章 排序 知识梳理 + 习题详解

    目录 一.归并排序 二.交换排序 1.快速排序 2.冒泡排序 三.插入排序 1.直接插入排序(基于顺序查找) 2.折半插入排序(基于折半查找) 3.希尔排序(基于逐趟缩小增量) 四.选择排序 0.直接 ...

  7. 数据结构(C语言版) 第 六 章 图 知识梳理 + 习题详解

    目录 一. 图的基本定义和术语 一.图的基本概念 1.度 2.连通 (1)连通图 (2)强连通/强连通图 3.回路 4.完全图 二.图的三种存储结构 1.邻接矩阵表示法 2.邻接表(链式)表示法 3. ...

  8. 数据结构(C语言版) 第二章 线性表 知识梳理+作业习题详解

    目录 一.线性表顺序存储结构(顺序表) 0.线性表的基本概念 1.样例引入:多项式相加 二.线性表链式存储结构(链表) 0.链表的基本概念 1.前插法代码实例 2.链表尾插法完整代码附带各种操作 三. ...

  9. 严蔚敏版《数据结构 (C语言版)》和《数据结构题集》(一)

    这里用的是严蔚敏版<数据结构 (C语言版)>和<数据结构题集>,深感自己的代码写得又渣又无力,打算把这本书好好琢磨透彻,里面的算法和模板都实现一遍,题目也都做一遍.最终能够做到 ...

最新文章

  1. ASP.NET MVC5+EF6+EasyUI 后台管理系统(50)-Easyui 扁平化皮肤
  2. 深入理解l内核v4l2框架之video for linux 2(一)
  3. linux c段错误,Linux C中段错误
  4. 脉冲神经网络的开发公司AI-CTX
  5. 戴尔电脑安装win 7
  6. 6个最佳的开源Python应用服务器
  7. UVa 10118 免费糖果(记忆化搜索+哈希)
  8. 斯坦福大学深度学习公开课cs231n学习笔记(4)正向传播及反向传播
  9. k8s traefik 映射外部服务,映射其他域名,映射内网其他服务
  10. java设计模式----代理模式
  11. Python3快速入门—7.枚举
  12. DDOS攻击/防御介绍
  13. Centos7命令行方式安装DM
  14. 使用libusb读取鼠标数据
  15. XXE漏洞详解(三)——XXE漏洞实际运用
  16. 600道计算机二级python选择题在线真题题库
  17. 适老化专栏(二)| 互联网世界中的老年人
  18. ERP系统对接方案,API接口封装系列(高并发)
  19. leetcode977
  20. 编写强力黑白棋的历程

热门文章

  1. 得到SIM卡串号和IMEI号
  2. WPS、WORD文档插入手写电子签名
  3. Android平台实现系统内录(捕获播放的音频)并推送RTMP服务技术方案探究
  4. 可以在线编辑任何网页的JS代码
  5. python的双色球模拟算奖
  6. JS判断是否是ioS或者Android
  7. 用后处理做一个烟雾的效果(也可以用指数雾达到类似的效果)
  8. WIN7 转换为动态磁盘(管理工具下无法新建分区的解决方案),跨盘符合并分区
  9. 怎么用文本文档写html框架,基于Metronic的Bootstrap开发框架经验总结(17)-- 使用 summernote插件实现HTML文档的编辑和图片插入操作...
  10. Nature旗下SCI期刊征收AI方向论文, 56%录用率, 5个月出录用