Skip to content

Commit c6b11b7

Browse files
author
张加涛
committed
Stack & Queue Completed
1 parent a42e407 commit c6b11b7

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

group08/108621969/com/coding/basic/ArrayList.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ private boolean isFull() {
6363
return false;
6464
}
6565

66-
private boolean isEmpty() {
66+
public boolean isEmpty() {
6767
if(size == 0) {
6868
return true;
6969
}

group08/108621969/com/coding/basic/Queue.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,30 @@
44
* Created by zhangjiatao on 2017/2/25.
55
*/
66
public class Queue {
7+
private ArrayList elementData = new ArrayList();
8+
79
public void enQueue(Object o) {
10+
elementData.add(0, o);
811
}
912

1013
public Object deQueue() {
11-
return null;
14+
return elementData.remove(elementData.size() - 1);
1215
}
1316

1417
public boolean isEmpty() {
15-
return false;
18+
return elementData.isEmpty();
1619
}
1720

1821
public int size() {
19-
return -1;
22+
return elementData.size();
23+
}
24+
25+
public static void main(String[] args) {
26+
Queue queue = new Queue();
27+
queue.enQueue(1);
28+
queue.enQueue(2);
29+
queue.enQueue(3);
30+
System.out.println(queue.deQueue());
31+
System.out.println(queue.toString());
2032
}
2133
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,40 @@
11
package com.coding.basic;
22

3+
import java.util.ArrayList;
4+
5+
36
/**
47
* Created by zhangjiatao on 2017/2/25.
58
*/
69
public class Stack {
10+
private ArrayList elementData = new ArrayList();
11+
12+
public void push(Object o) {
13+
elementData.add(elementData.size(), o);
14+
}
15+
16+
public Object pop() {
17+
return elementData.remove(elementData.size() - 1);
18+
}
19+
20+
public Object peek() {
21+
return elementData.get(elementData.size() - 1);
22+
}
23+
24+
public boolean isEmpty() {
25+
return elementData.isEmpty();
26+
}
27+
28+
public int size() {
29+
return elementData.size();
30+
}
31+
32+
public static void main(String[] args) {
33+
Stack stack = new Stack();
34+
stack.push(1);
35+
stack.push(2);
36+
stack.push(3);
37+
System.out.println(stack.pop());
38+
System.out.println(stack.toString());
39+
}
740
}

0 commit comments

Comments
 (0)