6-1 Deque(25 分)Data Structures and Algorithms (English)
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
代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.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;
}
PrintDeque(D);
}
return ;
}
void PrintDeque( Deque D )
{
if(D -> Rear == D -> Front)
{
printf("Deque is Empty!");
return;
}
printf("Inside Deque:");
PtrToNode p = D -> Front -> Next;
while(p)
{
printf(" %d",p -> Element);
p = p -> Next;
}
}
Operation GetOp()
{
char s[];
scanf("%s",s);
if(!strcmp(s,"Pop"))return pop;
else if(!strcmp(s,"Push"))return push;
else if(!strcmp(s,"Inject"))return inject;
else if(!strcmp(s,"Eject"))return eject;
else return end;
}
Deque CreateDeque()
{
Deque q = (Deque)malloc(sizeof(struct DequeRecord));///申请个队列
PtrToNode p = (PtrToNode)malloc(sizeof(struct Node));///申请一个头结点,不添加数据
p -> Next = p -> Last = NULL;///头结点最初前后都指向空
q -> Front = q -> Rear = p;///队列首尾都指向它
return q;
}
int Push( ElementType X, Deque D )///在首插入一个结点
{
PtrToNode p = (PtrToNode)malloc(sizeof(struct Node));
if(p == NULL)return ;///申请失败
p -> Element = X;///添加数据 p -> Next = D -> Front -> Next;///p指向原来的第一个非头结点
if(p -> Next)p -> Next -> Last = p;///然后非头结点还要指回来 前提是存在
p -> Last = D -> Front;
D -> Front -> Next = p;
if(D -> Rear == D -> Front) D -> Rear = p;
return ;///成功
}
int Inject( ElementType X, Deque D )
{
PtrToNode p = (PtrToNode)malloc(sizeof(struct Node));
if(p == NULL)return ;///申请失败
p -> Element = X;///添加数据
p -> Last = D -> Rear;
p -> Next = NULL;
D -> Rear -> Next = p;
D -> Rear = p;
return ;///成功
}
ElementType Pop( Deque D )
{
if(D -> Front == D -> Rear)return ERROR;///只有头结点,即队列为空
PtrToNode p = D -> Front -> Next;
ElementType d = p -> Element;
D -> Front -> Next = p -> Next;
if(p -> Next)p -> Next -> Last = D -> Front;
free(p);
if(D -> Front -> Next == NULL)D -> Rear = D -> Front;
return d;
}
ElementType Eject( Deque D )
{
if(D -> Front == D -> Rear)return ERROR;
PtrToNode p = D -> Rear;
ElementType d = p -> Element;
D -> Rear = p -> Last;
p -> Last -> Next = NULL;
free(p);
return d;
}
6-1 Deque(25 分)Data Structures and Algorithms (English)的更多相关文章
- CSC 172 (Data Structures and Algorithms)
Project #3 (STREET MAPPING)CSC 172 (Data Structures and Algorithms), Spring 2019,University of Roche ...
- CSIS 1119B/C Introduction to Data Structures and Algorithms
CSIS 1119B/C Introduction to Data Structures and Algorithms Programming Assignment TwoDue Date: 18 A ...
- Basic Data Structures and Algorithms in the Linux Kernel--reference
http://luisbg.blogalia.com/historias/74062 Thanks to Vijay D'Silva's brilliant answer in cstheory.st ...
- 剪短的python数据结构和算法的书《Data Structures and Algorithms Using Python》
按书上练习完,就可以知道日常的用处啦 #!/usr/bin/env python # -*- coding: utf-8 -*- # learn <<Problem Solving wit ...
- [Data Structures and Algorithms - 1] Introduction & Mathematics
References: 1. Stanford University CS97SI by Jaehyun Park 2. Introduction to Algorithms 3. Kuangbin' ...
- 学习笔记之Problem Solving with Algorithms and Data Structures using Python
Problem Solving with Algorithms and Data Structures using Python — Problem Solving with Algorithms a ...
- Trainning Guide, Data Structures, Example
最近在复习数据结构,发现这套题不错,题目质量好,覆盖广,Data Structures部分包括Example,以及简单,中等,难三个部分,这几天把Example的做完了, 摘要如下: 通过这几题让我复 ...
- Python Tutorial 学习(五)--Data Structures
5. Data Structures 这一章来说说Python的数据结构 5.1. More on Lists 之前的文字里面简单的介绍了一些基本的东西,其中就涉及到了list的一点点的使用.当然,它 ...
- [译]The Python Tutorial#5. Data Structures
[译]The Python Tutorial#Data Structures 5.1 Data Structures 本章节详细介绍之前介绍过的一些内容,并且也会介绍一些新的内容. 5.1 More ...
随机推荐
- C++中的RAII介绍 资源管理
摘要 RAII技术被认为是C++中管理资源的最佳方法,进一步引申,使用RAII技术也可以实现安全.简洁的状态管理,编写出优雅的异常安全的代码. 资源管理 RAII是C++的发明者Bjarne Stro ...
- placement new--《C++必知必会》 条款35
placement new是重载operator new的一个标准.全局的版本,它不能被自定义的版本代替(不像普通的operator new和operator delete能够被替换成用户自定义的版本 ...
- Bootstrap单按钮的下拉菜单
简介 把任意一个按钮放入 .btn-group 中,然后加入适当的菜单标签,就可以让按钮作为菜单的触发器了. 插件依赖 按钮式下拉菜单依赖下拉菜单插件 ,因此需要将此插件包含在你所使用的 Bootst ...
- mysql_escape_string — 转义一个字符串用于 mysql_query
string mysql_escape_string ( string $unescaped_string ) 本函数将 unescaped_string 转义,使之可以安全用于 mysql_quer ...
- smart基础
主要是libs里面的smarty类,和init.inc.php配置文件 剩下的是php文件夹.模板文件夹,临时文件夹.缓存文件夹.配置文件夹.插件文件夹 调用main文件夹中的php文件,通过libs ...
- Kylo 入坑记
一.概述 Kylo,作为一个基于 Spark 和 NiFi 的开源数据湖编排框架,解决对数据湖获取.治理.感知和技术支持等诸多问题.Kylo 将数据湖的很多功能自动化,包括数据接入.准备.分析发现.P ...
- Centos 7 无法上网的解决办法
首先,鼠标右击桌面,点击“在终端中打开”. 然后如下图所示,输入:su,按回车后输入自己的root密码:注意,输密码的时候密码区域并不显示任何东西哦,自己输错了就多按几次backspace就行 ...
- 38初识xml
XML(可扩展标记语言)是一种用于记录多种数据类型的标记语言.使用XML可以将各类型的文档定义为容易读取的格式,便于用户读取.而且,在应用程序中使用XML,可以轻松实现数据交换. QT中提供两种访问X ...
- Python 安装pytz
1. https://pypi.org/project/pytz/#files 2. 下载上图标黄的文件, 3. pip install 4. from pytz import ...
- 对Java平台的理解
1) Java是一种面向对象的语言(封装,继承,多态),最显著的特性有两个方面: ----书写一次,到处运行(Write once,run anywhere) 能够非常容易的获得跨平台的能力 --- ...