首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >指向结构的数据结构

指向结构的数据结构
EN

Stack Overflow用户
提问于 2021-03-13 23:29:05
回答 1查看 35关注 0票数 0

我想从头开始构建一个指向C++中的结构的优先级队列。我知道使用priority_queue <struct_name> pq的STL库可以很容易地做到这一点。但是,我对使用数组自己构建它很感兴趣。我最初的方法是为整数构建一个Min堆,并最终修改代码,使数组指向结构。为了给你更多的背景信息,这里是我之前写的代码。

代码语言:javascript
复制
#include<iostream>

#define MAX_SIZE 100
using namespace std;

struct cell{
    int x, y;
    int depth;
    
    // Constructor
    cell(int x, int y, int depth):
        x(x), y(y), depth(depth) {}
    
    
};

class minHeap{
private:
    // Current size of heap
    int size;
    // Max size of heap
    int capacity;
    // Storing elements as an array
    struct cell* heap = new struct cell[MAX_SIZE];
    // Returns the parent index
    int parent(int i){
        return (i-1)/2;
    }
    // Returns left child
    int leftChild(int i){
        return 2*i + 1;
    }
    // returns right child
    int rightChild(int i){
        return 2*i + 2;
    }
public:
    // Constructor
    minHeap(int capacity){
        size = 0;
        this->capacity = capacity;
        
    }
    
    
    void insert(struct cell node){
        if(size == capacity){
            cout<<"Heap is FULL"<<endl;
            return;
        }
        // Increase the amount of elements in the heap
        size++;
        
        // Insert the element at the end of heap
        heap[size-1] = node;
        
        // Maintain heap invariance
        int i = size - 1;
        while(i!=0 && greater(heap[parent(i)], heap[i])){
            struct cell temp = heap[i];
            heap[i] = heap[parent(i)];
            heap[parent(i)] = temp;
            i = parent(i);
        }
    }
    
    void heapify(int i){
        int left = leftChild(i);
        int right = rightChild(i);
        int smallest = i;
        
        // Find the smallest element of the three
        if((left < size) && greater(heap[left], heap[smallest])){
            smallest = left;
        }
        if((right < size) && (greater(heap[right], heap[smallest]))){
            smallest = right;
        }
        // If the smallest is left or right, keep heapifying
        if(smallest != i){
            struct cell temp = heap[i];
            heap[i] = heap[smallest];
            heap[smallest] = temp;
            heapify(smallest);
        }
        
    }
    
    int extractMin(){
        // Check is heap is empty
        if(size == 0){
            cout<<"Heap is empty"<<endl;
            return -1;
        }
        else if(size == 1){
            size--;
            return heap[0].depth;
        }
        else{
            struct cell root = heap[0];
            heap[0] = heap[size-1];
            size--;
            heapify(0);
            
            return root.depth;
        }
    }
    
    void printHeap(){
        int power = 0;
        int value = 0;
        for(int i=0; i<size; i++){
            if(i == value){
                cout<<endl;
                power += 1;
                value += (1<<power);
            }
            cout<<heap[i].depth<<" ";
        }
        cout<<endl;
    }
};

bool greater(struct cell cell1, struct cell cell2){
        if(cell1.depth == cell2.depth){
            if(cell1.x != cell2.depth){
                return (cell1.x < cell2.x);
            }
            else return cell1.y < cell2.y;
        }
        return cell1.depth < cell2.depth;
    }

int main(){
    minHeap m1(15);
    return 0;
}

然而,在这个实现之后,我遇到了一些错误。我在下面列出了它们:

代码语言:javascript
复制
Heap.cpp:24:46: error: no matching function for call to 'cell::cell()'
  struct cell* heap = new struct cell[MAX_SIZE];
                                              ^
Heap.cpp:11:2: note: candidate: cell::cell(int, int, int)
  cell(int x, int y, int depth):
  ^~~~
Heap.cpp:11:2: note:   candidate expects 3 arguments, 0 provided
Heap.cpp:6:8: note: candidate: constexpr cell::cell(const cell&)
 struct cell{
        ^~~~
Heap.cpp:6:8: note:   candidate expects 1 argument, 0 provided
Heap.cpp:6:8: note: candidate: constexpr cell::cell(cell&&)
Heap.cpp:6:8: note:   candidate expects 1 argument, 0 provided
Heap.cpp: In member function 'void minHeap::insert(cell)':
Heap.cpp:59:24: error: missing template arguments before '(' token
   while(i!=0 && greater(heap[parent(i)], heap[i])){
                        ^
Heap.cpp: In member function 'void minHeap::heapify(int)':
Heap.cpp:73:30: error: missing template arguments before '(' token
   if((left < size) && greater(heap[left], heap[smallest])){
                              ^
Heap.cpp:76:32: error: missing template arguments before '(' token
   if((right < size) && (greater(heap[right], heap[smallest]))){

我对C++比较陌生,所以我不确定我是否给出了所有必要的信息。如果我能提供更多的细节,请让我知道。

EN

回答 1

Stack Overflow用户

发布于 2021-03-14 02:42:34

因为cell有一个带参数的构造函数,但没有列出默认的(无参数)构造函数,所以您会得到第一个错误。

您需要创建一个默认构造函数,或者让您已有的构造函数在其参数上采用默认值。

修复它,看看有多少其他错误消失了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66615224

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档