-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStackImplementationUsingArray.java
66 lines (61 loc) · 1.58 KB
/
StackImplementationUsingArray.java
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
// import java.util.Stack;
public class StackImplementationUsingArray {
public static class Stack{
private int arr[]=new int[4];
private int index=0;
void push(int x){
if(isfull()){
System.out.println("stack is empty....");
return;
}
arr[index]=x;
index++;
}
int peek(){
if(index==0){
System.out.println("stack is empty.....");
return -1;
}
return arr[index-1];
}
int pop(){
if(index==0){
System.out.println("stack is empty..");
return -1;
}
int top=arr[index-1];
arr[index-1]=0;
index--;
return top;
}
void display(){
for(int i=0;i<=index-1;i++){
System.out.println(arr[i]+" ");
}
System.out.println();
}
int size(){
return index;
}
boolean isEmpty(){
if(index==0) return true;
else return false;
}
boolean isfull(){
if(index==arr.length) return true;
else return false;
}
}
public static void main(String[] args) {
Stack st=new Stack();
st.push(2);
st.push(4);
st.push(9);
st.push(7);
st.display();
st.pop();
st.push(10);
st.display();
st.size();
}
}