Solved

# Problems with making this code work...PLEASE HELP

Posted on 2005-03-16

Update. I have pasted my code below for the adjacency list .cpp and .h files and the driver file. I am having problems with the pointer array. At least I think that is the problem. If anyone of you could help with this I would greatly appreciate it. Thanks for you help. I have posted this code on a previous post, but no replies. I didn't have an option to assign points to it. I am not sure how the point system or how other people see my posts, so I started a new one. Sorry if I shouldn't do this, but I am desparate to finish this code. See my statement at the bottom of the driver code.

The Header file:

------------------------------------

//SPECIFICATION FILE: adjacency.h

#ifndef _ADJACENCYLIST

#define _ADJACENCYLIST

#include <string>

using namespace std;

template <class NodeInfo>

class AdjacencyList

{

public:

//Default constructor

AdjacencyList();

//Destructor

~AdjacencyList();

//Constructor to create and edge with incident

//vertices v1 and v2, the weight of the edge

void AddEdge(NodeInfo, NodeInfo, int);

//Returns the weight of the nodes that is being sent by user

int WeightIs(NodeInfo);

private:

int numVertices; // index keeps track of the array size

struct Vertex // struct of type vertex

{

int v1;

int v2;

int weight;

Vertex *next;

};

Vertex *Adjacency; // a pointer of type vertex

Vertex *dummy1; // a pointer of type vertex

};

#endif

/************************************************************************************************/

The .cpp file

------------------------------------

//IMPLEMENTATION FILE: adjacency.cpp

#include <string>

#include "adjacency.h"

using namespace std;

//***************************************************************

// Function: Default constructor

// Description: Initializes the private member variables

// Pre: None

// Post: Constructs the disjoint set object, numberOfElements is

// the initial number of disjoint sets.

//***************************************************************

template <class NodeInfo>

AdjacencyList<NodeInfo>::AdjacencyList()

{

Adjacency = new Vertex[100];

dummy1 = Adjacency;

numVertices= 0;

Adjacency->v1 = 0;

Adjacency->v2 = 0;

Adjacency->weight= 0;

}

//***************************************************************

// Function: Destructor

// Description: destroys what the constructor created

// Pre: An array exists

// Post: Destroys the Array.

//***************************************************************

template <class NodeInfo>

AdjacencyList<NodeInfo>::~AdjacencyList()

{

delete [] Adjacency;

}

//***************************************************************

// Function: AddEdge

// Description: gets the value from the user and adds them to the nodes

// Pre: An array exists

// Post: Adds the edges with the cost and connects it with other equivalent

// edges.

//***************************************************************

template <class NodeInfo>

void AdjacencyList<NodeInfo>::AddEdge(NodeInfo vertex1,

NodeInfo vertex2, int wgt)

{

if(Adjacency!=NULL)

{

Adjacency[numVertices].v1 = vertex1;

Adjacency->v2 = vertex2;

Adjacency->weight= wgt;

Adjacency = new Vertex;

Adjacency = Adjacency->next;

}

Adjacency= dummy1;

numVertices++;

}

//***************************************************************

// Function: WeightIs

// Description: returns the weight of two nodes.

// Pre: an array has been constructed

// Post: Constructs the disjoint set object, numberOfElements is

// the initial number of disjoint sets.

//***************************************************************

template <class NodeInfo>

int AdjacencyList<NodeInfo>::WeightIs(NodeInfo vertex)

{

int totalWeight;

Adjacency = dummy1;

if(Adjacency!=NULL)

{

for(int j=0;j<numVertices;j++)

{

if(Adjacency[j].v1 == vertex)

{

totalWeight = Adjacency->weight;

}

else if(Adjacency->next != NULL)

{

Adjacency = Adjacency->next;

}

j++;

}

}

Adjacency= dummy1;

return totalWeight;

}

/******************************************************************************/

driver .cpp file

------------------------------------

#include <iostream>

#include <string>

#include "unionfind.h"

#include "adjacencylist.h"

using namespace std;

int main()

{

//Driver for Adjacency list class

Graph<int> g;

g.AddEdge(2, 3, 4);

g.AddEdge(5, 6, 7);

g.AddEdge(8, 5, 9);

g.AddEdge(1, 3, 6);

// When it gets here, WeightIs function, doesn't work. Get a segmentation fault here

cout<<"The weight of 2 is: "<<g.WeightIs(2)<<endl;

cout<<"The weight of 5 is: "<<g.WeightIs(5)<<endl;

cout<<"The weight of 8 is: "<<g.WeightIs(8)<<endl;

cout<<"The weight of 1 is: "<<g.WeightIs(1)<<endl;

return 0;

}

/*************************************************************************/

I am creating a list of integer arrays where each cell is a pointer, but I am not sure how to do this. I believe this is my problem. I would appreciate any help!! Thanks.