2016/11/20

来源:互联网 发布:java游戏圈钱 编辑:程序博客网 时间:2024/05/16 10:20

1631-5 黄加勉 <2016.11.20> 【连续第50天总结】

A.今日任务

1.队列(100%);2.蓝桥杯试题(40%)

B.具体内容

1.队列其实也是一个数组,不过是首尾相连  当插入新元素时,队尾索引后移一位,即m_iTail++  当队首出队时队首索引后移一位,即m_iHead++2.因为当队首出队,而新元素入队后后会占用原来队首的空间    比如一个容量为4的队列,当队列填满且队首出队并且新入队元素时,新元素要指向m_pQueue[0]    而此时因为m_iTail++,所以队列指针索引变成4,发生了越界,因此需要把m_iHead取m_iCapacity的模  同理,新元素出队时也要把索引取容量的模;3.当遍历队列时,队列指针的索引应该是队首指针索引加上遍历因子,再取容量的模:
/*MyQueue.h*/#pragma onceclass MyQueue{public:    MyQueue(int capacity);  //初始化队列    ~MyQueue();  //销毁队列    void clearQueue();  //清空队列    bool QueueEmpty();  //判空队列    bool QueueFull();  //判满队列    int get_QueueLength();  //获取队列长度    bool enQueue(int element);  //新元素入队    bool deQueue();  //队首出队    void TravelQueue();  //遍历队列private:    int *m_pQueue;  //队列指针    int m_iHead; //队首索引    int m_iTail;  //队尾索引    int m_iCapacity;  //队列容量    int m_iLength;  //队列长度};/*MyQueue.cpp*/#include "MyQueue.h"#include <iostream>using namespace std;MyQueue::MyQueue(int capacity){    cout << "MyQueue()" << endl;    m_iCapacity = capacity;    m_pQueue = new int[m_iCapacity];    if (m_pQueue == NULL)  //判断分配内存空间是否成功    {        cout << "allocate memory space error!" << endl;        system("pause");    }    clearQueue();}MyQueue::~MyQueue(){    cout << "~MyQueue()" << endl;    delete[] m_pQueue;    m_pQueue = NULL;}void MyQueue::clearQueue(){    m_iHead = m_iTail = m_iLength = 0;}bool MyQueue::QueueEmpty(){    return m_iLength == 0 ? true : false;}bool MyQueue::QueueFull(){    return m_iLength == m_iCapacity ? true : false;}int MyQueue::QueueLength(){    return m_iLength;}bool MyQueue::enQueue(int element){    if (QueueFull()) return 0;    m_pQueue[m_iTail] = element;    m_iTail++;    m_iTail %= m_iCapacity;  //队尾索引回溯    m_iLength++;    return 1;}bool MyQueue::deQueue(){    if (QueueEmpty()) return 0;    m_iHead++;    m_iHead %= m_iCapacity;  //队头索引回溯    m_iLength--;    return 1;}void MyQueue::TravelQueue(){    if (QueueEmpty()) cout << "NULL";    for (int i = 0; i < m_iLength; i++)    {        cout << m_pQueue[(m_iHead + i) % m_iCapacity] << " ";    }    cout << endl;}/*Main.cpp*/#include <iostream>#include <math.h>#include "MyQueue.h"using namespace std;int main(){    int i, temp, n;    char x;    MyQueue *p = new MyQueue(10);    cout << endl;    cout << "type in element's number : ";    cin >> n;    while (1)    {        cout << endl;        cout << "type in " << n << " numbers : ";        p->clearQueue();        for (i = 1; i <= n; i++)  //插入元素        {            cin >> temp;            p->enQueue(temp);        }        while (1)  //是否队首出队        {            cout << "deelement ? (y/n) : ";            cin >> x;            if (x == 'y')            {                if (!p->deQueue())                {                    cout << endl;                    cout << "########## Error code 233 : queue's empty! ##########" << endl;                    cout << endl;                    break;                }            }            else            {                break;            }        }        cout << "reset queue ? (y/n) : ";  //是否手动清空队列        cin >> x;        if (x == 'n')  //打印队列属性        {            cout << endl;            cout << "if empty ?         " << p->QueueEmpty() << endl;            cout << "if full ?          " << p->QueueFull() << endl;            cout << "queue's length :   " << p->get_QueueLength() << endl;            cout << "queue's element :  ";            p->TravelQueue();            cout << endl;        }    }    //销毁队列    delete p;    p = NULL;    system("pause");    return 0;}
4.蓝桥杯的题做了一些,感觉主要是以算法为主  感觉这个在线测试有bug,只能用它的方法解题,不同方法但是结果一样也不行,打个endl都算错!!!  还有同一个判断回文数的程序分别用c、c++、java写了一遍,内存使用分别为836KB、944KB、**18.77MB**,你没看错,是**MB**!!

java c++大法好

因此 还要学习一个

5.这个markdown挺有意思

C.明日任务
1.栈
2.蓝桥杯试题

0 0