调试优先级队列程序时出现访问冲突错误
Access Violation error when I debug my priority queue program
当我尝试 运行 时,我的代码出现访问冲突错误。该程序是一个优先级队列,每次插入和最小提取后都会插入一个值并打印堆。
头文件:
#pragma once
/*
Header file for the 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 and return the smallest item currently in the priority queue
int extractMin();//dequeue
bool empty() const;
int min() const; //return the smallest item
};
#endif
主要代码:
#include <iostream>
#include "priorityQueue.h"
using namespace std;
int main()
{
priorityQueue myqueue; //class object
if (myqueue.empty())
cout << "My priority Queue is empty\n" << endl; //prompt
myqueue.insert(59); //Insert value into 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 the 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 the 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 the minimum value Priority Queue has" << endl;
cout <<"Minimum value is " ;
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 two variables
{
int temp = item1;
item1 = item2;
item2 = temp;
}
void priorityQueue::heapify(int index) //heapifies the 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
{
for (int i = 0; i < CAPACITY; i++)
{
cout << data[i] << ", ";
}
}
int priorityQueue::extractMin() //finds min and removes it
{
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 is empty
{
if (data == NULL)
{
return true;
}
else
{
return false;
}
}
void priorityQueue::insert(int Item) //inserts values into heap
{
size += 1;
int i = size - 1;
while (i > 0 && data[getParent(i)] < Item)
{
data[i] = data[getParent(i)];
i = getParent(i);
}
data[i] = Item;
}
在您的构造函数中,&data[size];
什么都不做。您需要为其分配一些内存,可能使用 new - data = new int[size]
- 或使用智能指针。
当我尝试 运行 时,我的代码出现访问冲突错误。该程序是一个优先级队列,每次插入和最小提取后都会插入一个值并打印堆。
头文件:
#pragma once
/*
Header file for the 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 and return the smallest item currently in the priority queue
int extractMin();//dequeue
bool empty() const;
int min() const; //return the smallest item
};
#endif
主要代码:
#include <iostream>
#include "priorityQueue.h"
using namespace std;
int main()
{
priorityQueue myqueue; //class object
if (myqueue.empty())
cout << "My priority Queue is empty\n" << endl; //prompt
myqueue.insert(59); //Insert value into 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 the 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 the 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 the minimum value Priority Queue has" << endl;
cout <<"Minimum value is " ;
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 two variables
{
int temp = item1;
item1 = item2;
item2 = temp;
}
void priorityQueue::heapify(int index) //heapifies the 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
{
for (int i = 0; i < CAPACITY; i++)
{
cout << data[i] << ", ";
}
}
int priorityQueue::extractMin() //finds min and removes it
{
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 is empty
{
if (data == NULL)
{
return true;
}
else
{
return false;
}
}
void priorityQueue::insert(int Item) //inserts values into heap
{
size += 1;
int i = size - 1;
while (i > 0 && data[getParent(i)] < Item)
{
data[i] = data[getParent(i)];
i = getParent(i);
}
data[i] = Item;
}
在您的构造函数中,&data[size];
什么都不做。您需要为其分配一些内存,可能使用 new - data = new int[size]
- 或使用智能指针。