Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What should the 'pop()' method return when the stack is empty? [duplicate]

Possible Duplicate:
C++ STL stack question: Why does pop() not throw an exception if the stack is empty?

When designing a stack in C++, what should the pop() method (or front() method) return when the stack is empty? Which of the following design is better?

  1. Throw an exception
  2. Undefined, but require the user calling isempty() method to check before calling pop()
  3. Return a bool code, while using an extra parameter (a reference) to pass the popped element
  4. Define an unique empty element

OK, I see that my question is not that clear, let me try to rewrite it:

There are some data structures which can be implemented based on linked list like stack, queue, and each of them has a methods returning the front element (or the tail).

I want to know, is there any principle guideline about designing such a method regarding the case when the data is empty.

And my definition of better is "easy to use correctly and hard to use incorrectly".

like image 262
zhanwu Avatar asked Sep 12 '11 15:09

zhanwu


People also ask

What does pop return if stack is empty?

Return Value: This method returns the element present at the top of the stack and then removes it. Exceptions: The method throws EmptyStackException is thrown if the stack is empty.

What does pop () do in a stack?

The pop() function is used to remove or 'pop' an element from the top of the stack(newest or the topmost element in the stack).

What happens if we call pop () and there is no element?

Description. The pop() method removes the last element from an array and returns that value to the caller. If you call pop() on an empty array, it returns undefined .

Can you pop an empty stack Java?

Methods in the Stack class The push() method pushes an element, that is passed as the parameter, on the top of the stack. pop(): The pop() method removes and returns the top element of the stack. An EmptyStackException exception is thrown if we call the pop() method on an empty stack.


2 Answers

The programming-by-contract style would be that having a non-empty stack is a precondition of calling pop, and that calling a method without meeting its preconditions has an undefined outcome. My implementation would throw a std::logic_error, but that would not be required. In C, my implementation would abort via assert.

The caller of popis responsible for ensuring that the precondition that the stack is not empty holds before calling pop. The stack should therefore have an isEmpty method for the caller to check.

like image 161
Raedwald Avatar answered Oct 03 '22 13:10

Raedwald


The C++ STL actuall doesn't return anything via pop() since it decouples returning the value of an object and actually popping an object off the stack's internal data-structure, making them two separate functions. So that's another option to consider in your design of a stack data-structure.

Your third option is also a pretty idiomatic approach for these types of data-structures.

For your fourth option, rather than a "unique empty element", I would actually do a variation on your third option where your pop() function that takes a pointer argument rather than a reference type, and returns NULL if there are no objects left in the stack.

like image 23
Jason Avatar answered Oct 03 '22 11:10

Jason