Stack Class

  1. It is the child interface of vector.
  2. It is specially designed class for last in first out (LIFO).

Methods in stack

  1. Object push(Object o)

For inserting an object to the stack.

  • Object pop()

To remove and return top of the object.

  • Int peak();

To return the top of the stack without removal of object.

  • Int search(Object o)

If the specified object is available it returns its offset from the top of the stack.

If the specified object is not available it returns -1.

Examples

package List;

import java.util.Stack;

public class StackDemo {
	public static void main(String[] args) {
		Stack s = new Stack();
		s.push("A");
		s.push("B");
		s.push("C");
		System.out.println(s); // [A, B, C]
		System.out.println(s.search("A")); // 3
		System.out.println(s.search("Z")); // -1
	}

}

247 total views, 1 views today