looking at the first line t is the vertex 2 is the number of edges it has and X and F are the edges connecting to T.
So im suppose to implement an adjacency list which will read a file and store it in array called info which contains vertexname, outdegree, and adjcency list.
i really need help how to correctly read the file and store the values into an array using link list class i have
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include "slist.h"
#include "ll.C"
usingnamespace std;
void getData();
void printData();
constint SIZE = 10;
struct info
{
char vertexName;
int outDegree;
slist adjacentOnes; // this comes from slist.h which is a link list class
};
int main()
{
}
//PURPOSE: GETS DATA FROM THE FILE
void getData()
{
info table[SIZE];
ifstream fin;
fin.open("table.txt");
while(!fin.eof())
{
for(int i = 0; i < SIZE;i++)
{
fin >> table[i].vertexName;
fin >> table[i].outDegree;
// this is where im kinda of lost, I have a slist class which is a link list class
// i implemented and it works. It has a member called addRear. addRear must be called
// everytime a letter is read from the file.For how would i do this??
//
}
}
}
void printData()
{
}
ok now i have the following code, how could i use this information to use printData() and print the table
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
void getData()
ifstream fin("table.txt"); // opens the file
string line; // declares line as a string
// goes on a for loop and reads every variable, if variable is
// a letter it calls addRear and appends it to the back of a list
for(int i = 0; i<SIZE && getline(fin, line); i++)
{
istringstream S(line);
S >> table[i].vertexName >> table[i].outDegree;
char x;
while(S >> x)
table[i].adjacentOnes.addRear(x);
}
}
if the output looks like the input then code should be similar.
First you need a variable counter which counts the entries in your table.
Your function should look like this
1 2
int getData(info table[], int max_size); // parameter: the table and the size of the table / return: the number of entries
void printData(info table[], int size); // parameter: the table and the number of entries
then you can use it in your 'main' like so:
1 2 3 4 5 6 7 8 9
int main()
{
info table[SIZE];
int size = getData(table, SIZE);
printData(table, size);
return 0;
}
Your print function might look somewhat like this:
1 2 3 4 5 6 7 8 9
void printData(info table[], int size)
{
for(int i = 0; i<size; i++)
{
std::cout << table[i].vertexName << " " << table[i].outDegree;
table[i].adjacentOnes.print(); // This would print recursively
}
}