程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了指向结构体的数据结构大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决指向结构体的数据结构?

开发过程中遇到指向结构体的数据结构的问题如何解决?下面主要结合日常开发的经验,给出你关于指向结构体的数据结构的解决方法建议,希望对你解决指向结构体的数据结构有所启发或帮助;

我想从头开始构建一个指向 C++ 结构的优先级队列。我知道这可以通过 priority_queue <struct_name> pq 使用 STL 库轻松完成。但是,我有兴趣使用数组自己构建它。我最初的方法是为整数构建一个最小堆,并最终修改代码使数组指向一个结构。为了给你更多的上下文,这是我到目前为止编写的代码。

#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(){
        // @R_618_10943@k 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;
}

但是,我在执行此操作后遇到了一些错误。我在下面列出了它们:

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 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++ 比较陌生,所以我不确定我是否提供了所有必要的信息。如果我能提供任何其他详细信息,请告诉我。

解决方法

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

您要么需要创建一个默认构造函数,要么使您拥有的构造函数的参数采用默认值。

解决这个问题,看看有多少其他错误消失了。

大佬总结

以上是大佬教程为你收集整理的指向结构体的数据结构全部内容,希望文章能够帮你解决指向结构体的数据结构所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: