File tree Expand file tree Collapse file tree
data_structures/Java/stack Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import java .lang .Math ;
2+ public class Mystack implements Stack {
3+ Object data [];
4+ int top =0 ,cap ;
5+ Mystack (int size ){
6+ data = new Object [size ];
7+ cap = size ;
8+ }
9+ public boolean IsFull (){
10+ if (top ==cap ){
11+ return true ;
12+ }
13+ return false ;
14+ }
15+ public boolean IsEmpty (){
16+ if (top == 0 ){
17+ return true ;
18+ }
19+ return false ;
20+ }
21+ public void Push (Object ele ){
22+ if (IsFull ()){
23+ System .out .println ("Stack is Full, Can't add more elemeants." );
24+ }
25+ data [top ] = ele ;
26+ top ++;
27+ System .out .println ("Element added." );
28+ }
29+ public Object Pop (){
30+ if (IsEmpty ()){
31+ System .out .println ("Stack is empty, add element first." );
32+ return -1 ;
33+ }
34+ top --;
35+ return data [top ];
36+ }
37+ public Object peek (){
38+ if (IsEmpty ()){
39+ System .out .println ("Stack is empty, add element first." );
40+ return -1 ;
41+ }
42+ return data [top -1 ];
43+ }
44+
45+ public int Prec (char ele ){
46+ switch (ele ){
47+ case '+' :
48+ case '-' :
49+ return 1 ;
50+
51+ case '*' :
52+ case '/' :
53+ return 2 ;
54+
55+ case '^' :
56+ return 3 ;
57+ }
58+ return -1 ;
59+ }
60+
61+ public Object opration (int b ,int a ,char c ){
62+ switch (c ){
63+ case '+' :
64+ return a +b ;
65+ case '-' :
66+ return a -b ;
67+ case '*' :
68+ return a *b ;
69+ case '/' :
70+ return a /b ;
71+ // case '^':
72+ // int n=1;
73+ // boolean x=true;
74+ // if(a==0)
75+ // x=false;
76+ // while(x){
77+ // n*=a;
78+ // a--;
79+ // if(a>0)
80+ // x=false;
81+ // }
82+ // return n;
83+ default :
84+ return -1 ;
85+ }
86+ }
87+ }
You can’t perform that action at this time.
0 commit comments