Vj实验七队列的使用

A : 卡片游戏



要求

  1. 创建队列类,使用数组描述的循环队列
  2. 实现卡片游戏



描述

假设桌上有一叠扑克牌,依次编号为1-n(从上至下)。当至少还有两张的时候,可以进行操作:把第一张牌扔掉,然后把新的第一张(原先扔掉的牌下方的那张牌,即第二张牌)放到整叠牌的最后。输入n,输出最后剩下的牌。



格式



输入

一个整数n,代表一开始卡片的总数。



输出

最后一张卡片的值。



样例



输入

100



输出

72

 以0代替n,实现要求(因为用0代替n并不会对实验进行影响)

#include <iostream>



using namespace std;



template<class T>

class arrayQueue{

    public:

        arrayQueue(int initialCapacity = 10); // 构造函数

        ~arrayQueue(){delete[] queue;} // 析构函数

        void push(const T& theElement); // 插入

        void pop(); // 删除

        // bool empty(){return queueFront = queueBack;}

        int size()const{return (arrayLength + queueBack - queueFront) % arrayLength;}

        T &front(){ // 返回队首元素

            return queue[queueFront + 1 % arrayLength];

        }

        T &back(){ // 返回队尾元素

            return queue[queueBack];

        }

        T output(){

            while(size() != 1){

            pop();

            push(front());

            pop();

            }

            return front();

        }

        // void change1(){

        //     T* newQueue = new T[2*arrayLength]; // 分配空间

        //     int start = (queueFront + 1) % arrayLength;

        //     if(start < 2){ // 没有形成环

        //         copy(queue + start, queue + start + arrayLength - 1, newQueue);

        //     }

        //     else{ // 形成环

        //         copy(queue + start, queue + arrayLength, newQueue);

        //         copy(queue, queue + queueBack + 1, newQueue + arrayLength - start);

        //     }

        //     // 重新设置队首和队尾元素

        //     queueFront = 2 * arrayLength - 1;

        //     queueBack = arrayLength - 2;

        //     arrayLength = arrayLength * 2;

        //     delete[] queue;

        //     queue = newQueue;

        // }

    private:

        int arrayLength; // 长度

        int queueFront; // 头

        int queueBack; // 尾

        T *queue; // 队列

};



// 构造函数

template<class T>

arrayQueue<T>::arrayQueue(int initialCapacity){

    arrayLength = initialCapacity;

    queue = new T[arrayLength];

    queueFront = 0;

    queueBack = queueFront;

    for(int i = 0; i < initialCapacity; i++){

        queue[i] = i;

        // queueBack = (queueBack + 1) % arrayLength;

        // if((queueBack + 1) % arrayLength == queueFront){

        //     change1();

        // }

       

    }

}



// 插入函数

template<class T>

void arrayQueue<T>::push(const T& theElement){

    // if((queueBack + 1) % arrayLength == queueFront){

    //     change1();

    // }

    queueBack = (queueBack + 1) % arrayLength;

    queue[queueBack] = theElement;

}



// 删除函数

template<class T>

void arrayQueue<T>::pop(){

    queueFront = (queueFront+1) % arrayLength;

    queue[queueFront].~T();

}

int main(){

    int n;

    cin >> n;

    arrayQueue<int> q1(n);

    cout << q1.output() << endl;

    return 0;

}

 正常使用1-n实现

#include <iostream>



using namespace std;



template<class T>

class arrayQueue{

    public:

        arrayQueue(int initialCapacity = 10); // 构造函数

        ~arrayQueue(){delete[] queue;} // 析构函数

        void push(const T& theElement); // 插入

        void pop(); // 删除

        // bool empty(){return queueFront = queueBack;}

        int size()const{return (arrayLength + queueBack - queueFront) % arrayLength;}

        T &front(){ // 返回队首元素

            return queue[queueFront + 1 % arrayLength];

        }

        T &back(){ // 返回队尾元素

            return queue[queueBack];

        }

        void change1(){

            T* newQueue = new T[2*arrayLength]; // 分配空间

            int start = (queueFront + 1) % arrayLength;

            if(start < 2){ // 没有形成环

                copy(queue + start, queue + start + arrayLength - 1, newQueue);

            }

            else{ // 形成环

                copy(queue + start, queue + arrayLength, newQueue);

                copy(queue, queue + queueBack + 1, newQueue + arrayLength - start);

            }

            // 重新设置队首和队尾元素

            queueFront = 2 * arrayLength - 1;

            queueBack = arrayLength - 2;

            arrayLength = arrayLength * 2;

            delete[] queue;

            queue = newQueue;

        }

    private:

        int arrayLength; // 长度

        int queueFront; // 头

        int queueBack; // 尾

        T *queue; // 队列

};



// 构造函数

template<class T>

arrayQueue<T>::arrayQueue(int initialCapacity){

    arrayLength = initialCapacity;

    queue = new T[arrayLength];

    queueFront = 0;

    queueBack = queueFront;

    for(int i = 0; i < initialCapacity; i++){

        queue[i] = i;

        queueBack = (queueBack + 1) % arrayLength;

        if((queueBack + 1) % arrayLength == queueFront){

            change1();

        }

       

    }

}



// 插入函数

template<class T>

void arrayQueue<T>::push(const T& theElement){

    if((queueBack + 1) % arrayLength == queueFront){

        change1();

    }

    queueBack = (queueBack + 1) % arrayLength;

    queue[queueBack] = theElement;

}



// 删除函数

template<class T>

void arrayQueue<T>::pop(){

    queueFront = (queueFront+1) % arrayLength;

    queue[queueFront].~T();

}

int main(){

    int n;

    cin >> n;

    arrayQueue<int> q1(n);

    while(q1.size() != 1){

        q1.pop();

        q1.push(q1.front());

        q1.pop();

    }

    cout << q1.back() << endl;


    return 0;

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值