algorithm for change operation in stack