forked from examplehub/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoStack.java
More file actions
123 lines (106 loc) · 2.63 KB
/
TwoStack.java
File metadata and controls
123 lines (106 loc) · 2.63 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package com.examplehub.datastructures.stack;
import java.util.EmptyStackException;
public class TwoStack<E> {
/** The stack to store elements. */
private final E[] stack;
/** The default capacity. */
private static final int DEFAULT_CAPACITY = 10;
/** The capacity of this stack. */
private final int capacity;
/** The first top of first stack. */
private int top1;
/** The second top of second stack. */
private int top2;
public TwoStack() {
this(DEFAULT_CAPACITY);
}
public TwoStack(int capacity) {
@SuppressWarnings("unchecked")
E[] tempStack = (E[]) new Object[capacity];
stack = tempStack;
this.capacity = capacity;
this.top1 = -1;
this.top2 = capacity;
}
/**
* Push item to first stack.
*
* @param item the item to be added to first stack.
* @return {@code true} if added successfully
* @throws EmptyStackException if the stack is full.
*/
public boolean push1(E item) {
if (full()) {
throw new EmptyStackException();
}
stack[++top1] = item;
return true;
}
/**
* Push item to second stack.
*
* @param item the item to be added to second stack.
* @return {@code true} if added successfully
* @throws EmptyStackException if the stack is full.
*/
public boolean push2(E item) {
if (full()) {
throw new EmptyStackException();
}
stack[--top2] = item;
return true;
}
/**
* Remove item from the top of first stack.
*
* @return item at the top of first stack.
* @throws EmptyStackException if first stack is empty.
*/
public E pop1() {
if (top1 == -1) {
throw new EmptyStackException();
}
return stack[top1--];
}
/**
* Remove item from the top of second stack.
*
* @return item at the top of second stack.
* @throws EmptyStackException if second stack is empty.
*/
public E pop2() {
if (top2 == capacity) {
throw new EmptyStackException();
}
return stack[top2++];
}
/**
* Return the number of elements in this stack.
*
* @return size of this stack.
*/
public int size() {
return (top1 + 1) + (capacity - top2);
}
/** Clear all elements in the stack. */
public void clear() {
top1 = -1;
top2 = capacity;
}
/**
* Returns {@code true} if this stack contains no elements.
*
* @return {@code true} if this stack contains no elements, otherwise {@code false}.
*/
public boolean empty() {
return size() == 0;
}
/**
* Test if this stack is full.
*
* @return {@code true} if this stack is full, otherwise {@code false}.
*/
public boolean full() {
return size() == capacity;
}
}