Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Java/data-structures/StackDemo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import java.util.Scanner;
import java.util.Stack;

public class StackDemo {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);//declaring a scanner for taking input from user
Stack<Integer> stck=new Stack<Integer>();//using inbuilt stack class
System.out.println("Enter how many elements you want to insert into the stack");
int n=sc.nextInt();

System.out.println("Enter "+ n + " elements");
for(int i=0;i<n;i++)
{
stck.push(sc.nextInt());//pushing elements into the stack
}

System.out.println("Printing stack elements");
while(!stck.empty())
{
System.out.println(stck.pop()); //it will pop the element from the stack and print it.
}
}

}