Stack:
A stack is a linear data structure that follows LIFO(LAST IN FIRST OUT) principle
linear data structure–>where elements are arranged in a sequence order. Every elements will be connected to its previous or next element
Task Description
push(item)———————-Adds an item to the top of the stack
pop()—————————Remove and return the top item from the stack
peek()————————–Returns the top item without removing it
isEmpty()———————–checks if the stack is empty
search(obj)——————–searches for an object and returns its position
import java.util.*;
class Stackexample
{
public static void main(String args[])
{
Stack<String> s=new Stack<>();
//push the elements(inserting elements)
s.push(“Apple”);
s.push(“Banana”);
s.push(“cherry”);
System.out.println(“the elements are: +s);
//peek-display the top element without removing it
System.out.println(“Top element is=” +s.peek());
//pop-top element will be removed
System.out.println(“Top element is=” +s.pop());
//check if the stack is empty
System.out.println(“whether the stack is empty=” +s.isEmpty());
System.out.println(“Top element is=” +s.pop());
System.out.println(“Top element is=” +s.pop());
System.out.println(“the elements are: +s);
}}