Skip to content

Commit f52dbe1

Browse files
committed
[Funcion add]
1.Add the realization of FIFOQueue and FILOStack.
1 parent e863bd1 commit f52dbe1

File tree

5 files changed

+182
-12
lines changed

5 files changed

+182
-12
lines changed

Algorithm(4th_Edition)/algorithm_note.txt

+76-4
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,37 @@
111111
Integer size();
112112
}
113113

114+
->FIFO队列,通过单向链表实现,新的节点从链表的尾部插入,从链表的头部移出。
115+
实现:ca.mcmaster.chapter.one.FIFO.ListFIFOQueue<T>
116+
public class ListFIFOQueue<T> implements FifoQueue<T> {
117+
private Node first;
118+
private Node last;
119+
private Integer size = 0;
120+
private class Node{
121+
T t;
122+
Node next;
123+
}
124+
public void enqueue(T t) {
125+
Node oldLast = last;
126+
last = new Node();
127+
last.t = t;
128+
last.next = null;
129+
if(isEmpty()) first = last;
130+
else oldLast.next = last;
131+
size ++;
132+
}
133+
public T dequeue() {
134+
if(isEmpty()) throw new IndexOutOfBoundsException();
135+
T t = first.t;
136+
first = first.next;
137+
size--;
138+
if(size.equals(0)) last = null;
139+
return t;
140+
}
141+
public Boolean isEmpty() { return size.equals(0); }
142+
public Integer size() { return size; }
143+
}
144+
114145
11. 栈 LIFO队列:
115146
接口:ca.mcmaster.chapter.one.stuck.Stack<T>
116147
public interface MyStack<T>{
@@ -120,11 +151,12 @@
120151
Integer size();
121152
}
122153

123-
->定容字符串栈:
124-
public class FixedCapacityStackOfString<T> implements MyStack<T> {
154+
->LIFO栈 可动态调整数组大小栈:
155+
实现:ca.mcmaster.chapter.one.stack.ResizingArrayStack<T>
156+
public class ResizingArrayStack<T> implements MyStack<T> {
125157
private T[] a;
126158
private int size;
127-
public FixedCapacityStackOfString(int capacity){ a = (T[])new Object[capacity]; } //无法泛型数组,所以创建一个Object类,再转型为泛型类。
159+
public ResizingArrayStack(int capacity){ a = (T[])new Object[capacity]; } //无法泛型数组,所以创建一个Object类,再转型为泛型类。
128160
public void push(T t) {
129161
if(size == a.length)
130162
this.resize(a.length * 2);
@@ -142,7 +174,47 @@
142174
for(int i = 0; i < size; i++)
143175
temp[i] = a[i];
144176
a = temp;
145-
}
177+
}
178+
private class ReverseArrayIterator implements Iterator<T>{
179+
public boolean hasNext() { return size > 0; }
180+
public T next() { return a[--size]; }
181+
public void remove() {}
182+
}
183+
184+
public Iterator<T> iterator() {
185+
return new ReverseArrayIterator();
186+
}
187+
}
188+
*内部类:
189+
->当前的ReverseArrayIterator就是一个内部类,该类拥有所有对外部类的field的操作权限,并且可以在外部类内部被调用。
190+
->如果外部需要该内部类对象,可以构造一个public的访问器。
191+
->**如果因为内部类和泛型的问题不知道怎么接收到参数,可以考虑使用多态,利用接口接受参数。
192+
193+
->通过单向链表实现的LIFO栈:
194+
实现:ca.mcmaster.chapter.one.stack.ListStack<T>
195+
public class ListStack<T> implements MyStack<T> {
196+
private Node first;
197+
private Integer size = 0;
198+
private class Node{
199+
T t;
200+
Node next;
201+
}
202+
public void push(T t) {
203+
Node oldFirst = first;
204+
first = new Node();
205+
first.t = t;
206+
first.next = oldFirst;
207+
size++;
208+
}
209+
public T pop() {
210+
if(size.equals(0)) throw new IndexOutOfBoundsException();
211+
T t = first.t;
212+
first = first.next;
213+
size--;
214+
return t;
215+
}
216+
public Boolean isEmpty() { return size.equals(0); };
217+
public Integer size() { return size; }
146218
}
147219

148220
12. 装箱拆箱:

Algorithm(4th_Edition)/src/ca/mcmaster/chapter/one/FIFO/FifoQueue.java

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

