c++ - Access Violation error when I debug my priority queue program -
i getting access violation error in code when try run it. program priority queue inserts value , prints heap after each insertion , min extract.
header file:
#pragma once /* header file priority queue class */ #ifndef priorityqueue_h #define priorityqueue_h class priorityqueue { private: int size; int *data; public: static const int capacity = 50; priorityqueue();//constructor ~priorityqueue();//destructor int getparent(int index); int getleftchild(int index); int getrightchild(int index); void swap(int &, int &); void insert(int item); //enqueue - heap_insert void printarray(int []); void heapify(int index); //remove , return smallest item in priority queue int extractmin();//dequeue bool empty() const; int min() const; //return smallest item }; #endif
main code:
#include <iostream> #include "priorityqueue.h" using namespace std; int main() { priorityqueue myqueue; //class object if (myqueue.empty()) cout << "my priority queue empty\n" << endl; //prompt myqueue.insert(59); //insert value queue cout << "after inserting 59 priority queue has" << endl; myqueue.insert(41); cout << "after inserting 41 priority queue has" << endl; myqueue.insert(25); cout << "after inserting 25 priority queue has" << endl; myqueue.insert(12); cout << "after inserting 12 priority queue has" << endl; myqueue.insert(91); cout << "after inserting 91 priority queue has" << endl; myqueue.min(); myqueue.extractmin(); cout << "after extracting minimum value priority queue has" << endl; myqueue.insert(34); cout << "after inserting 34 priority queue has" << endl; myqueue.insert(63); cout << "after inserting 63 priority queue has" << endl; myqueue.extractmin(); cout << "after extracting minimum value priority queue has" << endl; myqueue.insert(75); cout << "after inserting 75 priority queue has" << endl; myqueue.insert(85); cout << "after inserting 85 priority queue has" << endl; myqueue.extractmin(); cout << "after extracting minimum value priority queue has" << endl; cout <<"minimum value " ; cout << myqueue.min() <<endl; //prints out heap min system("pause"); return 0; } priorityqueue::priorityqueue() //constructor { size = capacity; &data[size]; } priorityqueue::~priorityqueue() //destructor { } int priorityqueue::getparent(int index) //finds parent { return (index - 1) / 2; } int priorityqueue::getleftchild(int index) //finds left child { return (2 * index) + 1; } int priorityqueue::getrightchild(int index) //find right child { return (2 * index) + 2; } void priorityqueue::swap(int& item1, int& item2) //swaps value of 2 variables { int temp = item1; item1 = item2; item2 = temp; } void priorityqueue::heapify(int index) //heapifies heap { int largest = index; int l = getleftchild(index); int r = getrightchild(index); if (l < size && data[l] > data[index]) { largest = l; } if (r < size && data[r] > data[largest]) { largest = r; } if (largest != index) { swap(data[index], data[largest]); heapify(data[size]); } } void priorityqueue::printarray(int []) //prints array { (int = 0; < capacity; i++) { cout << data[i] << ", "; } } int priorityqueue::extractmin() //finds min , removes { int min = data[0]; data[0] = data[size - 1]; size - 1; heapify(data[size]); return min; } int priorityqueue::min() const // finds min { return data[0]; } bool priorityqueue::empty() const // checks if heap empty { if (data == null) { return true; } else { return false; } } void priorityqueue::insert(int item) //inserts values heap { size += 1; int = size - 1; while (i > 0 && data[getparent(i)] < item) { data[i] = data[getparent(i)]; = getparent(i); } data[i] = item; }
in constructor, &data[size];
nothing. need allocate memory it, possibly using new - data = new int[size]
- or use smart pointer.
Comments
Post a Comment