forked from hussien89aa/DataStructureAndAlgorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackDArray.java
More file actions
54 lines (44 loc) · 962 Bytes
/
StackDArray.java
File metadata and controls
54 lines (44 loc) · 962 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
46
47
48
49
50
51
52
53
54
package com.ds;
import java.util.Arrays;
public class StackDArray<T> {
Object[] ArrayStack;
int size;
int top;
public StackDArray(int size) {
this.size=size;
ArrayStack= new Object[this.size];
top=-1;
}
public void push(Object newItem){
ensureCapacity(top+2);
top=top+1;
ArrayStack[top]=newItem;
}
public void ensureCapacity( int minCapacity){
int oldCapacity= getSize();
if(minCapacity>oldCapacity){
int newCapacity=oldCapacity*2;
if(newCapacity<minCapacity)
newCapacity=minCapacity;
ArrayStack=Arrays.copyOf(ArrayStack, newCapacity);
}
}
public Boolean isFull(){
return(top==size-1);
}
public T pop(){
if(isEmplty()){
System.out.println("stack is empty");
return null;
}
T item=(T) ArrayStack[top];
top=top-1;
return item;
}
public Boolean isEmplty(){
return(top==-1);
}
public int getSize(){
return ArrayStack.length;
}
}