-
Notifications
You must be signed in to change notification settings - Fork 247
Expand file tree
/
Copy pathStackUsingArray.java
More file actions
46 lines (37 loc) · 793 Bytes
/
Copy pathStackUsingArray.java
File metadata and controls
46 lines (37 loc) · 793 Bytes
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
package Stacks;
public class StackUsingArrays {
private int[] data;
private int top;
public static final int Default_Capacity=10;
public StackUsingArrays throws Exception ()
{
this.(Default_Capacity);
}
public StackUsingArrays(int capacity) throws Exception()
{ if(capacity<1)
throw new Exception("Invalid Capacity");
this.data=new int[capacity];
top=-1;
}
public int size()
{
return this.top+1;
}
public boolean isEmpty() {
return this.size()==0;
}
public void push(int value) {
if(this.size()==data.length)
throw new Exception("Stack is Full");
this.top++;
this.data[this.top]=value;
}
public int top() throws Exception{
if(this.size()==0)
throw new Exception("Stack is Empty");
int rv=this.data[this.top];
this.data[this.top]=0;
this.top--;
return rv;
}
}