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)的更多相关文章

  1. CSC 172 (Data Structures and Algorithms)

    Project #3 (STREET MAPPING)CSC 172 (Data Structures and Algorithms), Spring 2019,University of Roche ...

  2. CSIS 1119B/C Introduction to Data Structures and Algorithms

    CSIS 1119B/C Introduction to Data Structures and Algorithms Programming Assignment TwoDue Date: 18 A ...

  3. 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 ...

  4. 剪短的python数据结构和算法的书《Data Structures and Algorithms Using Python》

    按书上练习完,就可以知道日常的用处啦 #!/usr/bin/env python # -*- coding: utf-8 -*- # learn <<Problem Solving wit ...

  5. [Data Structures and Algorithms - 1] Introduction & Mathematics

    References: 1. Stanford University CS97SI by Jaehyun Park 2. Introduction to Algorithms 3. Kuangbin' ...

  6. 学习笔记之Problem Solving with Algorithms and Data Structures using Python

    Problem Solving with Algorithms and Data Structures using Python — Problem Solving with Algorithms a ...

  7. Trainning Guide, Data Structures, Example

    最近在复习数据结构,发现这套题不错,题目质量好,覆盖广,Data Structures部分包括Example,以及简单,中等,难三个部分,这几天把Example的做完了, 摘要如下: 通过这几题让我复 ...

  8. Python Tutorial 学习(五)--Data Structures

    5. Data Structures 这一章来说说Python的数据结构 5.1. More on Lists 之前的文字里面简单的介绍了一些基本的东西,其中就涉及到了list的一点点的使用.当然,它 ...

  9. [译]The Python Tutorial#5. Data Structures

    [译]The Python Tutorial#Data Structures 5.1 Data Structures 本章节详细介绍之前介绍过的一些内容,并且也会介绍一些新的内容. 5.1 More ...

随机推荐

  1. 《大话设计模式》ruby版代码:建造者模式

    需求: 画一个小人,有头,有身体,两手两脚即可. 初始代码: # -*- encoding: utf-8 -*- #小人一 puts '这是第一个小人' puts '小人一:头' puts '小人一: ...

  2. appscan 9.0.3.10 版本下载

    http://download4.boulder.ibm.com/sar/CMA/RAA/07ukf/0/ 其他版本下载 https://www.cnblogs.com/hua198/p/100447 ...

  3. ubuntu16.04(64位)安装 Drcom

    一 : 下载DrClient(DrcomAndPPOE) 解压   下载地址 https://www.baidu.com/s?wd=DrClient(DrcomAndPPOE)&rsv_spt ...

  4. 分布式session的管理

    在分布式架构或微服务架构下,必须保证一个应用服务器上保存Session后,其它应用服务器可以同步或共享这个Session,可能会出现在A1系统登录后创建并保存Session,再次发起请求,请求被转发到 ...

  5. .net core 2.2 & Mongodb

    .net core 2.2 API项目中使用Mongodb 简单的CRUD封装 创建FoodPlan.Core 项目 创建IEntityBase.cs 接口约束 创建Single.cs 实体 IEnt ...

  6. shell指令操作memcached

    shell指令操作memcached,可以用来直接测试memcached. 初始值为1000 #set test-value =1000printf "set test-value 0 0 ...

  7. 一个简单清晰的Redis操作类-php

    <?php /** * redis处理的二次封装 * */ class Redis{ private $_redis; private $_config; public function __c ...

  8. Python3.x:os.chdir(改变当前路径方法)介绍

    Python3.x:os.chdir(改变当前路径方法)介绍 1,os.chdir() import os os.chdir(r'C:\python36\test_chdir') 说明:chdir() ...

  9. .net Core 发布并布署到Iis

               配置 Program.cs代码 namespace WebApplication8 { public class Program { public static void Mai ...

  10. UVa 12169 不爽的裁判

    https://vjudge.net/problem/UVA-12169 题意: 输入T,x1,x2,x3,...,x2T-1,输出x2,x4,...,x2T. 递推公式为xi=(axi-1+b)mo ...