The Java stack class USES the use of the stack in the instance of of Java

  • 2020-04-01 02:39:57
  • OfStack

In JAVA, objects are created using the constructor of the java.util.stack class.

  Public class Stack extends vector

  Constructor: public Stack() creates an empty Stack.

Methods:   1. Public push  (item)   Push the item to the top of the stack. It has the same function as addElement (item).

Parameter item pushes the item at the top of the stack. Returns: item parameter;

2. Public pop () removes the top object on the stack and returns it as the value of the function.

Returns: top of the stack object (the last item in the Vector object).

Throws an exception: EmptyStackException if the stack is empty...

3. Public peek() views the top object on the stack without removing it.

Returns: top of the stack object (the last item in the Vector object).

Throws an exception: EmptyStackException if the stack is empty...

4. Public Boolean empty (tests if the stack is empty)   Returns true if and only if there is no item in the stack, false otherwise.

5. Public int search  (object o)   Returns the position of the object on the stack, in base 1. If object o is an item on the stack, this method returns the distance from the closest occurrence to the top of the stack to the top of the stack. The top item on the stack is one away. Use the equals method to compare o with the items in the stack...    

Parameter: o target object;



package thinkingJava;
import java.util.*;

import com.sun.org.apache.bcel.internal.generic.NEW;

public class StackTest {

    
    public static void main(String[] args) {
        Stack stack = new Stack(); //Creating a stack object
        System.out.println("11111, absdder, 29999.3  Three elements are pushed "); 
        stack.push(new Integer(11111)); //Press the integer 11111 onto the stack
        printStack(stack);  //Displays all the elements in the stack

        stack.push("absdder"); //Push into the stack
        printStack(stack);  //Displays all the elements in the stack

        stack.push(new Double(29999.3)); //Push into the stack
        printStack(stack);  //Displays all the elements in the stack

        String s = new String("absdder");
        System.out.println(" The element absdder On the stack "+stack.search(s));      
        System.out.println(" The element 11111 On the stack "+stack.search(11111));

        System.out.println("11111, absdder, 29999.3  Three elements out "); //Pops the top of the stack element
        System.out.println(" The element "+stack.pop()+" Out of the stack ");
        printStack(stack);  //Displays all the elements in the stack
        System.out.println(" The element "+stack.pop()+" Out of the stack ");
        printStack(stack);  //Displays all the elements in the stack
        System.out.println(" The element "+stack.pop()+" Out of the stack ");
        printStack(stack);  //Displays all the elements in the stack

 
    }

    private static void printStack(Stack<Integer> stack ){
        if (stack.empty())
            System.out.println(" The stack is empty and has no elements ");
            else {
                System.out.print(" Elements in the stack: ");
                Enumeration items = stack.elements(); //Get the enumerated object in the stack
                while (items.hasMoreElements()) //Displays all the elements in the enumeration (stack)
                    System.out.print(items.nextElement()+" ");
            }
        System.out.println(); //A newline
    }
}


Related articles: