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. 关于c/c++语言的EOF(C++实现闰年判断)

    EOF 是 End Of File 的缩写,在 C 语言标准库中的定义如下: #define EOF (-1) 迄今为止,关于 EOF 作用的观点各异.大多数程序员认为“文件中有一个 EOF 字符,用 ...

  2. 如何准备Java面试?如何把面试官的提问引导到自己准备好的范围内?

    Java能力和面试能力,这是两个方面的技能,可以这样说,如果不准备,一些大神或许也能通过面试,但能力和工资有可能被低估.再仔细分析下原因,面试中问的问题,虽然在职位介绍里已经给出了范围,但针对每个点, ...

  3. python学习--quote()函数

    屏蔽特殊的字符.比如如果url里面的空格!url里面是不允许出现空格的. 在 Python2.x 中的用法是:urllib.quote(text)Python3.x 中是urllib.parse.qu ...

  4. Codeforces 1050D Three Religions (dp+序列自动机)

    题意: 给一个1e5的串str,然后有三个起始空串,不超过1000次操作,对三个字符串的一个尾部加一个字符或者减一个字符,保证每个字符不会超过250 每次操作之后询问你这三个串是不是可以组成str的子 ...

  5. Scala 学习(9)之「函数式编程」

    引用透明 对相同的输入,总是能得到相同的输出. 如果 f(x) 的参数 x 和函数体都是引用透明的,那么函数 f 是纯函数. 违反引用透明的例子 我们可以很清楚的看到,对于相同的输入,第二次调用app ...

  6. Python3 (五)函数应用

    一.认识函数 在命令行中查看内置函数的方法: 1.先在命令行里输入python 2.help(函数) 二.函数的定义及运行特点 1.函数基本定义: def funcname(parameter_lis ...

  7. apache主配置文件httpd.conf详解

    [root@lamp conf]# vi httpd.conf.bak 1 # 2 # This is the main Apache HTTP server configuration file. ...

  8. pymongo(看后转载,在原基础上添加了类连接和简单调用)

    一.MongoDB 数据库操作 1. 连接数据库 import pymongo conn = pymongo.Connection() # 连接本机数据库 # conn = pymongo.Conne ...

  9. codewars--js--RGB To Hex Conversion

    问题描述: The rgb() method is incomplete. Complete the method so that passing in RGB decimal values will ...

  10. Java Lamada

    Collection: ->stream:返回一个以 colleciotn 元素为数据源的数据流. -->map: 入参 Function 对象,将此流中的元素依次作用于传入的 Funct ...