-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackMain.java
More file actions
68 lines (66 loc) · 1.17 KB
/
Copy pathStackMain.java
File metadata and controls
68 lines (66 loc) · 1.17 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
//stack implement in java program in inheritane concept of interface
interface stack{
public void push(int e);
public int pop();
public int peak();
public boolean isEmpty();
public boolean isfull();
}
class MyStack implements stack{
private int top;
private int[] S;
public MyStack(int size){
this.top=-1;
this.S=new int[size];
}
public boolean isEmpty(){
if(this.top==-1){
return true;
}
else{
return false;
}
}
public boolean isfull(){
if(top==(S.length-1)){
return true;
}
else{
return false;
}
}
public int peak(){
if(isEmpty()){
System.out.println("Stack is Empty");
return -1;
}
else{
return (this.S[this.top]);
}
}
public int pop(){
if(isEmpty()){
System.out.println("Stack is Empty");
return -1;
}
else{
return (this.S[this.top--]);
}
}
public void push(int x){
if(isfull()){
System.out.println("Stack overflow");
}
else{
S[++this.top]=x;
}
}
}
class StackMain{
public static void main(String[] args){
MyStack obj=new MyStack(10);
if(!obj.isfull())
obj.push(15);
System.out.println("top item = "+obj.peak());
}
}