3-
public interface FifoQueue<T> extends Iterable<T> {
3+
public interface FifoQueue<T>{
44
void enqueue(T t);
55
T dequeue();
66
Boolean isEmpty();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package ca.mcmaster.chapter.one.FIFO;
2+
3+
public class ListFIFOQueue<T> implements FifoQueue<T> {
4+
private Node first;
5+
private Node last;
6+
private Integer size = 0;
7+
private class Node{
8+
T t;
9+
Node next;
10+
}
11+
public void enqueue(T t) {
12+
Node oldLast = last;
13+
last = new Node();
14+
last.t = t;
15+
last.next = null;
16+
if(isEmpty()) first = last;
17+
else oldLast.next = last;
18+
size ++;
19+
}
20+
public T dequeue() {
21+
if(isEmpty()) throw new IndexOutOfBoundsException();
22+
T t = first.t;
23+
first = first.next;
24+
size--;
25+
if(size.equals(0)) last = null;
26+
return t;
27+
}
28+
public Boolean isEmpty() { return size.equals(0); }
29+
public Integer size() { return size; }
30+
31+
public static void main(String[] args){
32+
ListFIFOQueue<Integer> queue = new ListFIFOQueue<>();
33+
for (int i = 0; i < 10; i++) {
34+
queue.enqueue(i);
35+
}
36+
Integer size = queue.size();
37+
for (int i = 0; i < size; i++) {
38+
System.out.println(queue.dequeue());
39+
}
40+
System.out.println(queue.isEmpty());
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package ca.mcmaster.chapter.one.stack;
2+
3+
public class ListStack<T> implements MyStack<T>{
4+
private Node first;
5+
private Integer size = 0;
6+
private class Node{
7+
T t;
8+
Node next;
9+
}
10+
public void push(T t) {
11+
Node oldFirst = first;
12+
first = new Node();
13+
first.t = t;
14+
first.next = oldFirst;
15+
size++;
16+
}
17+
public T pop() {
18+
if(size.equals(0)) throw new IndexOutOfBoundsException();
19+
T t = first.t;
20+
first = first.next;
21+
size--;
22+
return t;
23+
}
24+
public Boolean isEmpty() { return size.equals(0); };
25+
public Integer size() { return size; }
26+
27+
public static void main(String[] args){
28+
ListStack<Integer> stack = new ListStack<>();
29+
for (int i = 0; i < 10; i++) {
30+
stack.push(i);
31+
}
32+
Integer size = stack.size();
33+
for (int i = 0; i < size; i++) {
34+
System.out.println(stack.pop());
35+
}
36+
System.out.println(stack.isEmpty());
37+
}
38+
}

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

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

3-
public class FixedCapacityStackOfString<T> implements MyStack<T> {
3+
import java.util.Iterator;
4+
5+
public class ResizingArrayStack<T> implements MyStack<T>, Iterable<T> {
46
private T[] a;
57
private int size;
68
@SuppressWarnings("unchecked")
7-
public FixedCapacityStackOfString(int capacity){ a = (T[])new Object[capacity]; }
9+
public ResizingArrayStack(int capacity){ a = (T[])new Object[capacity]; }
810
public void push(T t) {
911
if(size == a.length)
1012
this.resize(a.length * 2);
1113
a[size++] = t;
1214
}
1315
public T pop() {
1416
if(size > 0 && size < a.length/4 ) this.resize(a.length / 2);
15-
return (T)a[--size];
17+
T ret = (T)a[--size];
18+
a[size] = null;
19+
return ret;
1620
}
1721
public Boolean isEmpty() { return size == 0;}
1822
public Integer size() { return size;}
@@ -25,15 +29,29 @@ public void resize(int capacity){
2529
a = temp;
2630
}
2731

32+
private class ReverseArrayIterator implements Iterator<T>{
33+
public boolean hasNext() { return size > 0; }
34+
public T next() { return a[--size]; }
35+
public void remove() {}
36+
}
37+
38+
public ReverseArrayIterator iterator() {
39+
return new ReverseArrayIterator();
40+
}
41+
2842
public static void main(String[] args){
29-
FixedCapacityStackOfString<String> stack = new FixedCapacityStackOfString<String>(100);
43+
ResizingArrayStack<String> stack = new ResizingArrayStack<String>(100);
3044
stack.resize(120);
3145
for(int i = 0; i < 10; i ++)
3246
stack.push("" + i);
3347
System.out.println(stack.isEmpty());
34-
int size = stack.size();
35-
for(int i = 0; i < size; i++){
36-
System.out.println(stack.pop());
48+
// int size = stack.size();
49+
// for(int i = 0; i < size; i++){
50+
// System.out.println(stack.pop());
51+
// }
52+
Iterator<String> it = stack.iterator();
53+
while(it.hasNext()){
54+
System.out.println(it.next());
3755
}
3856
System.out.println(stack.isEmpty());
3957
}

0 commit comments

Comments
 (0)