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. Maven - 配置管理

    Maven Maven是一款自动化构建工具,专注服务于Java平台的项目构建和依赖管理.Project Object Model:项目对象模型.将Java工程的相关信息封装为对象形式作为便于操作和管理 ...

  2. 生成HTML测试报告表格

    #生成HTML测试报告 #-*- coding:utf-8 -*- from selenium import webdriver from selenium.webdriver.common.by i ...

  3. Python 代码风格规范(Google)

    Python风格规范 分号 tip 不要在行尾加分号, 也不要用分号将两条命令放在同一行. 行长度 tip 每行不超过80个字符 例外: 长的导入模块语句 注释里的URL 不要使用反斜杠连接行. Py ...

  4. HDU_2191_多重背包

    http://acm.hdu.edu.cn/showproblem.php?pid=2191 简单多重背包题. #include<iostream> #include<cstdio& ...

  5. Codeforces_429_B

    http://codeforces.com/problemset/problem/429/B 挺简单的题,先求出四个点到每一点的最大和,然后枚举每一点,取和最大值. 注意两条路相交的点有且只有一个,这 ...

  6. CentOS8 上安装Docker

    从 2017 年 3 月开始 docker 在原来的基础上分为两个分支版本: Docker CE 和 Docker EE.Docker CE 即社区免费版,Docker EE 即企业版,强调安全,但需 ...

  7. JDBC访问数据库的具体步骤(MySql + Oracle + SQLServer)

    * 感谢DT课堂颜群老师的视频讲解(讲的十分仔细,文末有视频链接) import java.sql.Connection; import java.sql.DriverManager; import ...

  8. 入侵检测基本准则(Basic principles of intrusion detection)【v1.0】

    所谓“入侵检测”,顾名思义,就是对入侵行为的发觉.他通过对计算机网络或计算机系统中若干关键点收集信息并对其进行分析,从中发现网络或系统中是否有违反安全策略的行为和被攻击的迹象.” 但实际上,所谓的“违 ...

  9. 终于成功部署 Kubernetes HPA 基于 QPS 进行自动伸缩

    昨天晚上通过压测验证了 HPA 部署成功了. 所使用的 HPA 配置文件如下: apiVersion: autoscaling/v2beta2 kind: HorizontalPodAutoscale ...

  10. MySQL中大数据表增加字段,增加索引实现

    MySQL中大数据表增加字段,通过增加索引实现 普通的添加字段sql ALTER TABLE `table_name` ADD COLUMN `num` int(10) NOT NULL DEFAUL ...