Stack Class in Java with Example - Javastudypoint

Saturday, March 2, 2019

Stack Class in Java with Example

Stack in Java is a class which is present in java.util package. A stack is a child class of the vector that implements standard last-in,first-out(LIFO) stack data structure. It only defines the default constructor, which is used to create an empty stack. The stack includes all the methods defined by vector class and adds several of its own methods.
To put an element at the top of the stack we can use push() method. To remove the top element we can use a pop() method. An EmptyStackException is thrown if you call pop() method and your invoking stack is empty.
Read more about: Exception Handling in Java with Example.


Stack class in Java

Methods of Stack class in Java:


1. Object push(Object element): This method is used to push an element at the top of the stack.

2. Object pop(Object element): This method is used to pop the element at the top of the stack.

3. Object peek(): This method is used to returns the elements at the top of the stack, but doesn't remove it.

4. boolean empty(): This method is used to check the stack is empty or not. If the stack is empty returns true, and it returns false if the stack is not empty.

5. int search(Object element): This method is used to search whether a particular element is available in a stack or not. If the element is found in the stack it returns the position of the element else it returns -1.



Stack implementation in Java:



import java.util.*;

class Person

{
	//pushing element at the top of stack
	static void showPush(Stack<Integer> st, int a)
	{
		st.push(a);
		System.out.println("Push(" +a + ")");
		System.out.println("Stack: " +st);
	}

	//pop element from the stack
	static void showPop(Stack<Integer> st)
	{
		System.out.print("Pop -> ");
		Integer a =st.pop();
		System.out.println(a);
		System.out.println("Stack: " +st);
	}

	//displaying top element from stack
	static void showPeek(Stack<Integer> st)
	{
		Integer b = (Integer) st.peek();
		System.out.println("Element at the top of the stack is: " +b);
	}

	//searching element in stack
	static void showSearch(Stack<Integer> st, int element)
	{
		Integer position = (Integer) st.search(element); 
        if(position == -1) 
            System.out.println("Element not found"); 
        else
            System.out.println("Element is found at position " + position); 
	}

	public static void main(String args[])

	{
		Stack<Integer> st = new Stack<Integer>();

		System.out.println("Stack: " +st);

		showPush(st,10);

		showPush(st,20);

		showPush(st,30);

		showPush(st,40);

		showPop(st);

		showPeek(st);

		showSearch(st,20);

		showPop(st);

		showPop(st);

		showPop(st);

		try

		{
			showPop(st);

		}

		catch(EmptyStackException e)

		{

			System.out.println("Empty Stack");

		}

	}

}

Output:
Stack class in Java


You may also like:

What is Collection Framework in Java?
ArrayList in Java with Example
LinkedList in Java with Example
Serialization in Java with Example
Inheritance in Java OOPs concept
JSP Standard Tag Library- JSTL Tutorial
Session management techniques in Servlet
Types of JDBC Driver in Java




No comments:

Post a Comment