Skip to content

Commit e38a6fe

Browse files
committed
add Stack
1 parent 10b801d commit e38a6fe

File tree

5 files changed

+268
-1
lines changed

5 files changed

+268
-1
lines changed

Array/src/Array.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public E removeFirst(int index) {
133133
}
134134

135135
// 从数组中删除最后元素,返回删除的元素
136-
public E removeLast(int index) {
136+
public E removeLast() {
137137

138138
return remove(size - 1);
139139
}

Stack/src/Array.java

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
public class Array<E> {
2+
3+
private E[] data;
4+
private int size;
5+
6+
/**
7+
* 构造函数,传入数组的容量 capacity 构造 Array
8+
*
9+
* @param capacity
10+
*/
11+
public Array(int capacity) {
12+
data = (E[]) new Object[capacity]; // new E[capacity] 不支持该语法,历史遗留问题
13+
size = 0;
14+
}
15+
16+
// 无参数的构造函数,默认数组的容量 capacity=10
17+
public Array() {
18+
this(10);
19+
}
20+
21+
// 获取数组中的元素个数
22+
public int getSize() {
23+
return size;
24+
}
25+
26+
// 获取数组的容量
27+
public int getCapacity() {
28+
return data.length;
29+
}
30+
31+
// 返回数组是否为空
32+
public boolean isEmpty() {
33+
return size == 0;
34+
}
35+
36+
// 向所有元素后添加一个新元素
37+
public void addLast(E e) {
38+
/*if (size == data.length) {
39+
throw new IllegalArgumentException("AddLast failed. Array is full.");
40+
}
41+
42+
data[size] = e;
43+
size++;*/
44+
add(size, e);
45+
}
46+
47+
// 在所有元素前添加一个新元素
48+
public void addFirst(E e) {
49+
add(0, e);
50+
}
51+
52+
// 在第 index 个位置插入一个新元素 e
53+
public void add(int index, E e) {
54+
55+
if (index < 0 || index > size) {
56+
throw new IllegalArgumentException("add failed. Require index >=0 and index < size.");
57+
}
58+
if (size == data.length) {
59+
resize(2 * data.length);
60+
}
61+
62+
63+
for (int i = size - 1; i >= index; i--) {
64+
data[i + 1] = data[i];
65+
}
66+
data[index] = e;
67+
size++;
68+
}
69+
70+
//获取 index 索引位置的元素
71+
public E get(int index) {
72+
if (index < 0 || index >= size) {
73+
throw new IllegalArgumentException("Get failed. Index is illegal.");
74+
}
75+
return data[index];
76+
}
77+
78+
public E getLast() {
79+
return get(size - 1);
80+
}
81+
82+
public E getFirst() {
83+
return get(0);
84+
}
85+
86+
// 修改 index 索引位置的元素
87+
public void set(int index, E e) {
88+
if (index < 0 || index >= size) {
89+
throw new IllegalArgumentException("Set failed. Index is illegal.");
90+
}
91+
data[index] = e;
92+
}
93+
94+
// 查找数组中是否有元素 e
95+
public boolean contains(E e) {
96+
for (int i = 0; i < size; i++) {
97+
if (data[i].equals(e)) {
98+
return true;
99+
}
100+
}
101+
return false;
102+
}
103+
104+
// 查找数组中元素 e 所在的索引,如果不存在元素 e,则返回 -1
105+
public int find(E e) {
106+
for (int i = 0; i < size; i++) {
107+
if (data[i].equals(e)) {
108+
return i;
109+
}
110+
}
111+
return -1;
112+
}
113+
114+
// 从数组中删除 index 位置的元素,返回删除的元素
115+
public E remove(int index) {
116+
117+
if (index < 0 || index > size) {
118+
throw new IllegalArgumentException("remove failed. Index is illegal.");
119+
}
120+
E ret = data[index];
121+
for (int i = index + 1; i < size; i++) {
122+
data[i - 1] = data[i];
123+
}
124+
size--;
125+
126+
// 该行可选
127+
data[size] = null; // loitering objects != memory leak
128+
129+
if (size == data.length / 4 && data.length / 2 != 0) {
130+
resize(data.length / 2);
131+
}
132+
133+
return ret;
134+
}
135+
136+
137+
// 从数组中删除第一个元素,返回删除的元素
138+
public E removeFirst(int index) {
139+
140+
return remove(0);
141+
}
142+
143+
// 从数组中删除最后元素,返回删除的元素
144+
public E removeLast() {
145+
146+
return remove(size - 1);
147+
}
148+
149+
// 从数组中删除元素e
150+
public void removeElement(E e) {
151+
int index = find(e);
152+
if (index != -1) {
153+
remove(index);
154+
}
155+
}
156+
157+
158+
@Override
159+
public String toString() {
160+
StringBuilder res = new StringBuilder();
161+
res.append(String.format("Array: size = %d, capacity = %d\n", size, data.length));
162+
res.append("[");
163+
for (int i = 0; i < size; i++) {
164+
res.append(data[i]);
165+
if (i != size - 1) {
166+
res.append(", ");
167+
}
168+
}
169+
res.append("]");
170+
return res.toString();
171+
}
172+
173+
174+
private void resize(int newCapacity) {
175+
E[] newData = (E[]) new Object[newCapacity];
176+
for (int i = 0; i < size; i++) {
177+
newData[i] = data[i];
178+
}
179+
data = newData;
180+
}
181+
}

Stack/src/ArrayStack.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
public class ArrayStack<E> implements Stack<E> {
2+
Array<E> array;
3+
4+
public ArrayStack(int capacity) {
5+
array = new Array<>(capacity);
6+
}
7+
8+
public ArrayStack() {
9+
array = new Array<>();
10+
}
11+
12+
@Override
13+
public int getSize() {
14+
return array.getSize();
15+
}
16+
17+
@Override
18+
public boolean isEmpty() {
19+
return array.isEmpty();
20+
}
21+
22+
public int getCapacity() {
23+
return array.getCapacity();
24+
}
25+
26+
@Override
27+
public void push(E e) {
28+
array.addLast(e);
29+
}
30+
31+
@Override
32+
public E pop() {
33+
return array.removeLast();
34+
}
35+
36+
@Override
37+
public E peek() {
38+
return array.getLast();
39+
}
40+
41+
42+
@Override
43+
public String toString() {
44+
StringBuilder res = new StringBuilder();
45+
res.append("Stack: ");
46+
res.append("[");
47+
for (int i = 0; i < array.getSize(); i++) {
48+
res.append(array.get(i));
49+
if (i != array.getSize() - 1) {
50+
res.append(", ");
51+
}
52+
}
53+
res.append("] top");
54+
return res.toString();
55+
}
56+
}

Stack/src/Main.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
public class Main {
2+
public static void main(String[] args) {
3+
ArrayStack<Integer> stack = new ArrayStack<>();
4+
5+
for (int i = 0; i < 5; i++) {
6+
stack.push(i);
7+
System.out.println(stack);
8+
}
9+
10+
stack.pop();
11+
System.out.println(stack);
12+
}
13+
}

Stack/src/Stack.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
public interface Stack<E> {
2+
3+
// 获取栈大小
4+
int getSize();
5+
6+
// 栈是否为空
7+
boolean isEmpty();
8+
9+
// 向栈顶添加元素e
10+
void push(E e);
11+
12+
// 弹出栈顶元素
13+
E pop();
14+
15+
// 查看栈顶元素
16+
E peek();
17+
}

0 commit comments

Comments
 (0)