6-1 Deque(25 分)Data Structures and Algorithms (English)

时间:2022-08-22 13:25:10

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 &lpar;Data Structures and Algorithms&rpar;

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

  2. CSIS 1119B&sol;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. &lbrack;Data Structures and Algorithms - 1&rsqb; Introduction &amp&semi; 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&comma; Data Structures&comma; Example

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

  8. Python Tutorial 学习&lpar;五&rpar;--Data Structures

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

  9. &lbrack;译&rsqb;The Python Tutorial&num;5&period; Data Structures

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

随机推荐

  1. ASP&period;NET实现验证码

    using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...

  2. sql server添加列

    alter table 表名 add 列名 数据类型如:alter table student add sex char(2)

  3. 【转】Class&period;forName&lpar;&rpar;用法详解

    ref: http://blog.csdn.net/kaiwii/article/details/7405761 主要功能 Class.forName(xxx.xx.xx)返回的是一个类 Class. ...

  4. maven第7章生命周期和插件

    maven插件用到哪些思想? 7.7 从命令行调用插件 目标前缀和插件前缀是一个意思. 在本地搭建maven环境,熟悉maven的环境.

  5. AJAX 在手机上用时

    Response.ContentType = "text/html; text/plain; charset=UTF-8";

  6. 1712&colon; &lbrack;Usaco2007 China&rsqb;Summing Sums 加密

    1712: [Usaco2007 China]Summing Sums 加密 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 338  Solved: 12 ...

  7. 开源视频平台:ViMP

    ViMP是一个开源的视频平台,可以用于建立自己的视频门户.可以用于VoD系统,网络学习系统,企业内部视频系统的搭建. 这一阵子一直在研究网络视频平台.发现这类的开源系统相对来说还是比较少的,因此在发现 ...

  8. 20175234 2018-2019-2 《Java程序设计》第五周学习总结

    20175234 2018-2019-2 <Java程序设计>第五周学习总结 接口实现流程简述 interface定义接口 接口只能定义常量和方法,方法不能是具体的 实现接口时,需类来完成 ...

  9. Query实例的ajax应用之二级联动的后台是采用php来做的

    jQuery实例的ajax应用之二级联动的后台是采用php来做的,前台通过jquery的ajax方式实现二级联动数据库表设计 csj_trade id int(11) auto_increment  ...

  10. 编写高质量代码改善C&num;程序的157个建议——建议146:只对外公布必要的操作

    建议146:只对外公布必要的操作 那些没有必要公开的方法和属性要声明成private.如果需要公开的方法和属性超过9个,在VS默认的设置下,就需要滚屏才能显示在Intellisense中,如图: Sa ...