forked from examplehub/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayStack.java
More file actions
121 lines (104 loc) · 2.65 KB
/
ArrayStack.java
File metadata and controls
121 lines (104 loc) · 2.65 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
package com.examplehub.datastructures.stack;
import java.util.EmptyStackException;
import java.util.StringJoiner;
public class ArrayStack<E> {
/** The stack array which store elements. */
private final E[] stack;
/** The number of elements in this stack. */
private int size;
/** The top pointer of this stack. */
private int top;
private final int capacity;
/** Default initial capacity. */
private static final int DEFAULT_CAPACITY = 10;
public ArrayStack() {
this(DEFAULT_CAPACITY);
}
public ArrayStack(int capacity) {
@SuppressWarnings("unchecked")
E[] tempStack = (E[]) new Object[capacity];
stack = tempStack;
this.capacity = capacity;
top = -1;
size = 0;
}
/**
* Pushes an item onto the top of this stack.
*
* @param item the item to be pushed onto this stack.
* @return the {@code item} argument.
*/
public E push(E item) {
if (full()) {
throw new StackOverflowError("Stack is full");
}
stack[++top] = item;
size++;
return item;
}
/**
* Removes the object at the top of this stack and returns that object as the value of this
* function.
*
* @return The object at the top of this stack (the last pushed item).
*/
public E pop() {
if (empty()) {
throw new EmptyStackException();
}
size--;
return stack[top--];
}
/**
* Looks at the object at the top of this stack without removing it from the stack.
*
* @return the object at the top of this stack (the last item pushed).
* @throws EmptyStackException if the stack is empty.
*/
public E peek() {
if (empty()) {
throw new EmptyStackException();
}
return stack[top];
}
/** Clear all elements in the stack. */
public void clear() {
for (int i = 0; i < size; ++i) {
stack[i] = null; /* let GC do its work */
}
top = -1;
size = 0;
}
/**
* Returns the number of elements in this stack.
*
* @return the number of elements in this stack.
*/
public int size() {
return size;
}
/**
* 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;
}
/**
* Returns {@code true} if this stack is full.
*
* @return {@code true} if this stack is full, otherwise {@code false}.
*/
public boolean full() {
return size == capacity;
}
@Override
public String toString() {
StringJoiner joiner = new StringJoiner(", ", "[", "]");
for (int i = 0; i < size; ++i) {
joiner.add(stack[i].toString());
}
return joiner.toString();
}
}