-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackk.java
More file actions
64 lines (54 loc) · 1.2 KB
/
Stackk.java
File metadata and controls
64 lines (54 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import java.util.*;
class Stack{
int size=5;
int ele,top,i;
int stk[]=new int[size];
Stack(){
top=-1;
}
void push(){
if(top==size-1){
System.out.println("Stack is full.");
}else{
Scanner sc = new Scanner(System.in);
ele=sc.nextInt();
top++;
stk[top]=ele;
}
}
void pop(){
if(top==-1){
System.out.println("OOPS..!Stack is empty.");
}else{
ele=stk[top];
top--;
System.out.println("Popped element:" +ele);
}
}
void display(){
System.out.println("Stack elements are: ");
for(int i=top;i>=0;i--){
System.out.print(i+" ");
}
}
public static void main(String args[]){
Stack s=new Stack();
int choice;
Scanner sc=new Scanner(System.in);
System.out.println("Enter Your choice: ");
choice=sc.nextInt();
do{
System.out.println("1.push");
System.out.println("2.pop");
System.out.println("3.display");
switch(choice){
case 1: s.push();
break;
case 2: s.pop();
break;
case 3: s.display();
break;
}while(choice!=3)
}
}
}