The
Stack class represents a last-in-first-out (LIFO) stack of objects. It extends class Java Vector with five more operations that allow a vector to be treated as a stack.
The usual
push and pop operations are provided, as well as a method to peek at the top item on the stack, a method to test for whether the stack is empty , and a method to search the stack for an item and discover how far it is from the top. When a stack is first created, it contains no items.
Stack Constructor:
Stack Methods:
Stack CODE import java.util.Stack; OUTPUT Is empty stack: true DESCRIPTION In this program, stack is created, checked for empty then adds two integers, pops top element and again adds one integer and then returns peek integer, position of THINGS TO TRY
Stack class extends Vector class, methods of Vector class are applicable for Stack class as well.
|