-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.java
More file actions
47 lines (39 loc) · 829 Bytes
/
Copy pathStack.java
File metadata and controls
47 lines (39 loc) · 829 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
package StackImplementation;
public class Stack<T> {
private int maxSize;
private T[] storage;
private int top;
public Stack(int size) {
maxSize = size;
storage = (T[])new Object[maxSize];
top = -1;
}
public void push(T value) {
storage[++top] = value;
}
public T pop() {
return storage[top--];
}
public T top() {
if (storage[top] == null)
return storage[--top];
return storage[top];
}
public boolean empty() {
return top == -1;
}
public long size() {
return ++top;
}
public void swap(Stack<T> obj) {
T[] tmp = obj.storage;
obj.storage = storage;
storage = tmp;
int tmp_maxSize = obj.maxSize;
obj.maxSize = obj.storage.length;
maxSize = tmp_maxSize;
int tmp_top = obj.top;
obj.top = top;
top = tmp_top;
}
}