Skip to content

Commit be1523e

Browse files
author
Liu Yuning
committed
edit author info
1 parent 88d2883 commit be1523e

2 files changed

Lines changed: 94 additions & 93 deletions

File tree

src/containers/Reverse.java

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,54 +8,55 @@
88
/**
99
* 利用迭代器实现反转操作
1010
*
11-
* @author liuyuning
11+
* @author liu yuning
1212
*
1313
*
1414
*/
1515
public class Reverse {
16-
@SuppressWarnings("serial")
17-
private static class ReverseCollection<T> extends ArrayList<T> {
16+
@SuppressWarnings("serial")
17+
private static class ReverseCollection<T> extends ArrayList<T> {
1818

19-
public ReverseCollection(Collection<T> collection) {
20-
super(collection);
21-
}
19+
public ReverseCollection(Collection<T> collection) {
20+
super(collection);
21+
}
2222

23-
public Iterable<T> reverse() {
24-
return new Iterable<T>() {
25-
26-
@Override
27-
public Iterator<T> iterator() {
28-
return new Iterator<T>() {
29-
int current = size() - 1;
30-
31-
@Override
32-
public boolean hasNext() {
33-
return current > -1;
34-
}
35-
36-
@Override
37-
public T next() {
38-
return get(current--);
39-
}
40-
};
41-
}
42-
};
43-
}
23+
public Iterable<T> reverse() {
24+
return new Iterable<T>() {
4425

45-
}
26+
@Override
27+
public Iterator<T> iterator() {
28+
return new Iterator<T>() {
29+
int current = size() - 1;
4630

47-
public static String reverseWords(String s) {
48-
ReverseCollection<String> reverseCollection = new ReverseCollection<String>(Arrays.asList(s.split(" ")));
49-
StringBuilder stringBuilder = new StringBuilder();
50-
for (String word : reverseCollection.reverse()) {
51-
stringBuilder.append(word + " ");
52-
}
53-
return stringBuilder.toString().trim();
31+
@Override
32+
public boolean hasNext() {
33+
return current > -1;
34+
}
5435

36+
@Override
37+
public T next() {
38+
return get(current--);
39+
}
40+
};
41+
}
42+
};
5543
}
5644

57-
public static void main(String[] args) {
58-
String s = "This is a test for reversing words";
59-
System.out.println(reverseWords(s));
45+
}
46+
47+
public static String reverseWords(String s) {
48+
ReverseCollection<String> reverseCollection = new ReverseCollection<String>(
49+
Arrays.asList(s.split(" ")));
50+
StringBuilder stringBuilder = new StringBuilder();
51+
for (String word : reverseCollection.reverse()) {
52+
stringBuilder.append(word + " ");
6053
}
54+
return stringBuilder.toString().trim();
55+
56+
}
57+
58+
public static void main(String[] args) {
59+
String s = "This is a test for reversing words";
60+
System.out.println(reverseWords(s));
61+
}
6162
}

src/datastructure/Stack.java

Lines changed: 55 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3,78 +3,78 @@
33
/**
44
* 使用Java泛型实现栈
55
*
6-
* @author liuyuning
6+
* @author liu yuning
77
*
88
* @param <T>
99
*/
1010
public class Stack<T> {
1111

12-
private static class Node<U> {
13-
private U value;
14-
private Node<U> next;
12+
private static class Node<U> {
13+
private U value;
14+
private Node<U> next;
1515

16-
public Node() {
17-
value = null;
18-
next = null;
19-
}
20-
21-
public Node(U value, Node<U> next) {
22-
this.value = value;
23-
this.next = next;
24-
}
25-
26-
/**
27-
* 判断栈是否为空
28-
*
29-
* @return true 如果为空;否则,false
30-
*/
31-
public boolean empty() {
32-
return value == null && next == null;
33-
}
16+
public Node() {
17+
value = null;
18+
next = null;
3419
}
3520

36-
/**
37-
* 定义栈顶元素
38-
*/
39-
private Node<T> top = new Node<T>();
21+
public Node(U value, Node<U> next) {
22+
this.value = value;
23+
this.next = next;
24+
}
4025

4126
/**
42-
* 入栈操作
27+
* 判断栈是否为空
4328
*
44-
* @param value
45-
* 入栈元素的值
29+
* @return true 如果为空;否则,false
4630
*/
47-
public void push(T value) {
48-
top = new Node<T>(value, top);
31+
public boolean empty() {
32+
return value == null && next == null;
4933
}
34+
}
5035

51-
/**
52-
* 出栈操作
53-
*
54-
* @return 栈顶元素的值
55-
*/
56-
public T pop() {
57-
T value = top.value;
58-
if (!top.empty()) {
59-
top = top.next;
60-
}
61-
return value;
36+
/**
37+
* 定义栈顶元素
38+
*/
39+
private Node<T> top = new Node<T>();
40+
41+
/**
42+
* 入栈操作
43+
*
44+
* @param value
45+
* 入栈元素的值
46+
*/
47+
public void push(T value) {
48+
top = new Node<T>(value, top);
49+
}
50+
51+
/**
52+
* 出栈操作
53+
*
54+
* @return 栈顶元素的值
55+
*/
56+
public T pop() {
57+
T value = top.value;
58+
if (!top.empty()) {
59+
top = top.next;
6260
}
61+
return value;
62+
}
6363

64-
public static void main(String[] args) {
65-
Stack<String> stack = new Stack<String>();
66-
String string = "This is a test for stack";
64+
public static void main(String[] args) {
65+
Stack<String> stack = new Stack<String>();
66+
String string = "This is a test for stack";
6767

68-
System.out.println("入栈元素(String)依次为:");
69-
for (String nodeIn : string.split(" ")) {
70-
stack.push(nodeIn);
71-
System.out.println(nodeIn);
72-
}
68+
System.out.println("入栈元素(String)依次为:");
69+
for (String nodeIn : string.split(" ")) {
70+
stack.push(nodeIn);
71+
System.out.println(nodeIn);
72+
}
7373

74-
System.out.println("出栈元素(String)依次为:");
75-
String nodeOut;
76-
while ((nodeOut = stack.pop()) != null) {
77-
System.out.println(nodeOut);
78-
}
74+
System.out.println("出栈元素(String)依次为:");
75+
String nodeOut;
76+
while ((nodeOut = stack.pop()) != null) {
77+
System.out.println(nodeOut);
7978
}
79+
}
8080
}

0 commit comments

Comments
 (0)