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 ...
随机推荐
- Linux系统——http协议原理
Web服务基础 用户访问网页基本流程 (1)在浏览器中输入域名,系统会查找系统本地的DNS缓存及hosts文件信息,查找是否存在域名对应的IP解析记录 (2)DNS解析域名为IP地址,系统会把浏览器的 ...
- 《零起点,python大数据与量化交易》
<零起点,python大数据与量化交易>,这应该是国内第一部,关于python量化交易的书籍. 有出版社约稿,写本量化交易与大数据的书籍,因为好几年没写书了,再加上近期"前海智库 ...
- uva10817 dijkstra
大白书P330 #include <iostream> #include <cstdio> #include <algorithm> #include <st ...
- Python3.x:bs4解析html基础用法
Python3.x:bs4解析html基础用法 代码: import urllib.request from bs4 import BeautifulSoup import re url = r'ht ...
- 使用cronolog工具给tomcat进行日志切割
关于cronolog的用法查看:https://www.freebsd.org/cgi/man.cgi?query=cronolog&apropos=0&sektion=0&m ...
- Kali视频学习21-25
Kali视频学习21-25 (21)密码攻击之在线攻击工具 一.cewl可以通过爬行网站获取关键信息创建一个密码字典. 二.CAT (Cisco-Auditing-Tool)很小的安全审计工具,扫描C ...
- 20155201 实验五《Java面向对象程序设计》实验报告
20155201 实验五<Java面向对象程序设计>实验报告 一.实验内容 1. 数据结构应用 2. 结对编程:利用IDEA完成网络编程任务,1人负责客户端,1人负责服务器 3. 密码结对 ...
- RPC框架yar安装
官方网站: http://pecl.php.net/package/msgpack http://pecl.php.net/package/yar 先安装 msgpack $ git clone ht ...
- 使用IDEA创建基于Gradle构建的JavaWeb项目
环境配置 jdk: 1.8 Gradle: 4.4.1 Tomcat: 9.0.0 One Step! 创建项目,初始化项目结构. 打开我们的IDEA,进入创建项目的界面,勾选Java,Web两个选项 ...
- 使用 if 语句
与很多编程语言一样,if 表达式用来处理逻辑条件.在 R 中,逻辑条件通常表达为某个表达式返回的单值逻辑向量.例如,我们可以写一个简单的函数 check_positive,如果输入一个正数则返回 1, ...