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 ...
随机推荐
- poj2954 Triangle
地址:http://poj.org/problem?id=2954 题目: Triangle Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- 470. Implement Rand10() Using Rand7() (拒绝采样Reject Sampling)
1. 问题 已提供一个Rand7()的API可以随机生成1到7的数字,使用Rand7实现Rand10,Rand10可以随机生成1到10的数字. 2. 思路 简单说: (1)通过(Rand N - 1) ...
- Python numpy有什么用?
NumPy is the fundamental package for scientific computing with Python.就是科学计算包. a powerful N-dimensio ...
- IBM究竟是一家怎样的公司
每次被问到这样的“简单”问题,我都很纠结: 这家公司,从创始至今已经积累了几十万种技术(2015年蝉联专利排行榜23年之久,仅2015年专利数7355项),开发了上万种产品(从银行的交易系统,到航空的 ...
- NOSQL数据库-Redis
官方提倡使用Linux版的Redis,所以官网值提供了Linux版的Redis下载,我们可以从GitHub上下载window版的Redis,具体链接地址如下: · 官网下载地址:http://redi ...
- 【postman】谷歌postman插件的基本选项含义
1.form-data: 就是http请求中的multipart/form-data,它会将表单的数据处理为一条消息,以标签为单元,用分隔符分开.既可以上传键值对,也可以上传文件.当上传的字段是文件 ...
- G_M_C_美食节
美食节 题解:学习了动态加边,可以说是进一步理解了网络流.具体思路就是考虑每一道菜,如果这是该位厨师最后一次做,那么等待时间就是做这道菜的时间,如果是倒数第二次做,就要两倍时间(目前做了一次,后面还有 ...
- 使用ntpdate工具校正linux服务器时间
当Linux服务器的时间不对的时候,可以使用ntpdate工具来校正时间. 安装:yum install ntpdate ntpdate简单用法: # ntpdate ip # ntpdate 210 ...
- UVa 11383 少林决胜(二分图最佳完美匹配)
https://vjudge.net/problem/UVA-11383 题意: 给定一个N×N矩阵,每个格子里都有一个正整数W(i,j).你的任务是给每行确定一个整数row(i),每列也确定一个整数 ...
- 面向对象之php多态
php是面向对象的脚本语言,而我们都知道,面向对象的语言具有三大特性:封装,继承,多态(接口的多种不同的实现方式即为多态). 封装是类的构建过程,php具有.php也具有继承的特性.唯独这个多态,ph ...