import java.util.Stack;
classMain {
publicstaticvoidmain(String[] args) {
Stack<String> animals=new Stack<>();
// Add elements to Stack
animals.push("Dog");
animals.push("Horse");
// Remove element from Stack
animals.pop();
// Access element from top of Stack
animals.peek();
}
}
Posted by: Guest
on May-20-2020
4
stack in java
// Java code for stack implementation import java.io.*;
import java.util.*;
classTest
{
// Pushing element on the top of the stack staticvoidstack_push(Stack<Integer> stack)
{
for(int i =0; i <5; i++)
{
stack.push(i);
}
}
// Popping element from the top of the stack staticvoidstack_pop(Stack<Integer> stack)
{
System.out.println("Pop Operation:");
for(int i =0; i <5; i++)
{
Integer y = (Integer) stack.pop();
System.out.println(y);
}
}
// Displaying element on the top of the stack staticvoidstack_peek(Stack<Integer> stack)
{
Integer element = (Integer) stack.peek();
System.out.println("Element on stack top: "+ element);
}
// Searching element in the stack staticvoidstack_search(Stack<Integer> stack, int element)
{
Integer pos = (Integer) stack.search(element);
if(pos ==-1)
System.out.println("Element not found");
else
System.out.println("Element is found at position: "+ pos);
}
publicstaticvoid main (String[] args)
{
Stack<Integer> stack =new Stack<Integer>();
stack_push(stack);
stack_pop(stack);
stack_push(stack);
stack_peek(stack);
stack_search(stack, 2);
stack_search(stack, 6);
}
}
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems
resetting your password contact us
Check Your Email and Click on the link sent to your email