zoukankan      html  css  js  c++  java
  • sicily数据结构-队列

    由于半个学期的数据结构都没学,期末开始慌张,开始敲其他班数据结构的题,发现连打个队列都成问题了,原题如下; 请完成以下队列类的实现:(请注意数组实现应该为循环数组)
    打吧。
    一开始的想法是将rear 和front无限延伸,然后在具体取数组的数值时进行取模处理,从而达到逻辑上循环数组的效果,但后来发现同学的代码不是这样过了,觉得我自己的有很大的问题,int类型本身就是有限的,不可能无限延伸,这样循环数组即使有限我的代码也会因为溢出出现问题,第二点,我将队列空的情况想成rear-front==-1,这样在我一开始的想法下是没错的,但用同学的想法(每一次对队列进行增减操作都将rear和front设到正确的位置)就有很大问题,比如在中间rear-front==-1就不知道是满了还是0,所以我变成了rear-front==0为空的判别条件
    最终代码如下:
    #include
    using namespace std;
    enum ErrorCode {
    success,
    underflow,
    overflow
    };
    const int maxQueue = 100;
    template
    class MyQueue {
    public:
    MyQueue() : front(0), rear(0) {}
    // 判断队列是否为空
    bool empty() const {return rear == front;}
    // 入队操作
    ErrorCode append(const QueueEntry &item) {
    if ((rear + 1) % maxQueue == front) return overflow;
    else {
    entry[rear] = item;
    rear = (rear + 1) % maxQueue;
    return success;
    }
    }
    // 出队操作
    ErrorCode serve() {
    if (rear == front) return underflow;
    else {
    front = (front + 1) % maxQueue;
    return success;
    }
    }
    // 获取队头元素
    ErrorCode retrieve(QueueEntry &item) const {
    if (rear == front) return underflow;
    else {
    item = entry[front];
    return success;
    }
    }
    // 判断队列是否已满
    bool full() const {
    return (rear + 1) % maxQueue == front;
    }
    // 获取队列已有元素个数
    int size() const {
    return (rear - front + maxQueue) % maxQueue;
    }
    // 清除队列所有元素
    void clear() {
    while (rear != front) {
    serve();
    }
    }
    // 获取队头元素并出队
    ErrorCode retrieve_and_serve(QueueEntry &item) {
    if (rear == front) return underflow;
    else {
    item = entry[front];
    front = (front + 1) % maxQueue;
    return success;
    }
    }
    void print() {
    for (int i = front; i < rear; i++) {
    cout << entry[i] << ' ';
    }
    cout << endl;
    cout << 'size' << size() << endl;
    cout << 'fulll?' << full() << endl;
    cout << 'empty?' << empty() << endl;
    }
    private:
    int front; // 队头下标
    int rear; // 队尾下标
    QueueEntry entry[100]; // 队列容器
    };
    第二题是根据队列进行一个游戏,题如下

    sicily数据结构复(yu)习;X


    本来用队列逻辑很清晰的做出来,按找游戏次序出列,入列,然后每个节点增加一个位置属性,输出就可以。懒癌复发,还是自己想其他方法作罢:
    #include
    using namespace std;
    struct node {
    int select;
    int content;
    };
    int main() {
    int t;
    cin >> t;
    while (t--) {
    int n;
    cin >> n;
    node num[27];
    int s = 0;
    for (int i = 0; i < 27; i++) {
    num[i].select = 0;
    num[i].content = 0;
    }//初始化
    int j = 0;
    for (int i = 0; i < n; i++) {
    while(num[j].select != 0) j++;//每一次将j调到正确的位置,即上一次j之后的第一个未被选中的位置
    int step = i;
    while (step--) {
    if (num[(j + 1) % n].select == 0) {}
    else step++;
    j = (j + 1) % n;
    }//走未被选中的位置走step步
    num[j].content = i + 1;
    num[j].select = 1;
    }
    for (int i = 0; i < n; i++) {
    cout << num[i].content << ' ';
    }
    cout << endl;
    }
    return 0;
    }

  • 相关阅读:
    Linux命令: 向文件写内容,编辑文件,保存文件,查看文件,不保存文件
    SQL: 左连接,右连接,内连接,左外连接,右外连接,完全连接
    Python: 没有switch-case语句
    Python:键盘输入input
    Python: 猴子分桃。海滩上有一堆桃子,五只猴子来分。
    error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools
    Scrapy安装
    Linux: 回到根目录cd /
    怎么查看是否安装Scrapy
    虚拟环境Scrapy安装
  • 原文地址:https://www.cnblogs.com/eggplant-is-me/p/6720173.html
Copyright © 2011-2022 走看看