forked from souvikg544/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.java
More file actions
78 lines (68 loc) · 1.22 KB
/
stack.java
File metadata and controls
78 lines (68 loc) · 1.22 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import java.util.*;
class Stack{
int tos;
int arr[];
public Stack(){
tos = -1;
arr = new int[5];
}
public Stack(int n){
tos = -1;
arr = new int[n];
}
public int getSize(){
return arr.length;
}
public void push(int data){
if(tos==arr.length-1){
System.out.println("OVERFLOW");
}
else{
arr[++tos] = data;
}
}
public int pop(){
if (tos==-1){
System.out.println("UNDERFLOW");
return 0;
}
else{
return(arr[tos--]);
}
}
public void display(){
System.out.println("Stack is: ");
for(int i=tos;i>=0;i--){
System.out.println(arr[i]);
}
}
}
class TestStack{
public static void main(String[] args) {
Scanner inpt = new Scanner(System.in);
Stack str = new Stack(5);
System.out.println("Size of the Stack is: " + str.getSize());
int ch;
do{
System.out.println("1. PUSH");
System.out.println("1. POP");
System.out.println("1. DISPLAY");
System.out.println("1. EXIT");
System.out.println("Enter Your Choice: ");
ch = inpt.nextInt();
switch(ch){
case 1:
System.out.println("ENTER THE VALUE TO BE PUSHED: ");
str.push(inpt.nextInt());
break;
case 2:
break;
case 3:
break;
case 4:
break;
}
}while(ch>0 && ch<5);
inpt.close();
}
}