-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayStack.java
More file actions
74 lines (64 loc) · 1.46 KB
/
ArrayStack.java
File metadata and controls
74 lines (64 loc) · 1.46 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
69
70
71
72
73
74
public class ArrayStack<E> implements Stack<E> {
private E[] St;
private int size;
public ArrayStack(){
St = (E[]) new Object[3];
size = 0;
}
@Override
public int size() {
// TODO Auto-generated method stub
return size;
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return (size == 0);
}
private void resize () {
E[] temp = (E[]) new Object[St.length *2];
//copy all elements from old array to new array
for (int i =0; i<size; i++){
temp[i] = St[i];
}
//update Arr to reference the new array
St = temp;
}
@Override
public void push(E e) {
if(St.length == size)
this.resize(); //increase the size, if the stack is full
St[size] = (E) e; //put the element to back
size++;
}
@Override
public E top() {
if (isEmpty())
return null;
return St[size - 1];
}
@Override
public E pop() {
if (isEmpty())
return null;
E temp = St[size-1];
St[size-1] = null; //set the last element to null and reduce the size
size--;
return temp;
}
public String toString() {
if (this.isEmpty())
return "{}";
String st = "bottom: {";
//add to the set all elements except the last one
for (int i = 0; i <size-1; i++){
st = st + St[i].toString() + ", ";
}
//add the last element
st = st + St[size-1].toString() + "} :Top";
return st;
}
public void print() {
System.out.println(this.toString());
}
}