The stack is a list-like structure in which elements may be inserted or removed from only one end. While this restriction makes stacks less ﬂexible than lists, it also makes stacks both efﬁcient (for those operations they can do) and easy to implement. Many applications require only the limited form of insert and remove operations that stacks provi...
Read MoreIn this post we have alreday seen a Array based stack implementation. As with the array-based Stack implementation, the Array must be declared of ﬁxed size when the stack is created. In the stack constructor, size serves to indicate this size. Method top acts somewhat like a current position value (because the “current” position is always at the ...
Read MoreIn a Stack, keep track of the maximum element value in it. It might be the top element in the stack but once it the current max is poped out from the stack then the maximum value should be the second maximum element in the stack. Algorithm: We will maintain 2 Stacks to solve the problem. The first one main stack will contains the elements and...
Read MoreThe stack is a list-like structure in which elements may be inserted or removed from only one end. While this restriction makes stacks less ﬂexible than lists, it also makes stacks both efﬁcient (for those operations they can do) and easy to implement. Many applications require only the limited form of insert and remove operations that stacks provi...
Read MoreLet s1 and s2 be the two Stacks to be used in the implementation of the Queue. All we have to so is to define the enQueue and deQueue operations for the Queue. The enQueue algorithm is very simple, Just push the value on stack s1. The time complexity of enQueue operation is O(1). deQueue Algorithm: If the stack s2 is not empty then pop from s2 and...
Read More