No.
What the code does is
- remember the top element in
topNonNull
- null out the top element and then
- decrease the top-"pointer" in the
S[top--]=null
line - return
topNonNull
If you change this to S[--top]=null
then you will null out a different element, which is not what you want.