Skip to content

Commit e863bd1

Browse files
committed
[Function add]
1.Realized a stack by lusing array.
1 parent a7710b4 commit e863bd1

File tree

4 files changed

+60
-25
lines changed

4 files changed

+60
-25
lines changed

Algorithm(4th_Edition)/algorithm_note.txt

+26-1
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,38 @@
113113

114114
11. 栈 LIFO队列:
115115
接口:ca.mcmaster.chapter.one.stuck.Stack<T>
116-
public interface Stack<T> extends Iterable<T> {
116+
public interface MyStack<T>{
117117
void push(T t);
118118
T pop();
119119
Boolean isEmpty();
120120
Integer size();
121121
}
122122

123+
->定容字符串栈:
124+
public class FixedCapacityStackOfString<T> implements MyStack<T> {
125+
private T[] a;
126+
private int size;
127+
public FixedCapacityStackOfString(int capacity){ a = (T[])new Object[capacity]; } //无法泛型数组,所以创建一个Object类,再转型为泛型类。
128+
public void push(T t) {
129+
if(size == a.length)
130+
this.resize(a.length * 2);
131+
a[size++] = t;
132+
}
133+
public T pop() {
134+
if(size > 0 && size < a.length/4 ) this.resize(a.length / 2);
135+
return (T)a[--size];
136+
}
137+
public Boolean isEmpty() { return size == 0;}
138+
public Integer size() { return size;}
139+
public void resize(int capacity){
140+
if(capacity < size) return;
141+
T[] temp = (T[])new Object[capacity];
142+
for(int i = 0; i < size; i++)
143+
temp[i] = a[i];
144+
a = temp;
145+
}
146+
}
147+
123148
12. 装箱拆箱:
124149
1.基本数据类型和封装数据类型之间的转换。
125150
自动装箱(AutoBoxing):从基本数据类型转化成封装数据类型。int->Integer

Algorithm(4th_Edition)/lib/algs4.jar

1.93 MB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,40 @@
11
package ca.mcmaster.chapter.one.stack;
22

3-
import java.util.Iterator;
4-
5-
public class FixedCapacityStackOfString implements Stack<String> {
6-
private String[] a;
7-
public FixedCapacityStackOfString(int capacity){
8-
a = new String[ca]
9-
}
10-
11-
public Iterator<String> iterator() {
12-
return null;
13-
}
14-
15-
public void push(String t) {
16-
3+
public class FixedCapacityStackOfString<T> implements MyStack<T> {
4+
private T[] a;
5+
private int size;
6+
@SuppressWarnings("unchecked")
7+
public FixedCapacityStackOfString(int capacity){ a = (T[])new Object[capacity]; }
8+
public void push(T t) {
9+
if(size == a.length)
10+
this.resize(a.length * 2);
11+
a[size++] = t;
1712
}
18-
19-
public String pop() {
20-
return null;
13+
public T pop() {
14+
if(size > 0 && size < a.length/4 ) this.resize(a.length / 2);
15+
return (T)a[--size];
2116
}
22-
23-
public Boolean isEmpty() {
24-
return null;
17+
public Boolean isEmpty() { return size == 0;}
18+
public Integer size() { return size;}
19+
@SuppressWarnings("unchecked")
20+
public void resize(int capacity){
21+
if(capacity < size) return;
22+
T[] temp = (T[])new Object[capacity];
23+
for(int i = 0; i < size; i++)
24+
temp[i] = a[i];
25+
a = temp;
2526
}
26-
27-
public Integer size() {
28-
return null;
27+
28+
public static void main(String[] args){
29+
FixedCapacityStackOfString<String> stack = new FixedCapacityStackOfString<String>(100);
30+
stack.resize(120);
31+
for(int i = 0; i < 10; i ++)
32+
stack.push("" + i);
33+
System.out.println(stack.isEmpty());
34+
int size = stack.size();
35+
for(int i = 0; i < size; i++){
36+
System.out.println(stack.pop());
37+
}
38+
System.out.println(stack.isEmpty());
2939
}
3040
}

Algorithm(4th_Edition)/src/ca/mcmaster/chapter/one/stack/Stack.java renamed to Algorithm(4th_Edition)/src/ca/mcmaster/chapter/one/stack/MyStack.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package ca.mcmaster.chapter.one.stack;
22

3-
public interface Stack<T> extends Iterable<T> {
3+
public interface MyStack<T>{
44
void push(T t);
55
T pop();
66
Boolean isEmpty();

0 commit comments

Comments
 (0)