Compare commits
2 Commits
fd72f740eb
...
042fbd164a
| Author | SHA1 | Date |
|---|---|---|
|
|
042fbd164a | |
|
|
c6b68cd40e |
|
|
@ -0,0 +1,39 @@
|
||||||
|
package com.nbee.solution.practice;
|
||||||
|
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 链表类
|
||||||
|
*/
|
||||||
|
public class SinglyLinkedList {
|
||||||
|
private Node head;
|
||||||
|
private static class Node {
|
||||||
|
int value;
|
||||||
|
Node next;
|
||||||
|
|
||||||
|
Node(int value, Node next) {
|
||||||
|
this.value = value;
|
||||||
|
this.next = next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void addFirst(int value) {
|
||||||
|
//链表为空
|
||||||
|
// head = new Node(value,null);
|
||||||
|
//链表非空,包含了链表为空的情况
|
||||||
|
head = new Node(value,head);
|
||||||
|
}
|
||||||
|
public void loop(Consumer<Integer> consumer) {
|
||||||
|
Node p = head;
|
||||||
|
while (p != null) {
|
||||||
|
consumer.accept(p.value);
|
||||||
|
p = p.next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void loop0 (Consumer<Integer> consumer){
|
||||||
|
for (Node p = head; p != null; p = p.next) {
|
||||||
|
consumer.accept(p.value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -0,0 +1,15 @@
|
||||||
|
package com.nbee.solution.practice;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class SinglyLinkedListTest {
|
||||||
|
private SinglyLinkedList list = new SinglyLinkedList();
|
||||||
|
@Test
|
||||||
|
public void singlyLinkedListAddFirstTest(){
|
||||||
|
list.addFirst(1);
|
||||||
|
list.addFirst(3);
|
||||||
|
list.addFirst(5);
|
||||||
|
list.addFirst(6);
|
||||||
|
list.loop0(i -> System.out.println(i + " "));
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue