Three сreateThree(Node **q)
{
if((*q)->p)
{
Three *one, *two;
one = del(q);
two = del(q);
Three *t = new Three;
t->left = one;
t->right = two;
t->weight = one->weight + two->weight;
Node *tmpQ = 0;
while(q)
{
Three *tmp;
tmp = del(q);
if(!tmpQ)
{
tmpQ = t->weight > tmp->weight ? first(tmp) : first(t);
}
else
{
t->weight > tmp->weight ? add(&tmpQ, tmp) : add(&tmpQ, t);
}
}
сreateThree(&tmpQ);
}
else
{
return (*(*q)->t);
}
}
Three* del(Node **pbeg)
{
Three *temp = (*pbeg)->t; //ПОМЕЧЕННАЯ СТРОКА
Node *pv = *pbeg;
*pbeg = (*pbeg)->p;
delete pv;
return temp;
}
int main()
{
/*НЕКИЙ РАБОТАЮЩИЙ КОД*/
Node *q;
formQueue(&q, values, symbols, count);
Three t = сreateThree(&q);
Show(&t);
cout<<endl;
return 0;
}
Three* createList(int weight, char symbol)
{
Three *t = new Three;
t->weight = weight;
t->symbol = symbol;
t->left = 0;
t->right = 0;
}
Three* createList(int weight, char symbol)
{
Three *t = new Three;
t->weight = weight;
t->symbol = symbol;
t->left = 0;
t->right = 0;
}
void Show(Three *t)
{
if(t)
{
cout<<t->weight<<"\t"<<t->symbol<<endl;
Show(t->left);
Show(t->right);
}
}
struct Node {
Three *t;
Node *p;
};
// Начальное формирование очереди
Node * first(Three *t)
{
Node *pv = new Node;
pv->t = t;
pv->p = 0;
return pv;
}
//---------------------------------------
// Добавление в конец
void add(Node **pend, Three *t)
{
Node *pv = new Node;
pv->t = t;
pv->p = 0;
(*pend)->p = pv;
*pend = pv;
}
//---------------------------------------
// Выборка
Three* del(Node **pbeg)
{
//assert(pbeg);
//assert(*pbeg); //- Этот аассерт срабатывает и видимо сразу на первом вызове
Three *temp = (*pbeg)->t;
Node *pv = *pbeg;
*pbeg = (*pbeg)->p;
delete pv;
return temp;
}
void sortArray(char *symbols, int *values, int count)
{
int tmp_i;
char tmp_c;
for(int i = 0; i < count; i++)
{
for(int j = count-1; j > i; j--)
{
if(values[j-1] > values[j])
{
tmp_i = values[j];
tmp_c = symbols[j];
values[j] = values[j-1];
symbols[j] = symbols[j-1];
values[j-1] = tmp_i;
symbols[j-1] = tmp_c;
}
}
}
}
void formQueue(Node **q, int *values, char *symbols, int count)
{
Three *l = createList(values[0], symbols[0]);
(*q) = first(l);
Node *qEnd = (*q);
for(int i = 1; i < count; i++)
{
Three *tmp = createList(values[i], symbols[i]);
add(&qEnd, tmp);
}
}
while(q) Show(del(&q));
return 0;
two = del(q);
Three* del(Node &*pbeg)
{
assert(pbeg != 0); // для гарантий
Three *temp = pbeg->t; //ПОМЕЧЕННАЯ СТРОКА
Node *pv = pbeg;
pbeg = pbeg->p;
delete pv;
return temp;
}