PTA Deque (C语言)
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语言)的更多相关文章
- 小白专场-FileTransfer-c语言实现
目录 一.集合的简化表示 二.题意理解 三.程序框架搭建 3.1 Input_connection 3.2 Check_connection 3.3 Check_network 四.pta测试 五.按 ...
- C语言Ⅰ博客作业07
这个作业属于那个课程 C语言程序设计II 这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/CST2019-3/homework/9933 我在这个课程的目 ...
- C语言Ⅰ博客作业05
这个作业属于那个课程 C语言程序设计II 这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/CST2019-3/homework/9827 我在这个课程的目 ...
- 第二周c语言PTA作业留
6-1 计算两数的和与差(10 分) 本题要求实现一个计算输入的两数的和与差的简单函数. 函数接口定义: void sum_diff( float op1, float op2, float psum ...
- C语言第一次实验报告————PTA实验1.2.3内容
一.PTA实验作业 题目1.温度转换 本题要求编写程序,计算华氏温度100°F对应的摄氏温度.计算公式:C=5×(F−32)/9,式中:C表示摄氏温度,F表示华氏温度,输出数据要求为整型. 1.实验代 ...
- c语言课本及pta作业中运用到的程序思维
c语言课本运用到的程序思维 我个人觉得在写程序的时候,有很多题目会用到我们学过的解决一个程序或者一个问题的方法,把这些方法运用起来,将会使自己更加灵活地解决诸多问题,为今后打下良好地基础. (因为还没 ...
- PTA 汉诺塔的非递归实现(C 语言)
借助堆栈以非递归(循环)方式求解汉诺塔的问题(n, a, b, c), 即将N个盘子从起始柱(标记为“a”)通过借助柱(标记为“b”)移动到目标柱(标记为“c”), 并保证每个移动符合汉诺塔问题的要求 ...
- PTA 学生成绩链表处理(C语言)
本题要求实现两个函数,一个将输入的学生成绩组织成单向链表:另一个将成绩低于某分数线的学生结点从链表中删除. 函数接口定义: struct stud_node *createlist(); struct ...
- PTA 简单计算器(C语言)
模拟简单运算器的工作.假设计算器只能进行加减乘除运算,运算数和结果都是整数,四种运算符的优先级相同,按从左到右的顺序计算. 输入格式:输入在一行中给出一个四则运算算式,没有空格,且至少有一个操作数.遇 ...
随机推荐
- 四、Django学习之关系表介绍及使用
关系表介绍及使用 一对一关系 xx = models.OneToOneField(to='表名',to_field='字段名',on_delete=models.CASCADE) #on_delete ...
- ubuntu16.04(其他版本也可)批量修改图片名---shell编程
在windows系统中有很多好用的图片排序软件,可以批量的进行图片排序.然而在ubuntu中,图片排序只能自己写一个shell脚本,编写shell代码.下面是具体的操作步骤.(1).新建一个renam ...
- 使用纯C++迭代器编写归并排序
第一次尝试用C++迭代器编写算法,使用的是纯迭代器 void mergeSort(vector<int>::iterator beg, vector<int>::iterato ...
- FPGA VGA+PLL+IP核笔记
1.实现了预定功能!整个工程,没有使用例程的25MHZ,全部统一使用50MHZ.2.分辨率使用了800*600@72HZ.3.实现了只显示白色部分,黑色部分RGB == 0,要显示背景色.VGA图形基 ...
- python学习记录(四)
0828--https://www.cnblogs.com/fnng/archive/2013/04/18/3029807.html 0828--https://www.cnblogs.com/fnn ...
- HDU 1251 统计难题 (Trie树模板题)
题目链接:点击打开链接 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单 ...
- 《Python学习手册 第五版》 -第8章 列表与字典
前面已经讲过数值类型(第5章)和字符串类型(第7章),本章继续其他数据类型的讲解:列表和字典 本章的核心内容 1.列表 1)什么是列表 2)基本列表操作 3)列表迭代和推导 4)索引.分片和矩阵 5) ...
- 曹工说Spring Boot源码(18)-- Spring AOP源码分析三部曲,终于快讲完了 (aop:config完整解析【下】)
写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享 曹工说Spring Boot源码(2)-- Bean ...
- 【OpenGL】GL_DEPTH_TEST深度测试问题
记录一个深度测试的问题 在实现一个简单的OpenGL程序时,遇到了一个问题,深度测试总是有问题,无法正常显示,如下 正常情况为 通过调试发现屏幕空间中的所有深度值均为1. OpenGL代码如下: vo ...
- pytorch之 RNN regression
关于RNN模型参数的解释,可以参看RNN参数解释 1 import torch from torch import nn import numpy as np import matplotlib.py ...