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 = ; 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 = ;
break;
}
}
return ;
} /* Your function will be put here */

Sample Input:

Pop
Inject
Pop
Eject
Push
Push
Eject
Inject
End

Sample Output:

Deque is Empty!
Deque is Empty!
Inside Deque:

题意:这道题要求设计一个链式双端队列 deque,并且要求有头指针。

初始时,链表为空,头指针 Front 和尾指针 Rear 指向同一片空的空间,

Push :将项 X 插入双端队列  D 的前端。下图为 Push(2) Push(1) 操作结果

Pop:从双端队列 D 中删除前端一项并返回。下图为 Pop() 操作结果

Inject:将项目 X 插入双端队列 D 的后端。下图为 Inject(3) 操作结果

Eject:从双端队列 D 中删除后端一项并返回。下图为Eject() 操作结果

若 Push 和  Inject操作成功,返回1,否则返回0;若双端队列为空,Pop 和 Eject 返回  ERROR,否则返回弹出结点的 Element。

代码:

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

PTA Deque (C语言)的更多相关文章

  1. 小白专场-FileTransfer-c语言实现

    目录 一.集合的简化表示 二.题意理解 三.程序框架搭建 3.1 Input_connection 3.2 Check_connection 3.3 Check_network 四.pta测试 五.按 ...

  2. C语言Ⅰ博客作业07

    这个作业属于那个课程 C语言程序设计II 这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/CST2019-3/homework/9933 我在这个课程的目 ...

  3. C语言Ⅰ博客作业05

    这个作业属于那个课程 C语言程序设计II 这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/CST2019-3/homework/9827 我在这个课程的目 ...

  4. 第二周c语言PTA作业留

    6-1 计算两数的和与差(10 分) 本题要求实现一个计算输入的两数的和与差的简单函数. 函数接口定义: void sum_diff( float op1, float op2, float psum ...

  5. C语言第一次实验报告————PTA实验1.2.3内容

    一.PTA实验作业 题目1.温度转换 本题要求编写程序,计算华氏温度100°F对应的摄氏温度.计算公式:C=5×(F−32)/9,式中:C表示摄氏温度,F表示华氏温度,输出数据要求为整型. 1.实验代 ...

  6. c语言课本及pta作业中运用到的程序思维

    c语言课本运用到的程序思维 我个人觉得在写程序的时候,有很多题目会用到我们学过的解决一个程序或者一个问题的方法,把这些方法运用起来,将会使自己更加灵活地解决诸多问题,为今后打下良好地基础. (因为还没 ...

  7. PTA 汉诺塔的非递归实现(C 语言)

    借助堆栈以非递归(循环)方式求解汉诺塔的问题(n, a, b, c), 即将N个盘子从起始柱(标记为“a”)通过借助柱(标记为“b”)移动到目标柱(标记为“c”), 并保证每个移动符合汉诺塔问题的要求 ...

  8. PTA 学生成绩链表处理(C语言)

    本题要求实现两个函数,一个将输入的学生成绩组织成单向链表:另一个将成绩低于某分数线的学生结点从链表中删除. 函数接口定义: struct stud_node *createlist(); struct ...

  9. PTA 简单计算器(C语言)

    模拟简单运算器的工作.假设计算器只能进行加减乘除运算,运算数和结果都是整数,四种运算符的优先级相同,按从左到右的顺序计算. 输入格式:输入在一行中给出一个四则运算算式,没有空格,且至少有一个操作数.遇 ...

随机推荐

  1. "@阅后即焚"上线了!

    前一阵发现了一个有趣的网站,他可以让你的文字在显示一次后销毁. 直到我把网站发给一个朋友,网站打不开了,于是就想着开发一个. 前端用的bootstrap这个框架,后端用PHP写的,没有后台,现在还不需 ...

  2. Codeforces_456_A

    http://codeforces.com/problemset/problem/456/A 按价格排序,比较质量. #include<cstdio> #include<algori ...

  3. JavaScript 与 Java 有什么不同?

    JavaScript 编程语言是由 Netscape,Inc. 开发的,它并不是 Java 平台的一部分. JavaScript 不会创建小应用程序或独立应用程序.在最常见的形式中,JavaScrip ...

  4. 分布式SnowFlakeID(雪花ID)原理和改进优化

    最近在研究分布式框架的组件和整体设计思路.所有的问题,一旦涉及分布式难度就呈几何倍数的提升.包括最常见的ID生成也是,单机情况下,使用数据库自增ID.UUID都是简单易行的选择 但在分布式环境下,就需 ...

  5. win10系统下安装JDK1.8及配置环境变量的方法

    本次演示基于windows10操作系统,如果你是linux,请参考:https://www.yn2333.com/archives/linux上安装JDK8 1:下载安装包 地址:https://ww ...

  6. sun.misc.Unsafe中一些常用方法记录

    sun.misc.Unsafe中一些常用方法记录 前情摘要 sun公司提供了可以用于直接操作内存的类,这个类就是sun.misc.Unsafe.因为Java本身是不会涉及到直接操作内存的,Java A ...

  7. 集智学院 “Deep X:Deep Learning with Deep Knowledge”的公开讲座---总结

    人工智能旨在了解人类智能的本质,并创造出能模仿人类智能做出反应的智能机器,目前在一些领域已经取得显著的成功,如AI玩游戏.问答系统.自动驾驶.无人机.机器人.翻译.人脸识别.语音识别等领域.深度学习的 ...

  8. linux 手工释放内存 高内存 内存回收 方法思路

    linux  跑的apache,apache工作模式有   Prefork.Worker和 Event  三种,分别是基于进程.线程.综合模式.        本文中使用的apache是 Event  ...

  9. Day5前端学习之路——盒模型和浮动

    盒子模型 浮动float 一.盒子模型 (1)content内容区 width和height是框内容显示的区域——包括框内的文本内容,以及表示嵌套子元素的其他框,也可以使用min-width.max- ...

  10. bootstrap--网格化布局

    1.响应式网格系统随着屏幕或视口(viewport)尺寸的增加,系统会自动分为最多12列 2.规则 行必须放置在 .container class 内,以便获得适当的对齐(alignment)和内边距 ...