4.1.5 Pop

To pop an item, we retrieve the value of the last element (based on size), then resize the array to one fewer. Of course, we need to check if the stack is empty, first.

char Stack_pop(struct Stack *pStack)
{
  char result = 0;
  if (!Stack_isempty(pStack))
  {
    unsigned size;

    size = ArrayADT_getsize((struct ArrayADT *)pStack);
    result = ArrayADT_getElement((struct ArrayADT *)pStack, size-1);
    ArrayADT_resize((struct ArrayADT *)pStack, size-1);
  }
  return result;
}



Copyright © 2006-10-23 by Tak Auyeung