0

So here is my code

void Graph::Print() const
{
        cout << "Number of verticies in graph: " << size << endl;
        cout << "-------- graph -------" << endl;

    list<int>::iterator a;
        for(int i=0;i<size;i++)
        {
        cout<<labels[i]<<":";
                for(auto a=adj_list[i].begin();a!=adj_list[i].end();a++)
                {
                        cout<< labels[*a] << ", ";
                        if(a!=adj_list[i].end())
                         
                }
                cout<<endl;
        }

        cout << "------- end of graph ------" << endl;

}

I am trying to have output like this where the comma is only printed between the letters but not if there is a single letter.

-------- graph -------
A: B, C, E
B: A, D, F
C: A, G
D: B
E: A, F
F: B, E
G: C
------- end of graph ------

my current output is

-------- graph -------
A:B, C, E,
B:A, D, F,
C:A, G,
D:B,
E:A, F,
F:B, E,
G:C,
------- end of graph ------

  • I generally print the first value without a comma and then print the comma before all subsequent values. – user4581301 Apr 27 '22 at 00:34
  • Reverse your thinking a little bit: `if ( some_condition) { print the comma; } print the letter` -- That is psuedo-code for how you would do this. Now think of what (some_condition) would be. – PaulMcKenzie Apr 27 '22 at 00:40

0 Answers0