adjacency list help
hi
been trying to compile this all day, but getting a bit stuck and generally confused. I'm new to programming and so any help/advice would be most appreciated.
Thanks in advance
Dan
// graph.cc
#include <iostream>
#include <fstream>
#include <string>
#include <float.h>
using namespace std;
struct costpair
{
int vnum;
float dist;
costpair(int v = 0, float f = 0):vnum(v), dist(f){};
};
struct node
{
costpair info;
node *next;
node *edge;
};
node *root;
node *current;
int num_in_graph;
class graph
{
public:
graph();
~graph();
void build_graph();
void print_graph();
void dijkstra();
private:
// Attributes
// Member functions
int index(string name); //declares integer index which contains a string called name
void insert_edge(string name1, string name2, float dist); //function of string name1 etc
int add_vertice(string name); //integer of string name
void add_edge(int index1, costpair temp);
void initialize();
int relax;
node *createnode(costpair temp);
};
// Constructor
graph::graph()
{
num_in_graph = 0;
}
// Destructor
graph::~graph()
{
}
void graph::build_graph()
{
string name1, name2,
char fileName;
int index1, index2, num_in_graph;
unsigned int dist;
cout << "please enter location of input file\n"<< endl;
cin >> fileName;
if (!fileName)||(!fileName.is_open())
{
cout << "Error opening file" << endl;
}
return (-1);
num_in_graph=0;
while (!fileName.eof() )
{
cin >> name1 >> name2 >> dist;
num_in_graph += 1;
insert_edge(name1, name2, dist);
}
}
// Function to insert new edge
// Called once two names and a dist have been read
void graph::insert_edge(string name1, string name2, float dist)
{
int index1 = index(name1);
//checks if first name needs to be added
if (index1 == -1)
index1 = add_vertice(name1);
int index2 = index(name2);
//checks if second name needs to be added
if (index2 == -1)
index2 = add_vertice(name2);
// At this point, index1 and index2 contain the indices of name1 and name2
// add the edge
add_edge(index1, costpair(index2, dist));
}
// Searches through linked list for name
// Returns the index of given name
int graph::index(string name)
{
current = root;
int i;
//does string compare until name is found, or reaches the end of linked list
while(strcmp(current->name, name)!=0 && current->next != NULL)
{
current = current->next;
i+=1; //increment to give position in list
if (strcmp(current->name, name) == 0)
{
return i; //returns position in the list
}
}
return -1; //else returns -1 if name is not found
}
// This functin addes a name to the name list
// and pushes the adjacency vector back.
// Then the new index of the name is returned
int graph::add_vertice(string name, int num_in_graph)
{
current = root;
if ( current != 0 )
{
while ( current->next != NULL)
current = current->next;
}
current->next = new node; // Creates a node at the end of the list
current = current->next; // Points to that node
current->next = 0; // Prevents it from going any further
current->dest = name;
num_in_graph += 1;
return (num_in_graph);
}
// Here a node is added to the adjacency vector
void graph::add_edge(int index1, costpair temp)
{
//transverse through graph index1 times
int i;
current = root;
for(i=0, i<5, i++)
{
current = current->next;
}
//add node to that position
node *p = createnode(temp); // Creates node
while (current->edge != NULL) //transverses edge
{
current = current->edge;
current->edge = new node; // Creates a node at the end of the list
current = current->edge; // Points to that node
current->edge = p;
}
}
// This function creates a node and adds info to node
node * graph::createnode(costpair temp)
{
node *p = new node;
p->info = temp;
p->next = NULL;
return p;
}
// This fuction prints all vertices along with edges
void graph::print_graph()
{
}
main ()
{
graph cities;
cities.build_graph();
cities.print_graph();
//cities.dijkstra();
}
[5228 byte] By [
deejay400] at [2007-11-19 6:15:09]

