Question: Please I need help figuring out how to fix my memory leak. I know the problem is from my PriorityQueue::insert with new but I don't

Please I need help figuring out how to fix my memory leak. I know the problem is from my PriorityQueue::insert with "new" but I don't know where to "delete" whithout causing more errors. Please and thank you.

#include

#include

#include

#include

using namespace std;

struct AdjListNode

{

int dest;

int weight;

struct AdjListNode* next;

AdjListNode(int d,int w, AdjListNode *n)

:dest(d), weight(w), next(n)

{}

static void deleteList(AdjListNode * l){

if(l){

deleteList(l->next);

delete l;

}

}

static AdjListNode* newAdjListNode(int dest, int weight,AdjListNode *h){

return new AdjListNode(dest,weight, h);

}

friend ostream & operator << (ostream & out, AdjListNode *l){

if(l!=nullptr){

out << "(" << l->dest << "," << l->weight << ")" << l->next;

}

return out;

}

};

struct AdjList

{

AdjListNode *head;

friend ostream & operator << (ostream & out, AdjList & a){

out << a.head<

return out;

}

};

struct Graph

{

int size;

AdjList* array;

Graph(const char fileName[]){

int value, src, dst, cost;

ifstream in(fileName);

if (in >> value){

size = value;

array = new AdjList[value];

}

for (int i = 0; i < size; ++i){

array[i].head = nullptr;

}

while(in >> src>> dst>> cost){

addEdge(src,dst,cost);

}

in.close();

}

void addEdge(int src, int dest, int weight)

{

array[src].head = AdjListNode::newAdjListNode(dest, weight,array[src].head);

array[dest].head = AdjListNode::newAdjListNode(src, weight,array[dest].head);

}

friend ostream & operator << (ostream & out, Graph & g){

for ( int i=0; i < g.size; ++i ){

out << i << " "<

}

return out;

}

~Graph(){

for ( int i=0; i < size; ++i ){

AdjListNode::deleteList(array[i].head);

}

delete[] array;

}

};

struct Vector

{

int v;

int dist;

Vector * next;

Vector(int vv, int dd, Vector * n)

:v(vv), dist(dd), next(n)

{}

static Vector* newVector(int vv, int dd, Vector * n){

return new Vector(vv, dd, n);

}

static void deleteList(Vector * l){

if(l){

deleteList(l->next);

delete l;

}

}

friend ostream & operator << (ostream & out, Vector *l){

if(l){

out << "("<< l->v << ","

}

return out;

}

};

struct VectorList{

Vector * nodes;

};

struct PriorityQueue

{

int size; // Number of heap nodes present currently

int capacity; // Capacity of min heap

int *pos; // This is needed for decreaseKey()

VectorList *front;

PriorityQueue(int N)

:pos(new int[N]), size(0), capacity(N)

{

front = new VectorList[N];

for (int i = 0; i< N; i++){

front[i].nodes = nullptr;

}

}

void insert(int v, int dst){

front[v].nodes = Vector::newVector(v, dst, front[v].nodes);

size++;

}

void siftDown(int idx)

{

int smallest, left, right;

smallest = idx;

left = 2 * idx + 1;

right = 2 * idx + 2;

if (left < size && front[left].nodes->dist < front[smallest].nodes->dist )

smallest = left;

if (right < size && front[right].nodes->dist < front[smallest].nodes->dist )

smallest = right;

if (smallest != idx)

{

Vector *smallestNode = front[smallest].nodes;

Vector *idxNode = front[idx].nodes;

pos[smallestNode->v] = idx;

pos[idxNode->v] = smallest;

swapVector(&front[smallest].nodes, &front[idx].nodes);

siftDown(smallest);

}

}

void swapVector(Vector** a, Vector** b)

{

Vector* t = *a;

*a = *b;

*b = t;

}

void decreaseKey(int v, int dist)

{

int i = pos[v];

front[i].nodes->dist = dist;

while (i && front[i].nodes->dist < front[(i - 1) / 2].nodes->dist)

{

pos[front[i].nodes->v] = (i-1)/2;

pos[front[(i-1)/2].nodes->v] = i;

swapVector(&front[i].nodes, &front[(i - 1) / 2].nodes);

i = (i - 1) / 2;

}

}

Vector* extractMin()

{

Vector* root = front[0].nodes;

Vector* lastNode =front[size-1].nodes;

front[0].nodes = lastNode;

pos[root->v] = size-1;

pos[lastNode->v] = 0;

--size;

siftDown(0);

return root;

}

int isEmpty()

{

return size <= 0;

}

friend ostream & operator << (ostream & out, PriorityQueue & h){

for(int i = 0; i < h.capacity; i++){

out << i << h.front[i].nodes<

}

return out;

}

~PriorityQueue(){

delete [] pos;

delete [] front;

}

};

void printPath(int parent[], int j)

{

if (parent[j]==-1)

return;

printPath(parent, parent[j]);

printf("-%d", j);

}

void printArr(int dist[], int n, int parent[])

{

cout<< "Destination \tCost \tPath"<

for (int i = 0; i < n; ++i){

cout << " "<

printPath(parent, i);

}

cout<

}

void dijkstra(Graph &graph, int src, int * dist, int * prev)

{

int V = graph.size;

PriorityQueue Q(V);

for (int v = 0; v < V; ++v)

{

prev[v] = -1;

dist[v] = INT_MAX;

Q.insert(v, dist[v]);

Q.pos[v] = v;

}

Q.pos[src] = src;

dist[src] = 0;

Q.decreaseKey(src, dist[src]);

while (!Q.isEmpty())

{

Vector* vector = Q.extractMin();

int u = vector->v;

AdjListNode* pCrawl = graph.array[u].head;

while (pCrawl != nullptr)

{

int v = pCrawl->dest;

if (dist[v]> pCrawl->weight + dist[u])

{

dist[v] = dist[u] + pCrawl->weight;

prev[v] = u;

Q.decreaseKey(v, dist[v]);

}

pCrawl = pCrawl->next;

}

}

}

int main()

{

Graph G("graph.txt");

int * dist = new int[G.size];

int * prev = new int[G.size];

dijkstra(G, 0, dist, prev);

printArr(dist, G.size, prev);

delete [] dist;

delete [] prev;

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!