forked from larissalages/code_problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.java
More file actions
87 lines (83 loc) · 1.58 KB
/
stack.java
File metadata and controls
87 lines (83 loc) · 1.58 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
79
80
81
82
83
84
85
86
87
import java.lang.Math;
public class Mystack implements Stack{
Object data[];
int top=0,cap;
Mystack(int size){
data = new Object[size];
cap = size;
}
public boolean IsFull(){
if(top==cap){
return true;
}
return false;
}
public boolean IsEmpty(){
if(top == 0){
return true;
}
return false;
}
public void Push(Object ele){
if(IsFull()){
System.out.println("Stack is Full, Can't add more elemeants.");
}
data[top] = ele;
top++;
System.out.println("Element added.");
}
public Object Pop(){
if(IsEmpty()){
System.out.println("Stack is empty, add element first.");
return -1;
}
top--;
return data[top];
}
public Object peek(){
if(IsEmpty()){
System.out.println("Stack is empty, add element first.");
return -1;
}
return data[top-1];
}
public int Prec(char ele){
switch(ele){
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
}
return -1;
}
public Object opration(int b,int a,char c){
switch(c){
case '+':
return a+b;
case '-':
return a-b;
case '*':
return a*b;
case '/':
return a/b;
// case '^':
// int n=1;
// boolean x=true;
// if(a==0)
// x=false;
// while(x){
// n*=a;
// a--;
// if(a>0)
// x=false;
// }
// return n;
default:
return -1;
}
}
}