[Solved] show the popped element in c++? [closed]


I’m not sure exactly what you were trying to do with your second loop in the pop function but this modified pop should display the popped value as well as the existing contents.

void pop (tindanan *t)
{
  int i;
  if(kosong(t) == 1)
    cout<<"\nTindanan Kosong\n";
  else
  {
    cout<<"\nPop Item: " << (t->senarai[t->atas]);

    t->atas--;

    cout<<"\nkandungan Timbunan :\nIndex | data\n";
    for(i = t->atas; i > -1; i--)
      cout<<" | "<<i<< " | "<<( t->senarai[i]);
  }
}    

But in general it would make sense to have pop return the item since that is how things commonly work. But then again push generally takes the item to push as well.

Alternatively you could write the pop like this and it would return the popped item then you could display it however you want.

char pop (tindanan *t)
{
  char popped_item = -1;
  int i;
  if(kosong(t) == 1)
    cout<<"\nTindanan Kosong\n";
  else
  {
    cout<<"\nPop Item: " << (t->senarai[t->atas]);

    popped_item = t->senarai[t->atas];

    t->atas--;

    cout<<"\nkandungan Timbunan :\nIndex | data\n";
    for(i = t->atas; i > -1; i--)
      cout<<" | "<<i<< " | "<<( t->senarai[i]);
  }
  return popped_item;
}    

3

solved show the popped element in c++? [closed]