循环队列,类之间的互动

来源:互联网 发布:西门子s7 200编程软件 编辑:程序博客网 时间:2024/05/17 06:07

好了,是这周的更新时间了,这一次我们继续分析分析学校vmatrix上的作业题
题目及相关源码下载地址:download

这一周,算法就不那么考虑了,都是涉及c++的语言特性及一些简单的数据结构

1.循环队列:

这周题目以数组形式考察循环队列,而我感觉,真正循环队列在数组中使用,关键就是isEmpty() 和isFull()的判断,以及push,pop机制。
题目如下
题目名称 : Stack And Queue(Eden)

时间限制 : 1000 ms

空间限制 : 32 MB
编译器类型 : g++

Introduction
Welcome to the c++ D&A (data structure and algorithm) topic. This topic will lead us to achieve a set of basic data structures and algorithm based on the c++ features. This time we are going to implement a simple stack as well as a simple queue using encapsulation in c++.
Knowledge
stack
In computer science, a stack is an abstract data type that serves as a collection of elements, with two principal operations: push, which adds an element to the collection, and pop, which removes the most recently added element that was not yet removed. The order in which elements come off a stack gives rise to its alternative name, LIFO (for last in, first out). Additionally, a peek operation may give access to the top without modifying the stack.
queue
In computer science, a queue is a particular kind of abstract data type or collection in which the entities in the collection are kept in order and the principal (or only) operations on the collection are the addition of entities to the rear terminal position, known as enqueue, and removal of entities from the front terminal position, known as dequeue. This makes the queue a First-In-First-Out (FIFO) data structure. In a FIFO data structure, the first element added to the queue will be the first one to be removed. This is equivalent to the requirement that once a new element is added, all elements that were added before have to be removed before the new element can be removed.
Requirement
For Stack:
Your job is to implement the following functions with some other limits:

// you can not push an element when the storage is full
void push(int);

// you can not pop an element when the storage is empty
void pop(void);

// when the stack is empty, you should return 0
int peek(void);

bool isEmpty(void);

bool isFull(void);

void clear(void);

For Queue:

Your job is to implement the following functions with some other limits:

// you can not push an element when the storage is full
void push(int);

// you can not pop an element when the storage is empty
void pop(void);

// when the queue is empty, you should return 0
int front(void);

// when the queue is empty, you should return 0
int back(void);

bool isFull(void);

bool isEmpty(void);

void clear(void);

Deep Thinking and Discuss

*1. Why the class queue initialize the storage’s size to MAXSIZE+1 rather than MAXSIZE?

*2. What’s the disadvantages of the stack and queue in this question?
More
If you have any ideas or thoughts about C++ D&A. Just leave a message below or send me a mail.
Next we will have some dynamic storage practice. I recommend you to review so basic knowledge about pointer and memory allocation about c that you learned it last semester.
Hint
Queue 中初始化的数组的大小为 maxsize+1,真正存储的最大容量仍然为MAX_SIZE. 也就是 capability == MAX_SIZE. 思考这样初始化的用意(循环队列)

2.类之间的互动:

这里还不涉及什么友元那些东西,关键就是利用各种组件,也很水啦。
题目名称 : The Werewolves of Miller’s Hollow

时间限制 : 1000 ms

空间限制 : 32 MB
编译器类型 : g++

Task

实现一个简单的“狼人游戏”通知机制
Details

角色有:村民,狼人,预言家,女巫,猎人(Uninitialized只用于默认构造函数)
法官呼叫“狼人/预言家/女巫/猎人”,相应角色进行回答
法官呼叫“村民”,全体角色回答
Hints

objA.on(objB)表示将objB作为objA的监听者(事件订阅者)
知识点:

观察者模式类与类的关系:关联C++ enum类静态变量

One more step

将构造函数和初始化操作(Player();和void init(std::string name, Role role);)分离出来有何优缺点?judge.hpp中const Player*能改为const Player&吗?为什么?enum类型Role定义在Player类的public部分有何好处?Role为什么定义成enum类型?定义成string数组合适吗?kMaxCountOfPlayers为什么要作为类静态变量?如果类静态变量为double类型,还能在类定义中初始化吗?

Links

《Google cpp style guide》--3.2. 初始化C++ initialize static variables in class?
0 0