简体   繁体   English

用类实现的队列

[英]Queue implemented with classes

I recently came to queue data struture. 我最近来排队数据结构。 In order to practise and learn something new i decided to implement it without STL library. 为了练习和学习新东西,我决定在没有STL库的情况下实现它。 The thing is , i have quite hard time to actually tell difference between typical array , and queue. 问题是,我很难分辨出典型数组和队列之间的区别。

I defined simple class 我定义了简单的类

class Queue{

public:

    Queue();
    Queue(int);


    void enqueue(int);
    int dequeue();
    int  first_out() ;
    int  last_out() ;
    bool isEmpty();
    bool isFull();

private:

    int current;
    int maxi;
    int *arr;
    int frontt;
    int rear;

};

its constructor 它的构造函数

Queue::Queue(int maxo){
    this -> current = 0;
    this -> maxi = maxo;
    this -> arr = new int[maxi];
    this -> frontt = 0;
    this -> rear = 0;
}

enqueue method, which reallocate the array inside , when it tries to access index which is bigger than max index. enqueue方法,当它尝试访问大于最大索引的索引时,该方法重新分配内部的数组。

void Queue::enqueue(int a){
    if( this -> rear == this -> maxi){

        int *temp;
        int tmp = maxi;
        while( this -> rear >= this -> maxi){
            this -> maxi *= 2;
        }
        temp = new int[maxi];
        for( int i = 0; i < tmp ; i++){
            temp[i]=arr[i];
        }
        rear = maxi;
        delete[] arr;
        arr = temp;

    }
    this -> arr[rear++] = a;


}

Dequeue which increment front index. 出队哪个增量前索引。

int Queue::dequeue(){



return arr[frontt++];

    }
    int Queue::first_out(){

        return arr[frontt];

    }

    int Queue::last_out(){

        return arr[rear-1];

    }

    bool Queue::isEmpty(){

        if( this -> frontt == this -> rear){
            cout << "Queue is empty" << endl;
            return 1;
        }
        return 0;
    }
    bool Queue::isFull(){

        if( this -> frontt == (this -> rear+1) % this -> maxi){


            return 1;

        }
        return 0;
    }

Main function to test it 主要功能测试

Queue tst(5);
    cout << "Write numbers" << endl;
    int n;
    while( cin >> n){
        tst.enqueue(n);
    }
    tst.dequeue();
    cout << "The firt out element is " << tst.first_out() << endl;
    cout << "The last out element is " << tst.last_out() << endl;
    return 0;

My question is quite trivial. 我的问题很琐碎。 Is this how to implement queue? 这是如何实现队列? Can i understand it as queue is just generator/iterator of values? 我能理解它吗,因为队列只是值的生成器/迭代器? Why to use queue instead of array then? 为什么要使用队列而不是数组呢? Also what is the point of circle queue? 另外,圆形队列的要点是什么?

Thanks for answers. 感谢您的回答。

At least in C and C++, an array is defined in concrete terms. 至少在C和C ++中,数组是用具体术语定义的。 Specifically, it's defined in terms of memory layout--objects at consecutive memory locations. 具体来说,它是根据内存布局(连续内存位置的对象)定义的。 How you use it, what you do with it, etc., are determined entirely by that definition. 您如何使用它,如何使用它等等,完全取决于该定义。

A queue is defined much more abstractly: it has to support a couple of specific operations (adding and retrieving items) and an order relationship between the two (first in, first out). 队列的定义更加抽象:它必须支持几个特定的​​操作(添加和检索项目)以及两者之间的顺序关系(先进先出)。 A few secondary operations such as checking whether the queue is empty, or checking its current size are often provided as well (but aren't normally seen as part of the definition). 通常还会提供一些辅助操作,例如检查队列是否为空或检查队列的当前大小(但通常不将其视为定义的一部分)。

You can certainly use an array to implement a queue. 您当然可以使用数组来实现队列。 You can also use a linked list. 您也可以使用链接列表。 Although it may not make a whole lot of sense, you could also use a binary tree, a multi-way tree, a hybrid such as a linked list of arrays, or essentially anything else you can think of, as long as it supports the required operations. 虽然它可能没有了一大堆的道理,你也可以使用二叉树,多路树,混合如阵列,或基本的链表其他任何你能想到的,只要它支持所需的操作。

Going the other direction, you could use an array to implement not only a queue, but also a stack, a deque, a heap/priority queue, a circular buffer, or any number of other abstractions. 朝另一个方向发展,您不仅可以使用数组来实现队列,而且可以实现堆栈,双端队列,堆/优先级队列,循环缓冲区或任意数量的其他抽象。

As to your specific question of: "Is this how to implement a queue"? 至于您的特定问题:“这是如何实现队列”? I'd say in general, no. 我一般地说,不。 Just for one example, you're ignored the law of the big three, so assignment, copying, and destruction don't really work for your queue (but you haven't prevented assignment or copying either). 仅举一个例子,您就忽略了三巨头的法则,因此分配,复制和销毁对您的队列实际上并不起作用(但是您并未阻止分配或复制)。 You've also used new[] to allocate the queue's space, which constructs objects into that space immediately upon allocation. 您还使用了new[]来分配队列的空间,该空间在分配后立即将对象构造到该空间中。 That's fine for a queue of primitive objects like int s, but not so fine in general. 对于像int这样的原始对象队列来说,这很好,但是通常来说并不是那么好。 Likewise, your queue can print out a message in response to isEmpty() . 同样,您的队列可以打印出一条消息以响应isEmpty() Assuming it exists at all, isEmpty should just return a value to indicate whether the queue is empty or not--if a printed message is needed, that should be handled by other code. 假设它完全存在, isEmpty应该只返回一个值以指示队列是否为空-如果需要打印的消息,则应由其他代码处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM