import java.io.
*;
import java.util.*;
class Test
{
// Pushing element on the top of the stack
static void stack_push(Stack<Integer> stack)
{
for(int i = 0; i < 5; i++)
{
stack.push(i);
}
}
// Popping element from the top of the stack
static void stack_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
static void stack_peek(Stack<Integer> stack)
{
Integer element = (Integer) stack.peek();
System.out.println("Element on stack top: " + element);
}
// Searching element in the stack
static void stack_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);
}
public static void 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);
}
}
import java.io.*;
import java.util.*;
class StackDemo {
// Main Method
public static void main(String[] args)
{
// Default initialization of Stack
Stack stack1 = new Stack();
// Initialization of Stack
// using Generics
Stack<String> stack2 = new Stack<String>();
// pushing the elements
stack1.push(4);
stack1.push("All");
stack1.push("Geeks");
stack2.push("Geeks");
stack2.push("For");
stack2.push("Geeks");
// Printing the Stack Elements
System.out.println(stack1);
System.out.println(stack2);
}
}
import java.util.*;
import java.io.*;
public class StackDemo {
public static void main(String args[])
{
// Creating an empty Stack
Stack<Integer> stack = new Stack<Integer>();
// Use add() method to add elements
stack.push(10);
stack.push(15);
stack.push(30);
stack.push(20);
stack.push(5);
// Displaying the Stack
System.out.println("Initial Stack: " + stack);
// Removing elements using pop() method
System.out.println("Popped element: "
+ stack.pop());
System.out.println("Popped element: "
+ stack.pop());
// Displaying the Stack after pop operation
System.out.println("Stack after pop operation "
+ stack);
}
}