Stack Class In Java

preview_player
Показать описание
#BackCoding

The stack is a linear data structure that is used to store the collection of objects. It is based on Last-In-First-Out (LIFO). Java collection framework provides many interfaces and classes to store the collection of objects. One of them is the Stack class that provides different operations such as push, pop, search, etc.

#programming
#java
#shorts
Рекомендации по теме
Комментарии
Автор

import java.util.*;

class StackDemo{

public static void main (String args[]){

Stack<Integer> st=new Stack<>();

int choice=0;
int position, element;

Scanner scr=new Scanner(System.in);

while(true){

System.out.println("Menu");

System.out.println("1..Push an Element");
System.out.println("2..Pop an Element ");
System.out.println("3.. Search an Element");
System.out.println("4.. Exit");

System.out.print("Enter your choice:-");
choice=scr.nextInt();

switch(choice){
case 1:
System.out.print("Enter element :");
element=scr.nextInt();
st.push(element);
break;

case 2:
Integer obj=st.pop();
System.out.println("Popped ="+obj);
break;

case 3:
System.out.println("which element? :");
element=scr.nextInt();
position=st.search(element);
if(position==-1){
System.out.println("Element not found");
}else{
System.out.println("Position :"+position);
}
break;

case 4:
System.exit(0);

default:
return;
}
System.out.println("Stack Contents "+st);
}
}
}

BackCoding
Автор

It's recommended to use the Deque collection instead of the Stack

jack_papel
Автор

You have to type this much in java lol, In python we can finish it with less than 10 lines

spikeop
join shbcf.ru