当前位置: 首页 > 知识库问答 >
问题:

C int PriorityQueue使用堆栈

越骏俊
2023-03-14

我刚刚开始学习C编程,为了锻炼我找到了这个任务。我必须使用动态、基于数组的整数堆栈编写PriorityQueue。这就是我到目前为止得到的。

#include <iostream>

using namespace std;


class PrioQueue
{
private:
    int *bottom_;
    int *top_;
    int size_;
public:
    PrioQueue(int n = 20){
        bottom_ = new int[n];
        top_ = bottom_;
        size_ = n;
    }

    int getSize(){ return size_; }

    void push(int c){
        if (!full()){
            *top_ = c;  
            top_++;

        }
        else{
            resize(size_ * 2);
            *top_ = c;  
            top_++;

        }
        SortPrioQueue();
    }

    void resize(int newSize){
        //Allocate new array and copy in data
        int *newArray = new int[newSize];
        memcpy(newArray, bottom_, size_ * sizeof(int));

        // Set the top to the new array
        top_ = newArray + (top_ - bottom_);

        // Delete old array
        delete[] bottom_;

        // Update pointers and size
        bottom_ = newArray;
        size_ = newSize;

        cout << "array has been resized" << endl;
    }

    void SortPrioQueue(){

        int swap = 0; //holding variable

        for (int i = 0; i < (size_ - 1); i++)
        {
            for (int j = (i + 1); j < size_; j++)
            {
                if (bottom_[i] > bottom_[j])
                {
                    swap = bottom_[i];
                    bottom_[i] = bottom_[j];
                    bottom_[j] = swap;
                }
            }
        }

    }


    int num_items() {
        return (top_ - bottom_);
    }
    int pop(){
        top_--;
        return *top_;
    }
    int full() {
        return (num_items() >= size_);
    }
    int empty() {
        return (num_items() <= 0);
    }
    void print(){
        cout << "Stack currently holds " << num_items() << " items: ";
        for (int *element = bottom_; element<top_; element++) {
            cout << " " << *element;
        }
        cout << "\n";
    }
    ~PrioQueue(){ // stacks when exiting functions
        delete[] bottom_;
    }
};
int main(){
    PrioQueue s(5);
    s.print(); cout << "\n";
    s.push(10); s.push(24); s.push(53); s.push(74); s.push(5);
    s.print(); cout << "\n";
    //s.SortPrioQueue();//if i call it here
    s.print(); cout << "\n";

    while (!s.empty()) s.pop();
    if (s.num_items() != 0) {
        cout << "Error: Stack is corrupt!\n";
    }
    s.print(); cout << "\n";
    // destructor for s automatically called
    system("pause"); // execute M$-DOS' pause command
    return 0;
}

提前感谢您的帮助。

共有1个答案

司英彦
2023-03-14

SortPrioQueue()应使用top\u-bottom(或num\u items())作为限制,而不是大小。

 类似资料:
  • 本文向大家介绍在C ++中正确使用堆栈和堆?,包括了在C ++中正确使用堆栈和堆?的使用技巧和注意事项,需要的朋友参考一下 堆栈-函数内部声明的所有变量将占用堆栈中的内存。因此,函数内的任何局部变量都位于堆栈中。 堆-这是程序的未使用内存,可用于在程序运行时动态分配内存。因此,如果我们希望某些东西的寿命比声明它的函数的寿命更长,则必须在堆上分配它。 示例 堆内存中的主要问题是碎片,而堆栈中更容易出

  • 关于这一点已经有很多问题(例如使用树实现堆),但是没有一个问题有一个公认的答案。所以,我在这里再问一次,让问题更清楚。< br >二叉树已经实现,并且二叉树的私有内部类包括 所以,我有元素,父,左孩子,右孩子的引用。 内部类包含每个获取器和设置器。 内部类正在实现

  • 对于堆排序,如果我们想按升序对数组排序,那么应该在最大堆还是最小堆中转换堆?

  • 问题内容: 使用Java中的链表实现堆栈的最佳方法是什么? 编辑:我将最好的定义为最有效的使用干净的代码。我已经使用数组来实现堆栈,但是对链接列表不熟悉,因此想知道是否有人可以帮助我实现类似于以下内容的内容: 编辑:如果有人感兴趣,这是链表的实现。 问题答案: 假设您真的想从头开始,而不是使用现有的完美堆栈实现之一,那么我建议您: 创建一个“ MyStack ”类,该类实现所需的任何接口(也许列出

  • 输入=堆栈数 但是你只能弹出输入,你不能推到它。输出也是另一个堆栈,你可以返回并推到它,但不能弹出 所以如果 由于您无法在中返回到

  • 问题内容: 我有一个堆栈A,我想创建一个与堆栈A相同的堆栈B。​​我不希望堆栈B只是指向A的指针- 我实际上是想创建一个包含相同元素的新堆栈B堆栈A的顺序与堆栈A相同。堆栈A是字符串的堆栈。 谢谢! 问题答案: 只需使用Stack类的clone()方法(它实现Cloneable)。 这是一个使用JUnit的简单测试用例: 编辑: tmsimont:这会为我创建“未经检查或不安全的操作”警告。有什么