Im bad with pointers and references so i have a question why in function hoffman the values are good and fine but when im trying see them out of function hoffman in main() i cant see almost none of this values. This is my code:
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
struct element {
char letter;
int number;
string code;
element *left;
element *right;
};
bool my_cmp(const element& a, const element& b)
{
return a.number<b.number;
}
void hoffman(element &tab)
{
if(tab.left!=NULL)
{
tab.left->code=tab.code;
tab.left->code.push_back('0');
if(tab.left->letter!=' ')
{
cout<<tab.left->letter<<" "<<tab.left->code<<endl;
}
hoffman(*tab.left);
}
if(tab.right!=NULL)
{
tab.right->code=tab.code;
tab.right->code.push_back('1');
if(tab.right->letter!=' ')
{
cout<<tab.right->letter<<" "<<tab.right->code<<endl;
}
hoffman(*tab.right);
}
}
int main()
{
string C="abcdefghijklmnopqestuvwxyz",text;
int T[26]={0};
vector<element> tab;
cin>>text;
int rozmiar=text.length();
int rozm=0;
for(int i=0;i<rozmiar;i++)
{
for(int j=0;j<26;j++)
{
if(text[i]==C[j])
{
T[j]++;
break;
}
}
}
for(int j=0;j<26;j++)
{
if(T[j]!=0)
{
tab.push_back({C[j],T[j],"",NULL,NULL});
}
}
sort(tab.begin(),tab.end(),my_cmp);
while((rozm+1)!=tab.size())
{
tab.push_back({' ',tab[rozm].number+tab[rozm+1].number,"",&tab[rozm],&tab[rozm+1]});
rozm+=2;
sort(tab.begin()+rozm,tab.end(),my_cmp);
}
hoffman(tab[tab.size()-1]);
cout<<endl;
for(int i=0;i<tab.size();i++)
{
if(tab[i].letter!=' ') cout<<tab[i].letter<<" = "<<tab[i].code<<endl;
}
return 0;
}
In function hoffman im assigning string code to every element of struct but i cant see it out of function using the last loop, any ideas?