# 1 Re: adjacency list help
here are some revisions:
// graph.cc
#include <iostream>
#include <fstream>
#include <string>
#include <float.h>
using namespace std;
struct costpair
{
int vnum;
float dist;
costpair(int v = 0, float f = 0):vnum(v), dist(f){};
};
struct node
{
costpair info;
node *next;
node *edge;
string name;//I have added!
};
node *root;
node *current;
int num_in_graph;
class graph
{
public:
graph();
~graph();
void build_graph();
void print_graph();
void dijkstra();
private:
// Attributes
// Member functions
int index(string name); //declares integer index which contains a string called name
void insert_edge(string name1, string name2, float dist); //function of string name1 etc
int add_vertice(string name,int num_in_graph=0); //integer of string nameI have set the num_in_graph 0 as a default value, because u are using it in the insert_edge,but u omit a parameter,so it gives the warning
void add_edge(int index1, costpair temp);
void initialize();
int relax;
node *createnode(costpair temp);
};
// Constructor
graph::graph()
{
num_in_graph = 0;
}
// Destructor
graph::~graph()
{
}
void graph::build_graph()
{
string veryLargeString;
string name1, name2;
ifstream fileName("c:\\programefiles");//the file needs a directory,so i give the arbitrary one
int /*index1, index2,*/ ]//the index1 and index2 are misused!
int num_in_graph;
float dist;
cout << "please enter location of input file\n"<< endl;
//cin >> fileName;U can not use the file input stream like this a option is as following:
fileName>>veryLargeString;
if ((!fileName)||(!fileName.is_open()))
{
cout << "Error opening file" << endl;
}
//return (-1);as u intend to return nothing
num_in_graph=0;
while (!fileName.eof() )
{
cin >> name1 >> name2 >> dist;
num_in_graph += 1;
insert_edge(name1, name2, dist);
}
}
// Function to insert new edge
// Called once two names and a dist have been read
void graph::insert_edge(string name1, string name2, float dist)
{
int index1 = index(name1);
//checks if first name needs to be added
if (index1 == -1)
index1 = add_vertice(name1); U 'd better pay attention the prototype of the add_vertice
int index2 = index(name2);
//checks if second name needs to be added
if (index2 == -1)
index2 = add_vertice(name2);
// At this point, index1 and index2 contain the indices of name1 and name2
// add the edge
add_edge(index1, costpair(index2, dist));
}
// Searches through linked list for name
// Returns the index of given name
int graph::index(string name)
{
current = root;
int i=0;
//does string compare until name is found, or reaches the end of linked list
while(strcmp((current->name).c_str(), name.c_str())!=0 && current->next != NULL)
{
current = current->next;
i+=1; //increment to give position in list
if (strcmp((current->name).c_str(), name.c_str()) == 0)
{
return i; //returns position in the list
}
}
return -1; //else returns -1 if name is not found
}
// This functin addes a name to the name list
// and pushes the adjacency vector back.
// Then the new index of the name is returned
int graph::add_vertice(string name, int num_in_graph)
{
current = root;
if ( current != 0 )
{
while ( current->next != NULL)
current = current->next;
}
current->next = new node; // Creates a node at the end of the list
current = current->next; // Points to that node
current->next = 0; // Prevents it from going any further
current->name = name;
num_in_graph += 1;
return (num_in_graph);
}
// Here a node is added to the adjacency vector
void graph::add_edge(int index1, costpair temp)
{
//transverse through graph index1 times
int i;
current = root;
for(i=0; i<5; i++)
{
current = current->next;
}
//add node to that position
node *p = createnode(temp); // Creates node
while (current->edge != NULL) //transverses edge
{
current = current->edge;
current->edge = new node; // Creates a node at the end of the list
current = current->edge; // Points to that node
current->edge = p;
}
}
// This function creates a node and adds info to node
node * graph::createnode(costpair temp)
{
node *p = new node;
p->info = temp;
p->next = NULL;
return p;
}
// This fuction prints all vertices along with edges
void graph:print_graph()
{
}
main ()
{
graph cities;
cities.build_graph();
cities.print_graph();
cities.dijkstra();
}
in all, there are simple bugs in your programme, i can figure that u didnot run it with the compiler yet,right.
regards,
jolley