Problem In a Stack

Hi guys...
I'm having a problem on my dynamic allocation stack...I try many changes, but unsuccessfully....When the program doesn't freeze, it says "Empty Pile"....
I put here a piece of my work, where I think has a problem....If you need more, I post later.....and If you can't understand, ask me, because I'm too lazy to translate....^^
Fix for me guys
Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

typedef struct NODO {
        struct REG dado; struct NODO *proximo;
}PILHA;
PILHA *CriaPilha()
{
      return NULL;
}
PILHA *Empilha(PILHA *topo, struct REG &elemento)
{
      struct NODO *p;
      p = (struct NODO * )malloc(sizeof(struct NODO));
      p->dado = elemento;
      p->proximo = topo;
      return p; // p é o novo topo
}
void ImprimePilha(PILHA *p)
{
    PILHA *f;
    f=CriaPilha();
    int i;
    f=p;
    if (PilhaVazia(f))
       cout <<"A pilha de Avioes Aterrissados esta vazia...";
    else 
    for (i=1;!PilhaVazia(f);f=f->proximo)
	{
	cout <<i <<" - " <<f->dado.voo <<" - " <<f->dado.origem
        <<" - " <<f->dado.destino <<" - " <<"COMB: "
        <<f->dado.comb <<"\n"; i++;
    }
}
int DesceAviao(FILA *v, PILHA *d)
{
     struct NODO *aviao;
     if(FilaVazia(v))
          cout <<"\nA fila de Avioes em Voo esta vazia...";
     else {
     Desenfileira(v, aviao->dado);
     Empilha(d, aviao->dado);
     cout <<"Aviao: " <<aviao->dado.voo <<" - " <<aviao->dado.origem 
          <<" - " <<aviao->dado.destino
          <<" - Aterrissado...\n"
          <<"Imprimindo a pilha atual de Avioes Aterrissados:\n\n";
     ImprimePilha(d);
     }
}
I guess it's Portuguese...

you need to provide all involved function.
What does DesceAviao() mean?
What does Desenfileira() do?

the use of Empilha() on line 40 doesn't look right. What happens to the newly created node?
or
I would have expect that on line 14 p and topo are exchanged.
DesceAviao() get a element of queue "v" and put in the stack "d"....
Desenfileira() get a element out of the queue....

I can't undestand what do you talk about the Empilha(), can you explain again?????..."Empilha()" get a element and put in the stack...."Topo" is the stack out of the function....When the Empilha finish, the "p" return in the place of "topo".....
When the Empilha finish, the "p" return in the place of "topo".....
Yes, but on line 40, what are you doing with the result of Empilha()? Nothing. topo is not changed, hence the stack will remain empty.

by the way: The return type of DesceAviao() is int, but within that function you don't return anything
I can't undestand....my objetive is put "p" on the place of "topo", because I get "topo" and the internal values of its for the "p", when I put "aviao" in the top and "topo" below....How can I put the "p" in the place of "topo"????
my objetive is put "p" on the place of "topo"
Yes, but you don't do it.

Use the reference operator & for this:
1
2
3
4
5
6
7
8
9
10
11
12
13
void Empilha(PILHA *&topo, struct REG &elemento) // Note: *& means you have a reference to your pointer
// i.e. the modification of the pointer will also have an effect on the caller
{
      struct NODO *p;
      p = (struct NODO * )malloc(sizeof(struct NODO));
      p->dado = elemento;
      p->proximo = topo;
      topo = p; // Note: this will set 'topo' to the newly created node
      // no need to return something
      return p; // p é o novo topo
}

int DesceAviao(FILA *v, PILHA *&d) // Note: *& is needed here also to transport the change of 'topo' outside of this function! 


[EDIT]
By the way: basically, when you pass a (non const) reference like struct REG &elemento to Empilha() means that you can modify elemento and aviao->dado (on line 40) is changed too
[/EDIT]
Last edited on
Thanks coder......I don't believe one "&" would solve my problem...¬¬... C++ is boring and madness....:P
But we need to stand that rock in the shoes....^^
Topic archived. No new replies allowed.