-
Notifications
You must be signed in to change notification settings - Fork 610
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7c60e95
commit b6507b6
Showing
4 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
whatsmars-common/src/test/java/org/hongxi/java/util/collections/ListReverseTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package org.hongxi.java.util.collections; | ||
|
||
/** | ||
* @author shenhongxi 2019/8/12 | ||
*/ | ||
public class ListReverseTest { | ||
|
||
public static void main(String[] args) { | ||
SimpleList<Integer> list = new SimpleList<>(); | ||
list.add(1); | ||
list.add(2); | ||
list.add(3); | ||
System.out.println(list); | ||
list.reverse(); | ||
System.out.println(list); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
whatsmars-common/src/test/java/org/hongxi/java/util/collections/SimpleList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package org.hongxi.java.util.collections; | ||
|
||
/** | ||
* @author shenhongxi 2019/8/12 | ||
* | ||
* @see java.util.LinkedList | ||
*/ | ||
public class SimpleList<E> { | ||
|
||
int size; | ||
Node<E> first; | ||
Node<E> last; | ||
|
||
void add(E e) { | ||
final Node<E> l = last; | ||
final Node<E> newNode = new Node<>(e, null); | ||
last = newNode; | ||
if (l == null) | ||
first = newNode; | ||
else | ||
l.next = newNode; | ||
size++; | ||
} | ||
|
||
void reverse() { | ||
if (first == null) return; | ||
last = first; | ||
Node<E> pre = first; | ||
Node<E> curr = pre.next; | ||
while (curr != null) { | ||
pre.next = curr.next; | ||
curr.next = first; | ||
first = curr; | ||
curr = pre.next; | ||
} | ||
} | ||
|
||
static class Node<E> { | ||
E item; | ||
Node<E> next; | ||
|
||
Node(E item, Node<E> next) { | ||
this.item = item; | ||
this.next = next; | ||
} | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
StringBuilder s = new StringBuilder(); | ||
for (Node<E> p = first; p != null; p = p.next) { | ||
s.append(p.item); | ||
if (p != last) { | ||
s.append(", "); | ||
} | ||
} | ||
return "[" + s.toString() + "]"; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
whatsmars-common/src/test/java/org/hongxi/java/util/collections/SimpleStack.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package org.hongxi.java.util.collections; | ||
|
||
import java.util.ArrayList; | ||
import java.util.EmptyStackException; | ||
import java.util.List; | ||
|
||
/** | ||
* @author shenhongxi 2019/8/12 | ||
* | ||
* @see java.util.Stack | ||
*/ | ||
public class SimpleStack<E> { | ||
|
||
private int size; | ||
|
||
private List<E> elements = new ArrayList<>(); | ||
|
||
public void push(E ele) { | ||
if (elements.size() > size) { | ||
elements.set(size, ele); | ||
} else { | ||
elements.add(ele); | ||
} | ||
size++; | ||
} | ||
|
||
public E pop() { | ||
if (size == 0) { | ||
throw new EmptyStackException(); | ||
} | ||
return elements.set(--size, null); | ||
} | ||
|
||
public int size() { | ||
return size; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
whatsmars-common/src/test/java/org/hongxi/java/util/collections/StackTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package org.hongxi.java.util.collections; | ||
|
||
/** | ||
* @author shenhongxi 2019/8/12 | ||
*/ | ||
public class StackTest { | ||
|
||
public static void main(String[] args) { | ||
SimpleStack<Integer> stack = new SimpleStack<>(); | ||
stack.push(1); | ||
stack.push(2); | ||
stack.push(3); | ||
System.out.println(stack.pop()); | ||
System.out.println(stack.pop()); | ||
System.out.println(stack.pop()); | ||
} | ||
} |