【问题标题】:Struct and class for queue队列的结构和类
【发布时间】:2021-04-05 04:02:36
【问题描述】:

当我构建这段代码时,它不会打印出任何东西。这段代码有什么问题,我参考了网上的一段代码,它使用了一个结构来声明队列。但我只是想使用类。使用类的对象有什么问题吗?这段代码还有哪些问题?

#include<bits/stdc++.h>
using namespace std;

    class Queue{
    public:
        int front, rear, size;
        unsigned capacity;
        int* array;
    };
    
    Queue* createQueue(unsigned capacity){
        Queue* queue = new Queue;
        queue->front = queue->size = 0;
        queue->capacity = capacity;
        queue->rear = queue->capacity-1;
        queue->array = new int[queue->capacity];
    }
    
    int isEmpty(Queue* queue){
        return (queue->size==0);
    }
    
    int isFull(Queue* queue){
        return (queue->size==queue->capacity);
    }
    
    void enqueue(Queue* queue, int item){
        if(isFull(queue)){
            return;
        }else{
            queue->rear = (queue->rear+1)%queue->capacity;
            queue->size = queue->size+1;
            queue->array[queue->rear] = item;
            cout << item << " enqueued to queue\n";
        }
    }
    
    int dequeue(Queue* queue){
        if(isEmpty(queue)){
            return NULL;
        }else{
            int temp = queue->array[queue->front];
            queue->front = (queue->front+1)%queue->capacity;
            queue->size = queue->size-1;
            return temp;
        }
    }
    
    int front(Queue* queue){
        if(isEmpty(queue)){
            return 0;
        }else{
            return queue->array[queue->front];
        }
    }
    
    int rear(Queue* queue){
        if(isEmpty(queue)){
            return NULL;
        }else{
            return queue->array[queue->rear];
        }
    }
    
    int main(){
        Queue queue;
        Queue* ptr = &queue;
        enqueue(ptr, 10);
    }

【问题讨论】:

  • 你打算用createQueue 做点什么吗?我很大的暗示,有些事情出轨了,这个函数似乎注定要...... 创建一个队列,但从未在任何这段代码中使用过。除了该函数中的错误(它从不返回值),现在Queue queue; 默认构造一个Queue,它从不初始化任何成员变量以确定值。因此,像isFull 中的return (queue-&gt;size==queue-&gt;capacity) 这样的后续表达式依赖于不确定的值,并调用未定义的行为。从字面上看,这看起来像是一个糟糕的 C 队列端口。
  • 感谢您的评论。我错过了 createQueue 来初始化成员的功能。非常感谢。
  • 是否有一些与 C 库兼容的要求,或者是否有任何其他原因不制作除 maincreateQueue 成员函数之外的所有函数并移动 createQueue 的逻辑给构造函数?

标签: c++ algorithm data-structures queue


【解决方案1】:

很多东西:

  • 您公开了所有Queue 类成员变量,违背了整个想法的封装。 (不要将您的会员暴露给其他人)
  • 最好不要对大小或容量使用无符号类型。无符号算术并不总是按照您期望的方式运行。
  • 你应该用类型封装函数。 IE。 enqueue 应该是 Queue 的成员函数。 (至少如果你想使用 OO)
  • 不要从谓词返回int。使用bool
  • createQueue 被定义为返回 Queue*,但是,您不会返回任何内容。那是未定义的行为。
  • dequeue 可以返回NULLNULL 用于空指针。但是该函数被定义为返回一个int,所以这是错误的。此外,在 C++ 中,我们使用nullptr。如果您有条件地想返回一些东西,请使用std::optional
  • 您正在处理一个原始指针 (array),但没有定义析构函数、复制/移动构造函数、复制/移动赋值运算符。所以你会有很多内存泄漏。 use the rule of five。更好的是:使用std::vector

整个事情可能看起来像(需要更多测试)

#include <vector>
#include <iostream>
#include <optional>

class Queue{
private:
    int capacity;
    std::vector<int> data{};
public:
    Queue(int capacity)
    : capacity(capacity)
    {}

    bool isEmpty(){
        return data.size()==0;
    }

    bool isFull(){
        return data.size()==capacity;
    }

    bool enqueue(int item){
        if (isFull()){
            return false;
        } else {
            data.push_back(item);
            std::cout << item << " enqueued to queue\n";
            return true;
        }
    }

    std::optional<int> dequeue(){
        if (isEmpty()) {
            return std::nullopt;
        } else {
            int retval = data.front();
            data.erase(data.begin());
            return retval;
        }
    }

    std::optional<int> front(){
        if (isEmpty()) {
            return std::nullopt;
        }else{
            return data.front();
        }
    }

    std::optional<int> back(){
        if (isEmpty()) {
            return std::nullopt;
        }else{
            return data.back();
        }
    }
};

int main(){
    Queue queue(5);
    queue.enqueue(10);
    std::cout << "removed from queue: " << queue.dequeue().value() << '\n';
    std::cout << "Queue contains nothing: " << (queue.back().has_value() == false?"yes":"no") << '\n';
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-08
    • 2016-09-25
    • 1970-01-01
    • 2022-06-10
    相关资源
    最近更新 更多