Stack in Java

Stack in java

Stack

Stack in Java, The evaluation of stack is always LIFO(last in first out) order.

The operation on the stack always for the top of the stack.

The stack is non-synchronous in nature.

The duplicate element can be stored by using the stack data structure.

Index of stack always starts with 1.

Methods in stack:-

Push():-  Pushes the element on the top of the stack.

Pop() :-  Pop the element on the stack.

Peek():-  Returns the element on the top of the stack, but does not remove it.

Empty():- It returns true or false. If the top of the stack is empty then it returns true otherwise return false.

Search():- It determines the object exists in the stack. If the element is found, it returns the position of the element else return -1.

Example:-

import java.util.*;
public class test 
{
   public static void main(String[] args) 
   {
        Stack<Character>st=new Stack<Character>();
        st.push('A');
        System.out.println(st);
        st.push('c');
        st.push('v');
        System.out.println(st);
        System.out.println(st.isEmpty());
        System.out.println(st.search('A'));
        st.pop();
        System.out.println(st.peek());
        System.out.println(st);
    }
}

 

 

basicittopic

basicittopic

A Computer Science Study for IT students and people of IT community

Leave a Reply