增加淘宝群内容,修改部分文件组织
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>00-leetcode</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
@@ -0,0 +1,11 @@
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
|
||||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||
org.eclipse.jdt.core.compiler.compliance=1.8
|
||||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.source=1.8
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,10 @@
|
||||
package 二叉树;
|
||||
|
||||
public class TreeNode {
|
||||
int val;
|
||||
TreeNode left;
|
||||
TreeNode right;
|
||||
TreeNode(int x) {
|
||||
val = x;
|
||||
}
|
||||
}
|
@@ -0,0 +1,74 @@
|
||||
package 二叉树;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
|
||||
/**
|
||||
* https://leetcode-cn.com/problems/invert-binary-tree/
|
||||
* @author MJ Lee
|
||||
*
|
||||
*/
|
||||
public class _226_翻转二叉树 {
|
||||
|
||||
// public TreeNode invertTree(TreeNode root) {
|
||||
// if (root == null) return root;
|
||||
//
|
||||
// TreeNode tmp = root.left;
|
||||
// root.left = root.right;
|
||||
// root.right = tmp;
|
||||
//
|
||||
// invertTree(root.left);
|
||||
// invertTree(root.right);
|
||||
//
|
||||
// return root;
|
||||
// }
|
||||
|
||||
// public TreeNode invertTree(TreeNode root) {
|
||||
// if (root == null) return root;
|
||||
//
|
||||
// invertTree(root.left);
|
||||
// invertTree(root.right);
|
||||
//
|
||||
// TreeNode tmp = root.left;
|
||||
// root.left = root.right;
|
||||
// root.right = tmp;
|
||||
//
|
||||
// return root;
|
||||
// }
|
||||
|
||||
// public TreeNode invertTree(TreeNode root) {
|
||||
// if (root == null) return root;
|
||||
//
|
||||
// invertTree(root.left);
|
||||
//
|
||||
// TreeNode tmp = root.left;
|
||||
// root.left = root.right;
|
||||
// root.right = tmp;
|
||||
//
|
||||
// invertTree(root.left);
|
||||
//
|
||||
// return root;
|
||||
// }
|
||||
public TreeNode invertTree(TreeNode root) {
|
||||
if (root == null) return root;
|
||||
|
||||
Queue<TreeNode> queue = new LinkedList<>();
|
||||
queue.offer(root);
|
||||
|
||||
while (!queue.isEmpty()) {
|
||||
TreeNode node = queue.poll();
|
||||
TreeNode tmp = node.left;
|
||||
node.left = node.right;
|
||||
node.right = tmp;
|
||||
|
||||
if (node.left != null) {
|
||||
queue.offer(node.left);
|
||||
}
|
||||
|
||||
if (node.right != null) {
|
||||
queue.offer(node.right);
|
||||
}
|
||||
}
|
||||
return root;
|
||||
}
|
||||
}
|
@@ -0,0 +1,63 @@
|
||||
package 栈;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Stack;
|
||||
|
||||
public class _20_有效的括号 {
|
||||
private static HashMap<Character, Character> map = new HashMap<>();
|
||||
static {
|
||||
// key - value
|
||||
map.put('(', ')');
|
||||
map.put('{', '}');
|
||||
map.put('[', ']');
|
||||
}
|
||||
|
||||
public boolean isValid(String s) {
|
||||
Stack<Character> stack = new Stack<>();
|
||||
|
||||
int len = s.length();
|
||||
for (int i = 0; i < len; i++) {
|
||||
char c = s.charAt(i);
|
||||
if (map.containsKey(c)) { // 左括号
|
||||
stack.push(c);
|
||||
} else { // 右括号
|
||||
if (stack.isEmpty()) return false;
|
||||
|
||||
if (c != map.get(stack.pop())) return false;
|
||||
}
|
||||
}
|
||||
return stack.isEmpty();
|
||||
}
|
||||
|
||||
public boolean isValid1(String s) {
|
||||
Stack<Character> stack = new Stack<>();
|
||||
|
||||
int len = s.length();
|
||||
for (int i = 0; i < len; i++) {
|
||||
char c = s.charAt(i);
|
||||
if (c == '(' || c == '{' || c == '[') { // 左括号
|
||||
stack.push(c);
|
||||
} else { // 右括号
|
||||
if (stack.isEmpty()) return false;
|
||||
|
||||
char left = stack.pop();
|
||||
if (left == '(' && c != ')') return false;
|
||||
if (left == '{' && c != '}') return false;
|
||||
if (left == '[' && c != ']') return false;
|
||||
}
|
||||
}
|
||||
return stack.isEmpty();
|
||||
}
|
||||
|
||||
public boolean isValid2(String s) {
|
||||
while (s.contains("{}")
|
||||
|| s.contains("[]")
|
||||
|| s.contains("()")) {
|
||||
s = s.replace("{}", "");
|
||||
s = s.replace("()", "");
|
||||
s = s.replace("[]", "");
|
||||
}
|
||||
return s.isEmpty();
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,44 @@
|
||||
package 栈;
|
||||
|
||||
import java.util.Stack;
|
||||
|
||||
public class _232_用栈实现队列 {
|
||||
private Stack<Integer> inStack;
|
||||
private Stack<Integer> outStack;
|
||||
|
||||
/** Initialize your data structure here. */
|
||||
public _232_用栈实现队列() {
|
||||
inStack = new Stack<>();
|
||||
outStack = new Stack<>();
|
||||
}
|
||||
|
||||
/** 入队 */
|
||||
public void push(int x) {
|
||||
inStack.push(x);
|
||||
}
|
||||
|
||||
/** 出队 */
|
||||
public int pop() {
|
||||
checkOutStack();
|
||||
return outStack.pop();
|
||||
}
|
||||
|
||||
/** 获取队头元素 */
|
||||
public int peek() {
|
||||
checkOutStack();
|
||||
return outStack.peek();
|
||||
}
|
||||
|
||||
/** 是否为空 */
|
||||
public boolean empty() {
|
||||
return inStack.isEmpty() && outStack.isEmpty();
|
||||
}
|
||||
|
||||
private void checkOutStack() {
|
||||
if (outStack.isEmpty()) {
|
||||
while (!inStack.isEmpty()) {
|
||||
outStack.push(inStack.pop());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
package 链表;
|
||||
|
||||
public class ListNode {
|
||||
int val;
|
||||
ListNode next;
|
||||
ListNode(int x) {
|
||||
val = x;
|
||||
}
|
||||
}
|
@@ -0,0 +1,20 @@
|
||||
package 链表;
|
||||
|
||||
public class _141_环形链表 {
|
||||
|
||||
public boolean hasCycle(ListNode head) {
|
||||
if (head == null || head.next == null) return false;
|
||||
|
||||
ListNode slow = head;
|
||||
ListNode fast = head.next;
|
||||
while (fast != null && fast.next != null) {
|
||||
slow = slow.next;
|
||||
fast = fast.next.next;
|
||||
|
||||
if (slow == fast) return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,29 @@
|
||||
package 链表;
|
||||
|
||||
public class _206_反转链表 {
|
||||
|
||||
public ListNode reverseList(ListNode head) {
|
||||
if (head == null || head.next == null) return head;
|
||||
|
||||
ListNode newHead = reverseList(head.next);
|
||||
head.next.next = head;
|
||||
head.next = null;
|
||||
return newHead;
|
||||
}
|
||||
|
||||
|
||||
public ListNode reverseList2(ListNode head) {
|
||||
if (head == null || head.next == null) return head;
|
||||
|
||||
ListNode newHead = null;
|
||||
while (head != null) {
|
||||
ListNode tmp = head.next;
|
||||
head.next = newHead;
|
||||
newHead = head;
|
||||
head = tmp;
|
||||
}
|
||||
|
||||
return newHead;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,14 @@
|
||||
package 链表;
|
||||
|
||||
/**
|
||||
* https://leetcode-cn.com/problems/delete-node-in-a-linked-list/
|
||||
* @author MJ Lee
|
||||
*
|
||||
*/
|
||||
public class _237_删除链表中的节点 {
|
||||
|
||||
public void deleteNode(ListNode node) {
|
||||
node.val = node.next.val;
|
||||
node.next = node.next.next;
|
||||
}
|
||||
}
|
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
17
数据结构(双语)/数据结构 day16【瑞客论坛 www.ruike1.com】/代码/01-复杂度/.project
Normal file
17
数据结构(双语)/数据结构 day16【瑞客论坛 www.ruike1.com】/代码/01-复杂度/.project
Normal file
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>01-复杂度</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
@@ -0,0 +1,11 @@
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
|
||||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||
org.eclipse.jdt.core.compiler.compliance=1.8
|
||||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.source=1.8
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,162 @@
|
||||
package com.mj;
|
||||
|
||||
public class Main {
|
||||
|
||||
/* 0 1 2 3 4 5
|
||||
* 0 1 1 2 3 5 8 13 ....
|
||||
*/
|
||||
|
||||
// O(2^n)
|
||||
public static int fib1(int n) {
|
||||
if (n <= 1) return n;
|
||||
return fib1(n - 1) + fib1(n - 2);
|
||||
}
|
||||
|
||||
// O(n)
|
||||
public static int fib2(int n) {
|
||||
if (n <= 1) return n;
|
||||
|
||||
int first = 0;
|
||||
int second = 1;
|
||||
for (int i = 0; i < n - 1; i++) {
|
||||
int sum = first + second;
|
||||
first = second;
|
||||
second = sum;
|
||||
}
|
||||
return second;
|
||||
}
|
||||
|
||||
public static int fib3(int n) {
|
||||
if (n <= 1) return n;
|
||||
|
||||
int first = 0;
|
||||
int second = 1;
|
||||
while (n-- > 1) {
|
||||
second += first;
|
||||
first = second - first;
|
||||
}
|
||||
return second;
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
int n = 12;
|
||||
|
||||
System.out.println(fib2(n));
|
||||
System.out.println(fib3(n));
|
||||
|
||||
// TimeTool.check("fib1", new Task() {
|
||||
// public void execute() {
|
||||
// System.out.println(fib1(n));
|
||||
// }
|
||||
// });
|
||||
//
|
||||
// TimeTool.check("fib2", new Task() {
|
||||
// public void execute() {
|
||||
// System.out.println(fib2(n));
|
||||
// }
|
||||
// });
|
||||
}
|
||||
|
||||
public static void test1(int n) {
|
||||
// 汇编指令
|
||||
|
||||
// 1
|
||||
if (n > 10) {
|
||||
System.out.println("n > 10");
|
||||
} else if (n > 5) { // 2
|
||||
System.out.println("n > 5");
|
||||
} else {
|
||||
System.out.println("n <= 5");
|
||||
}
|
||||
|
||||
// 1 + 4 + 4 + 4
|
||||
for (int i = 0; i < 4; i++) {
|
||||
System.out.println("test");
|
||||
}
|
||||
|
||||
// 140000
|
||||
// O(1)
|
||||
// O(1)
|
||||
}
|
||||
|
||||
public static void test2(int n) {
|
||||
// O(n)
|
||||
// 1 + 3n
|
||||
for (int i = 0; i < n; i++) {
|
||||
System.out.println("test");
|
||||
}
|
||||
}
|
||||
|
||||
public static void test3(int n) {
|
||||
// 1 + 2n + n * (1 + 3n)
|
||||
// 1 + 2n + n + 3n^2
|
||||
// 3n^2 + 3n + 1
|
||||
// O(n^2)
|
||||
|
||||
// O(n)
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
System.out.println("test");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void test4(int n) {
|
||||
// 1 + 2n + n * (1 + 45)
|
||||
// 1 + 2n + 46n
|
||||
// 48n + 1
|
||||
// O(n)
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < 15; j++) {
|
||||
System.out.println("test");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void test5(int n) {
|
||||
// 8 = 2^3
|
||||
// 16 = 2^4
|
||||
|
||||
// 3 = log2(8)
|
||||
// 4 = log2(16)
|
||||
|
||||
// 执行次数 = log2(n)
|
||||
// O(logn)
|
||||
while ((n = n / 2) > 0) {
|
||||
System.out.println("test");
|
||||
}
|
||||
}
|
||||
|
||||
public static void test6(int n) {
|
||||
// log5(n)
|
||||
// O(logn)
|
||||
while ((n = n / 5) > 0) {
|
||||
System.out.println("test");
|
||||
}
|
||||
}
|
||||
|
||||
public static void test7(int n) {
|
||||
// 1 + 2*log2(n) + log2(n) * (1 + 3n)
|
||||
|
||||
// 1 + 3*log2(n) + 2 * nlog2(n)
|
||||
// O(nlogn)
|
||||
for (int i = 1; i < n; i = i * 2) {
|
||||
// 1 + 3n
|
||||
for (int j = 0; j < n; j++) {
|
||||
System.out.println("test");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void test10(int n) {
|
||||
// O(n)
|
||||
int a = 10;
|
||||
int b = 20;
|
||||
int c = a + b;
|
||||
int[] array = new int[n];
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
System.out.println(array[i] + c);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,26 @@
|
||||
package com.mj;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
public class Times {
|
||||
private static final SimpleDateFormat fmt = new SimpleDateFormat("HH:mm:ss.SSS");
|
||||
|
||||
public interface Task {
|
||||
void execute();
|
||||
}
|
||||
|
||||
public static void test(String title, Task task) {
|
||||
if (task == null) return;
|
||||
title = (title == null) ? "" : ("【" + title + "】");
|
||||
System.out.println(title);
|
||||
System.out.println("开始:" + fmt.format(new Date()));
|
||||
long begin = System.currentTimeMillis();
|
||||
task.execute();
|
||||
long end = System.currentTimeMillis();
|
||||
System.out.println("结束:" + fmt.format(new Date()));
|
||||
double delta = (end - begin) / 1000.0;
|
||||
System.out.println("耗时:" + delta + "秒");
|
||||
System.out.println("-------------------------------------");
|
||||
}
|
||||
}
|
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
17
数据结构(双语)/数据结构 day16【瑞客论坛 www.ruike1.com】/代码/02-动态数组/.project
Normal file
17
数据结构(双语)/数据结构 day16【瑞客论坛 www.ruike1.com】/代码/02-动态数组/.project
Normal file
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>02-动态数组</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
@@ -0,0 +1,11 @@
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
|
||||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||
org.eclipse.jdt.core.compiler.compliance=1.8
|
||||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.source=1.8
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,209 @@
|
||||
package com.mj;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public class ArrayList<E> {
|
||||
/**
|
||||
* 元素的数量
|
||||
*/
|
||||
private int size;
|
||||
/**
|
||||
* 所有的元素
|
||||
*/
|
||||
private E[] elements;
|
||||
|
||||
private static final int DEFAULT_CAPACITY = 10;
|
||||
private static final int ELEMENT_NOT_FOUND = -1;
|
||||
|
||||
public ArrayList(int capaticy) {
|
||||
capaticy = (capaticy < DEFAULT_CAPACITY) ? DEFAULT_CAPACITY : capaticy;
|
||||
elements = (E[]) new Object[capaticy];
|
||||
}
|
||||
|
||||
public ArrayList() {
|
||||
this(DEFAULT_CAPACITY);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除所有元素
|
||||
*/
|
||||
public void clear() {
|
||||
for (int i = 0; i < size; i++) {
|
||||
elements[i] = null;
|
||||
}
|
||||
size = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 元素的数量
|
||||
* @return
|
||||
*/
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否为空
|
||||
* @return
|
||||
*/
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否包含某个元素
|
||||
* @param element
|
||||
* @return
|
||||
*/
|
||||
public boolean contains(E element) {
|
||||
return indexOf(element) != ELEMENT_NOT_FOUND;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加元素到尾部
|
||||
* @param element
|
||||
*/
|
||||
public void add(E element) {
|
||||
add(size, element);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取index位置的元素
|
||||
* @param index
|
||||
* @return
|
||||
*/
|
||||
public E get(int index) {
|
||||
rangeCheck(index);
|
||||
return elements[index];
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置index位置的元素
|
||||
* @param index
|
||||
* @param element
|
||||
* @return 原来的元素ֵ
|
||||
*/
|
||||
public E set(int index, E element) {
|
||||
rangeCheck(index);
|
||||
|
||||
E old = elements[index];
|
||||
elements[index] = element;
|
||||
return old;
|
||||
}
|
||||
|
||||
/**
|
||||
* 在index位置插入一个元素
|
||||
* @param index
|
||||
* @param element
|
||||
*/
|
||||
public void add(int index, E element) {
|
||||
rangeCheckForAdd(index);
|
||||
|
||||
ensureCapacity(size + 1);
|
||||
|
||||
for (int i = size; i > index; i--) {
|
||||
elements[i] = elements[i - 1];
|
||||
}
|
||||
elements[index] = element;
|
||||
size++;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除index位置的元素
|
||||
* @param index
|
||||
* @return
|
||||
*/
|
||||
public E remove(int index) {
|
||||
rangeCheck(index);
|
||||
|
||||
E old = elements[index];
|
||||
for (int i = index + 1; i < size; i++) {
|
||||
elements[i - 1] = elements[i];
|
||||
}
|
||||
elements[--size] = null;
|
||||
return old;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看元素的索引
|
||||
* @param element
|
||||
* @return
|
||||
*/
|
||||
public int indexOf(E element) {
|
||||
if (element == null) { // 1
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (elements[i] == null) return i;
|
||||
}
|
||||
} else {
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (element.equals(elements[i])) return i; // n
|
||||
}
|
||||
}
|
||||
return ELEMENT_NOT_FOUND;
|
||||
}
|
||||
|
||||
// public int indexOf2(E element) {
|
||||
// for (int i = 0; i < size; i++) {
|
||||
// if (valEquals(element, elements[i])) return i; // 2n
|
||||
// }
|
||||
// return ELEMENT_NOT_FOUND;
|
||||
// }
|
||||
//
|
||||
// private boolean valEquals(Object v1, Object v2) {
|
||||
// return v1 == null ? v2 == null : v1.equals(v2);
|
||||
// }
|
||||
|
||||
/**
|
||||
* 保证要有capacity的容量
|
||||
* @param capacity
|
||||
*/
|
||||
private void ensureCapacity(int capacity) {
|
||||
int oldCapacity = elements.length;
|
||||
if (oldCapacity >= capacity) return;
|
||||
|
||||
// 新容量为旧容量的1.5倍
|
||||
int newCapacity = oldCapacity + (oldCapacity >> 1);
|
||||
E[] newElements = (E[]) new Object[newCapacity];
|
||||
for (int i = 0; i < size; i++) {
|
||||
newElements[i] = elements[i];
|
||||
}
|
||||
elements = newElements;
|
||||
|
||||
System.out.println(oldCapacity + "扩容为" + newCapacity);
|
||||
}
|
||||
|
||||
private void outOfBounds(int index) {
|
||||
throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size);
|
||||
}
|
||||
|
||||
private void rangeCheck(int index) {
|
||||
if (index < 0 || index >= size) {
|
||||
outOfBounds(index);
|
||||
}
|
||||
}
|
||||
|
||||
private void rangeCheckForAdd(int index) {
|
||||
if (index < 0 || index > size) {
|
||||
outOfBounds(index);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
// size=3, [99, 88, 77]
|
||||
StringBuilder string = new StringBuilder();
|
||||
string.append("size=").append(size).append(", [");
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (i != 0) {
|
||||
string.append(", ");
|
||||
}
|
||||
|
||||
string.append(elements[i]);
|
||||
|
||||
// if (i != size - 1) {
|
||||
// string.append(", ");
|
||||
// }
|
||||
}
|
||||
string.append("]");
|
||||
return string.toString();
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
package com.mj;
|
||||
|
||||
public class Asserts {
|
||||
public static void test(boolean value) {
|
||||
try {
|
||||
if (!value) throw new Exception("测试未通过");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,5 @@
|
||||
package com.mj;
|
||||
|
||||
public class Car {
|
||||
|
||||
}
|
@@ -0,0 +1,47 @@
|
||||
package com.mj;
|
||||
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
ArrayList<Object> list = new ArrayList<>();
|
||||
list.add(10);
|
||||
list.add(new Person(10, "Jack"));
|
||||
list.add(22);
|
||||
|
||||
list.indexOf(new Person(10, "Jack"));
|
||||
|
||||
|
||||
// ArrayList<Object> persons = new ArrayList<>();
|
||||
// persons.add(new Person(10, "Jack"));
|
||||
// persons.add(null);
|
||||
// persons.add(new Person(15, "Rose"));
|
||||
// persons.add(null);
|
||||
// persons.add(new Person(12, "James"));
|
||||
// persons.add(null);
|
||||
//
|
||||
// System.out.println(persons.indexOf(null));
|
||||
}
|
||||
|
||||
static void test() {
|
||||
// int -> Integer
|
||||
|
||||
// 所有的类,最终都继承java.lang.Object
|
||||
|
||||
// new是向堆空间申请内存
|
||||
ArrayList<Person> persons = new ArrayList<>();
|
||||
persons.add(new Person(10, "Jack"));
|
||||
persons.add(new Person(12, "James"));
|
||||
persons.add(new Person(15, "Rose"));
|
||||
persons.clear();
|
||||
persons.add(new Person(22, "abc"));
|
||||
|
||||
System.out.println(persons);
|
||||
ArrayList<Integer> ints = new ArrayList<>();
|
||||
ints.add(10);
|
||||
ints.add(10);
|
||||
ints.add(22);
|
||||
ints.add(33);
|
||||
System.out.println(ints);
|
||||
}
|
||||
}
|
@@ -0,0 +1,33 @@
|
||||
package com.mj;
|
||||
|
||||
public class Person {
|
||||
private int age;
|
||||
private String name;
|
||||
|
||||
public Person(int age, String name) {
|
||||
this.age = age;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Person [age=" + age + ", name=" + name + "]";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void finalize() throws Throwable {
|
||||
super.finalize();
|
||||
|
||||
System.out.println("Person - finalize");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == null) return false;
|
||||
if (obj instanceof Person) {
|
||||
Person person = (Person) obj;
|
||||
return this.age == person.age;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
17
数据结构(双语)/数据结构 day16【瑞客论坛 www.ruike1.com】/代码/03-链表/.project
Normal file
17
数据结构(双语)/数据结构 day16【瑞客论坛 www.ruike1.com】/代码/03-链表/.project
Normal file
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>03-链表</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
@@ -0,0 +1,11 @@
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
|
||||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||
org.eclipse.jdt.core.compiler.compliance=1.8
|
||||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.source=1.8
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,56 @@
|
||||
package com.mj;
|
||||
|
||||
public abstract class AbstractList<E> implements List<E> {
|
||||
/**
|
||||
* 元素的数量
|
||||
*/
|
||||
protected int size;
|
||||
/**
|
||||
* 元素的数量
|
||||
* @return
|
||||
*/
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否为空
|
||||
* @return
|
||||
*/
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否包含某个元素
|
||||
* @param element
|
||||
* @return
|
||||
*/
|
||||
public boolean contains(E element) {
|
||||
return indexOf(element) != ELEMENT_NOT_FOUND;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加元素到尾部
|
||||
* @param element
|
||||
*/
|
||||
public void add(E element) {
|
||||
add(size, element);
|
||||
}
|
||||
|
||||
protected void outOfBounds(int index) {
|
||||
throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size);
|
||||
}
|
||||
|
||||
protected void rangeCheck(int index) {
|
||||
if (index < 0 || index >= size) {
|
||||
outOfBounds(index);
|
||||
}
|
||||
}
|
||||
|
||||
protected void rangeCheckForAdd(int index) {
|
||||
if (index < 0 || index > size) {
|
||||
outOfBounds(index);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,154 @@
|
||||
package com.mj;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public class ArrayList<E> extends AbstractList<E> {
|
||||
/**
|
||||
* 所有的元素
|
||||
*/
|
||||
private E[] elements;
|
||||
private static final int DEFAULT_CAPACITY = 10;
|
||||
|
||||
public ArrayList(int capaticy) {
|
||||
capaticy = (capaticy < DEFAULT_CAPACITY) ? DEFAULT_CAPACITY : capaticy;
|
||||
elements = (E[]) new Object[capaticy];
|
||||
}
|
||||
|
||||
public ArrayList() {
|
||||
this(DEFAULT_CAPACITY);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除所有元素
|
||||
*/
|
||||
public void clear() {
|
||||
for (int i = 0; i < size; i++) {
|
||||
elements[i] = null;
|
||||
}
|
||||
size = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取index位置的元素
|
||||
* @param index
|
||||
* @return
|
||||
*/
|
||||
public E get(int index) { // O(1)
|
||||
rangeCheck(index);
|
||||
|
||||
return elements[index];
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置index位置的元素
|
||||
* @param index
|
||||
* @param element
|
||||
* @return 原来的元素ֵ
|
||||
*/
|
||||
public E set(int index, E element) { // O(1)
|
||||
rangeCheck(index);
|
||||
|
||||
E old = elements[index];
|
||||
elements[index] = element;
|
||||
return old;
|
||||
}
|
||||
|
||||
/**
|
||||
* 在index位置插入一个元素
|
||||
* @param index
|
||||
* @param element
|
||||
*/
|
||||
public void add(int index, E element) {
|
||||
/*
|
||||
* 最好:O(1)
|
||||
* 最坏:O(n)
|
||||
* 平均:O(n)
|
||||
*/
|
||||
rangeCheckForAdd(index);
|
||||
|
||||
ensureCapacity(size + 1);
|
||||
|
||||
for (int i = size; i > index; i--) {
|
||||
elements[i] = elements[i - 1];
|
||||
}
|
||||
elements[index] = element;
|
||||
size++;
|
||||
} // size是数据规模
|
||||
|
||||
/**
|
||||
* 删除index位置的元素
|
||||
* @param index
|
||||
* @return
|
||||
*/
|
||||
public E remove(int index) {
|
||||
/*
|
||||
* 最好:O(1)
|
||||
* 最坏:O(n)
|
||||
* 平均:O(n)
|
||||
*/
|
||||
rangeCheck(index);
|
||||
|
||||
E old = elements[index];
|
||||
for (int i = index + 1; i < size; i++) {
|
||||
elements[i - 1] = elements[i];
|
||||
}
|
||||
elements[--size] = null;
|
||||
return old;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看元素的索引
|
||||
* @param element
|
||||
* @return
|
||||
*/
|
||||
public int indexOf(E element) {
|
||||
if (element == null) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (elements[i] == null) return i;
|
||||
}
|
||||
} else {
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (element.equals(elements[i])) return i;
|
||||
}
|
||||
}
|
||||
return ELEMENT_NOT_FOUND;
|
||||
}
|
||||
|
||||
/**
|
||||
* 保证要有capacity的容量
|
||||
* @param capacity
|
||||
*/
|
||||
private void ensureCapacity(int capacity) {
|
||||
int oldCapacity = elements.length;
|
||||
if (oldCapacity >= capacity) return;
|
||||
|
||||
// 新容量为旧容量的1.5倍
|
||||
int newCapacity = oldCapacity + (oldCapacity >> 1);
|
||||
E[] newElements = (E[]) new Object[newCapacity];
|
||||
for (int i = 0; i < size; i++) {
|
||||
newElements[i] = elements[i];
|
||||
}
|
||||
elements = newElements;
|
||||
|
||||
System.out.println(oldCapacity + "扩容为" + newCapacity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
// size=3, [99, 88, 77]
|
||||
StringBuilder string = new StringBuilder();
|
||||
string.append("size=").append(size).append(", [");
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (i != 0) {
|
||||
string.append(", ");
|
||||
}
|
||||
|
||||
string.append(elements[i]);
|
||||
|
||||
// if (i != size - 1) {
|
||||
// string.append(", ");
|
||||
// }
|
||||
}
|
||||
string.append("]");
|
||||
return string.toString();
|
||||
}
|
||||
}
|
@@ -0,0 +1,189 @@
|
||||
package com.mj;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
|
||||
/**
|
||||
* 有动态缩容操作
|
||||
* @author MJ Lee
|
||||
*
|
||||
* @param <E>
|
||||
*/
|
||||
public class ArrayList2<E> extends AbstractList<E> {
|
||||
/**
|
||||
* 所有的元素
|
||||
*/
|
||||
private E[] elements;
|
||||
private static final int DEFAULT_CAPACITY = 10;
|
||||
|
||||
public ArrayList2(int capaticy) {
|
||||
capaticy = (capaticy < DEFAULT_CAPACITY) ? DEFAULT_CAPACITY : capaticy;
|
||||
elements = (E[]) new Object[capaticy];
|
||||
}
|
||||
|
||||
public ArrayList2() {
|
||||
this(DEFAULT_CAPACITY);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除所有元素
|
||||
*/
|
||||
public void clear() {
|
||||
for (int i = 0; i < size; i++) {
|
||||
elements[i] = null;
|
||||
}
|
||||
size = 0;
|
||||
|
||||
// 仅供参考
|
||||
if (elements != null && elements.length > DEFAULT_CAPACITY) {
|
||||
elements = (E[]) new Object[DEFAULT_CAPACITY];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取index位置的元素
|
||||
* @param index
|
||||
* @return
|
||||
*/
|
||||
public E get(int index) { // O(1)
|
||||
rangeCheck(index);
|
||||
|
||||
return elements[index];
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置index位置的元素
|
||||
* @param index
|
||||
* @param element
|
||||
* @return 原来的元素ֵ
|
||||
*/
|
||||
public E set(int index, E element) { // O(1)
|
||||
rangeCheck(index);
|
||||
|
||||
E old = elements[index];
|
||||
elements[index] = element;
|
||||
return old;
|
||||
}
|
||||
|
||||
/**
|
||||
* 在index位置插入一个元素
|
||||
* @param index
|
||||
* @param element
|
||||
*/
|
||||
public void add(int index, E element) {
|
||||
/*
|
||||
* 最好:O(1)
|
||||
* 最坏:O(n)
|
||||
* 平均:O(n)
|
||||
*/
|
||||
rangeCheckForAdd(index);
|
||||
|
||||
ensureCapacity(size + 1);
|
||||
|
||||
for (int i = size; i > index; i--) {
|
||||
elements[i] = elements[i - 1];
|
||||
}
|
||||
elements[index] = element;
|
||||
size++;
|
||||
} // size是数据规模
|
||||
|
||||
/**
|
||||
* 删除index位置的元素
|
||||
* @param index
|
||||
* @return
|
||||
*/
|
||||
public E remove(int index) {
|
||||
/*
|
||||
* 最好:O(1)
|
||||
* 最坏:O(n)
|
||||
* 平均:O(n)
|
||||
*/
|
||||
rangeCheck(index);
|
||||
|
||||
E old = elements[index];
|
||||
for (int i = index + 1; i < size; i++) {
|
||||
elements[i - 1] = elements[i];
|
||||
}
|
||||
elements[--size] = null;
|
||||
|
||||
trim();
|
||||
|
||||
return old;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看元素的索引
|
||||
* @param element
|
||||
* @return
|
||||
*/
|
||||
public int indexOf(E element) {
|
||||
if (element == null) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (elements[i] == null) return i;
|
||||
}
|
||||
} else {
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (element.equals(elements[i])) return i;
|
||||
}
|
||||
}
|
||||
return ELEMENT_NOT_FOUND;
|
||||
}
|
||||
|
||||
/**
|
||||
* 保证要有capacity的容量
|
||||
* @param capacity
|
||||
*/
|
||||
private void ensureCapacity(int capacity) {
|
||||
int oldCapacity = elements.length;
|
||||
if (oldCapacity >= capacity) return;
|
||||
|
||||
// 新容量为旧容量的1.5倍
|
||||
int newCapacity = oldCapacity + (oldCapacity >> 1);
|
||||
|
||||
// 新容量为旧容量的2倍
|
||||
// int newCapacity = oldCapacity << 1;
|
||||
E[] newElements = (E[]) new Object[newCapacity];
|
||||
for (int i = 0; i < size; i++) {
|
||||
newElements[i] = elements[i];
|
||||
}
|
||||
elements = newElements;
|
||||
|
||||
System.out.println(oldCapacity + "扩容为" + newCapacity);
|
||||
}
|
||||
|
||||
private void trim() {
|
||||
// 30
|
||||
int oldCapacity = elements.length;
|
||||
// 15
|
||||
int newCapacity = oldCapacity >> 1;
|
||||
if (size > (newCapacity) || oldCapacity <= DEFAULT_CAPACITY) return;
|
||||
|
||||
// 剩余空间还很多
|
||||
E[] newElements = (E[]) new Object[newCapacity];
|
||||
for (int i = 0; i < size; i++) {
|
||||
newElements[i] = elements[i];
|
||||
}
|
||||
elements = newElements;
|
||||
|
||||
System.out.println(oldCapacity + "缩容为" + newCapacity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
// size=3, [99, 88, 77]
|
||||
StringBuilder string = new StringBuilder();
|
||||
string.append("size=").append(size).append(", [");
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (i != 0) {
|
||||
string.append(", ");
|
||||
}
|
||||
|
||||
string.append(elements[i]);
|
||||
|
||||
// if (i != size - 1) {
|
||||
// string.append(", ");
|
||||
// }
|
||||
}
|
||||
string.append("]");
|
||||
return string.toString();
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
package com.mj;
|
||||
|
||||
public class Asserts {
|
||||
public static void test(boolean value) {
|
||||
try {
|
||||
if (!value) throw new Exception("测试未通过");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,175 @@
|
||||
package com.mj;
|
||||
|
||||
import com.mj.AbstractList;
|
||||
|
||||
public class LinkedList<E> extends AbstractList<E> {
|
||||
private Node<E> first;
|
||||
private Node<E> last;
|
||||
|
||||
private static class Node<E> {
|
||||
E element;
|
||||
Node<E> prev;
|
||||
Node<E> next;
|
||||
public Node(Node<E> prev, E element, Node<E> next) {
|
||||
this.prev = prev;
|
||||
this.element = element;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
if (prev != null) {
|
||||
sb.append(prev.element);
|
||||
} else {
|
||||
sb.append("null");
|
||||
}
|
||||
|
||||
sb.append("_").append(element).append("_");
|
||||
|
||||
if (next != null) {
|
||||
sb.append(next.element);
|
||||
} else {
|
||||
sb.append("null");
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
size = 0;
|
||||
first = null;
|
||||
last = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E get(int index) {
|
||||
return node(index).element;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E set(int index, E element) {
|
||||
Node<E> node = node(index);
|
||||
E old = node.element;
|
||||
node.element = element;
|
||||
return old;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(int index, E element) {
|
||||
rangeCheckForAdd(index);
|
||||
|
||||
// size == 0
|
||||
// index == 0
|
||||
if (index == size) { // 往最后面添加元素
|
||||
Node<E> oldLast = last;
|
||||
last = new Node<>(oldLast, element, null);
|
||||
if (oldLast == null) { // 这是链表添加的第一个元素
|
||||
first = last;
|
||||
} else {
|
||||
oldLast.next = last;
|
||||
}
|
||||
} else {
|
||||
Node<E> next = node(index);
|
||||
Node<E> prev = next.prev;
|
||||
Node<E> node = new Node<>(prev, element, next);
|
||||
next.prev = node;
|
||||
|
||||
if (prev == null) { // index == 0
|
||||
first = node;
|
||||
} else {
|
||||
prev.next = node;
|
||||
}
|
||||
}
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E remove(int index) {
|
||||
rangeCheck(index);
|
||||
|
||||
Node<E> node = node(index);
|
||||
Node<E> prev = node.prev;
|
||||
Node<E> next = node.next;
|
||||
|
||||
if (prev == null) { // index == 0
|
||||
first = next;
|
||||
} else {
|
||||
prev.next = next;
|
||||
}
|
||||
|
||||
if (next == null) { // index == size - 1
|
||||
last = prev;
|
||||
} else {
|
||||
next.prev = prev;
|
||||
}
|
||||
|
||||
size--;
|
||||
return node.element;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int indexOf(E element) {
|
||||
if (element == null) {
|
||||
Node<E> node = first;
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (node.element == null) return i;
|
||||
|
||||
node = node.next;
|
||||
}
|
||||
} else {
|
||||
Node<E> node = first;
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (element.equals(node.element)) return i;
|
||||
|
||||
node = node.next;
|
||||
}
|
||||
}
|
||||
return ELEMENT_NOT_FOUND;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取index位置对应的节点对象
|
||||
* @param index
|
||||
* @return
|
||||
*/
|
||||
private Node<E> node(int index) {
|
||||
rangeCheck(index);
|
||||
|
||||
if (index < (size >> 1)) {
|
||||
Node<E> node = first;
|
||||
for (int i = 0; i < index; i++) {
|
||||
node = node.next;
|
||||
}
|
||||
return node;
|
||||
} else {
|
||||
Node<E> node = last;
|
||||
for (int i = size - 1; i > index; i--) {
|
||||
node = node.prev;
|
||||
}
|
||||
return node;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder string = new StringBuilder();
|
||||
string.append("size=").append(size).append(", [");
|
||||
Node<E> node = first;
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (i != 0) {
|
||||
string.append(", ");
|
||||
}
|
||||
|
||||
string.append(node);
|
||||
|
||||
node = node.next;
|
||||
}
|
||||
string.append("]");
|
||||
return string.toString();
|
||||
}
|
||||
}
|
@@ -0,0 +1,70 @@
|
||||
package com.mj;
|
||||
|
||||
public interface List<E> {
|
||||
static final int ELEMENT_NOT_FOUND = -1;
|
||||
/**
|
||||
* 清除所有元素
|
||||
*/
|
||||
void clear();
|
||||
|
||||
/**
|
||||
* 元素的数量
|
||||
* @return
|
||||
*/
|
||||
int size();
|
||||
|
||||
/**
|
||||
* 是否为空
|
||||
* @return
|
||||
*/
|
||||
boolean isEmpty();
|
||||
|
||||
/**
|
||||
* 是否包含某个元素
|
||||
* @param element
|
||||
* @return
|
||||
*/
|
||||
boolean contains(E element);
|
||||
|
||||
/**
|
||||
* 添加元素到尾部
|
||||
* @param element
|
||||
*/
|
||||
void add(E element);
|
||||
|
||||
/**
|
||||
* 获取index位置的元素
|
||||
* @param index
|
||||
* @return
|
||||
*/
|
||||
E get(int index);
|
||||
|
||||
/**
|
||||
* 设置index位置的元素
|
||||
* @param index
|
||||
* @param element
|
||||
* @return 原来的元素ֵ
|
||||
*/
|
||||
E set(int index, E element);
|
||||
|
||||
/**
|
||||
* 在index位置插入一个元素
|
||||
* @param index
|
||||
* @param element
|
||||
*/
|
||||
void add(int index, E element);
|
||||
|
||||
/**
|
||||
* 删除index位置的元素
|
||||
* @param index
|
||||
* @return
|
||||
*/
|
||||
E remove(int index);
|
||||
|
||||
/**
|
||||
* 查看元素的索引
|
||||
* @param element
|
||||
* @return
|
||||
*/
|
||||
int indexOf(E element);
|
||||
}
|
@@ -0,0 +1,64 @@
|
||||
package com.mj;
|
||||
|
||||
import com.mj.circle.CircleLinkedList;
|
||||
|
||||
public class Main {
|
||||
|
||||
static void testList(List<Integer> list) {
|
||||
list.add(11);
|
||||
list.add(22);
|
||||
list.add(33);
|
||||
list.add(44);
|
||||
|
||||
list.add(0, 55); // [55, 11, 22, 33, 44]
|
||||
list.add(2, 66); // [55, 11, 66, 22, 33, 44]
|
||||
list.add(list.size(), 77); // [55, 11, 66, 22, 33, 44, 77]
|
||||
|
||||
list.remove(0); // [11, 66, 22, 33, 44, 77]
|
||||
list.remove(2); // [11, 66, 33, 44, 77]
|
||||
list.remove(list.size() - 1); // [11, 66, 33, 44]
|
||||
|
||||
Asserts.test(list.indexOf(44) == 3);
|
||||
Asserts.test(list.indexOf(22) == List.ELEMENT_NOT_FOUND);
|
||||
Asserts.test(list.contains(33));
|
||||
Asserts.test(list.get(0) == 11);
|
||||
Asserts.test(list.get(1) == 66);
|
||||
Asserts.test(list.get(list.size() - 1) == 44);
|
||||
|
||||
System.out.println(list);
|
||||
}
|
||||
|
||||
static void josephus() {
|
||||
CircleLinkedList<Integer> list = new CircleLinkedList<>();
|
||||
for (int i = 1; i <= 8; i++) {
|
||||
list.add(i);
|
||||
}
|
||||
|
||||
// 指向头结点(指向1)
|
||||
list.reset();
|
||||
|
||||
while (!list.isEmpty()) {
|
||||
list.next();
|
||||
list.next();
|
||||
System.out.println(list.remove());
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
josephus();
|
||||
|
||||
// testList(new ArrayList<>());
|
||||
// testList(new LinkedList<>());
|
||||
|
||||
|
||||
// testList(new SingleCircleLinkedList<>());
|
||||
|
||||
// testList(new CircleLinkedList<>());
|
||||
|
||||
/*
|
||||
* gc root对象
|
||||
* 1> 被局部变量指向的对象
|
||||
*/
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,208 @@
|
||||
package com.mj.circle;
|
||||
|
||||
import com.mj.AbstractList;
|
||||
|
||||
public class CircleLinkedList<E> extends AbstractList<E> {
|
||||
private Node<E> first;
|
||||
private Node<E> last;
|
||||
private Node<E> current;
|
||||
|
||||
private static class Node<E> {
|
||||
E element;
|
||||
Node<E> prev;
|
||||
Node<E> next;
|
||||
public Node(Node<E> prev, E element, Node<E> next) {
|
||||
this.prev = prev;
|
||||
this.element = element;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
if (prev != null) {
|
||||
sb.append(prev.element);
|
||||
} else {
|
||||
sb.append("null");
|
||||
}
|
||||
|
||||
sb.append("_").append(element).append("_");
|
||||
|
||||
if (next != null) {
|
||||
sb.append(next.element);
|
||||
} else {
|
||||
sb.append("null");
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
current = first;
|
||||
}
|
||||
|
||||
public E next() {
|
||||
if (current == null) return null;
|
||||
|
||||
current = current.next;
|
||||
return current.element;
|
||||
}
|
||||
|
||||
public E remove() {
|
||||
if (current == null) return null;
|
||||
|
||||
Node<E> next = current.next;
|
||||
E element = remove(current);
|
||||
if (size == 0) {
|
||||
current = null;
|
||||
} else {
|
||||
current = next;
|
||||
}
|
||||
|
||||
return element;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
size = 0;
|
||||
first = null;
|
||||
last = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E get(int index) {
|
||||
return node(index).element;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E set(int index, E element) {
|
||||
Node<E> node = node(index);
|
||||
E old = node.element;
|
||||
node.element = element;
|
||||
return old;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(int index, E element) {
|
||||
rangeCheckForAdd(index);
|
||||
|
||||
// size == 0
|
||||
// index == 0
|
||||
if (index == size) { // 往最后面添加元素
|
||||
Node<E> oldLast = last;
|
||||
last = new Node<>(oldLast, element, first);
|
||||
if (oldLast == null) { // 这是链表添加的第一个元素
|
||||
first = last;
|
||||
first.next = first;
|
||||
first.prev = first;
|
||||
} else {
|
||||
oldLast.next = last;
|
||||
first.prev = last;
|
||||
}
|
||||
} else {
|
||||
Node<E> next = node(index);
|
||||
Node<E> prev = next.prev;
|
||||
Node<E> node = new Node<>(prev, element, next);
|
||||
next.prev = node;
|
||||
prev.next = node;
|
||||
|
||||
if (next == first) { // index == 0
|
||||
first = node;
|
||||
}
|
||||
}
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E remove(int index) {
|
||||
rangeCheck(index);
|
||||
return remove(node(index));
|
||||
}
|
||||
|
||||
private E remove(Node<E> node) {
|
||||
if (size == 1) {
|
||||
first = null;
|
||||
last = null;
|
||||
} else {
|
||||
Node<E> prev = node.prev;
|
||||
Node<E> next = node.next;
|
||||
prev.next = next;
|
||||
next.prev = prev;
|
||||
|
||||
if (node == first) { // index == 0
|
||||
first = next;
|
||||
}
|
||||
|
||||
if (node == last) { // index == size - 1
|
||||
last = prev;
|
||||
}
|
||||
}
|
||||
|
||||
size--;
|
||||
return node.element;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int indexOf(E element) {
|
||||
if (element == null) {
|
||||
Node<E> node = first;
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (node.element == null) return i;
|
||||
|
||||
node = node.next;
|
||||
}
|
||||
} else {
|
||||
Node<E> node = first;
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (element.equals(node.element)) return i;
|
||||
|
||||
node = node.next;
|
||||
}
|
||||
}
|
||||
return ELEMENT_NOT_FOUND;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取index位置对应的节点对象
|
||||
* @param index
|
||||
* @return
|
||||
*/
|
||||
private Node<E> node(int index) {
|
||||
rangeCheck(index);
|
||||
|
||||
if (index < (size >> 1)) {
|
||||
Node<E> node = first;
|
||||
for (int i = 0; i < index; i++) {
|
||||
node = node.next;
|
||||
}
|
||||
return node;
|
||||
} else {
|
||||
Node<E> node = last;
|
||||
for (int i = size - 1; i > index; i--) {
|
||||
node = node.prev;
|
||||
}
|
||||
return node;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder string = new StringBuilder();
|
||||
string.append("size=").append(size).append(", [");
|
||||
Node<E> node = first;
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (i != 0) {
|
||||
string.append(", ");
|
||||
}
|
||||
|
||||
string.append(node);
|
||||
|
||||
node = node.next;
|
||||
}
|
||||
string.append("]");
|
||||
return string.toString();
|
||||
}
|
||||
}
|
@@ -0,0 +1,135 @@
|
||||
package com.mj.circle;
|
||||
|
||||
import com.mj.AbstractList;
|
||||
|
||||
public class SingleCircleLinkedList<E> extends AbstractList<E> {
|
||||
private Node<E> first;
|
||||
|
||||
private static class Node<E> {
|
||||
E element;
|
||||
Node<E> next;
|
||||
public Node(E element, Node<E> next) {
|
||||
this.element = element;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(element).append("_").append(next.element);
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
size = 0;
|
||||
first = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E get(int index) {
|
||||
return node(index).element;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E set(int index, E element) {
|
||||
Node<E> node = node(index);
|
||||
E old = node.element;
|
||||
node.element = element;
|
||||
return old;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(int index, E element) {
|
||||
rangeCheckForAdd(index);
|
||||
|
||||
if (index == 0) {
|
||||
Node<E> newFirst = new Node<>(element, first);
|
||||
// 拿到最后一个节点
|
||||
Node<E> last = (size == 0) ? newFirst : node(size - 1);
|
||||
last.next = newFirst;
|
||||
first = newFirst;
|
||||
} else {
|
||||
Node<E> prev = node(index - 1);
|
||||
prev.next = new Node<>(element, prev.next);
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E remove(int index) {
|
||||
rangeCheck(index);
|
||||
|
||||
Node<E> node = first;
|
||||
if (index == 0) {
|
||||
if (size == 1) {
|
||||
first = null;
|
||||
} else {
|
||||
Node<E> last = node(size - 1);
|
||||
first = first.next;
|
||||
last.next = first;
|
||||
}
|
||||
} else {
|
||||
Node<E> prev = node(index - 1);
|
||||
node = prev.next;
|
||||
prev.next = node.next;
|
||||
}
|
||||
size--;
|
||||
return node.element;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int indexOf(E element) {
|
||||
if (element == null) {
|
||||
Node<E> node = first;
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (node.element == null) return i;
|
||||
|
||||
node = node.next;
|
||||
}
|
||||
} else {
|
||||
Node<E> node = first;
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (element.equals(node.element)) return i;
|
||||
|
||||
node = node.next;
|
||||
}
|
||||
}
|
||||
return ELEMENT_NOT_FOUND;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取index位置对应的节点对象
|
||||
* @param index
|
||||
* @return
|
||||
*/
|
||||
private Node<E> node(int index) {
|
||||
rangeCheck(index);
|
||||
|
||||
Node<E> node = first;
|
||||
for (int i = 0; i < index; i++) {
|
||||
node = node.next;
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder string = new StringBuilder();
|
||||
string.append("size=").append(size).append(", [");
|
||||
Node<E> node = first;
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (i != 0) {
|
||||
string.append(", ");
|
||||
}
|
||||
|
||||
string.append(node);
|
||||
|
||||
node = node.next;
|
||||
}
|
||||
string.append("]");
|
||||
return string.toString();
|
||||
}
|
||||
}
|
@@ -0,0 +1,144 @@
|
||||
package com.mj.single;
|
||||
|
||||
import com.mj.AbstractList;
|
||||
|
||||
public class SingleLinkedList<E> extends AbstractList<E> {
|
||||
private Node<E> first;
|
||||
|
||||
private static class Node<E> {
|
||||
E element;
|
||||
Node<E> next;
|
||||
public Node(E element, Node<E> next) {
|
||||
this.element = element;
|
||||
this.next = next;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
size = 0;
|
||||
first = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E get(int index) {
|
||||
/*
|
||||
* 最好:O(1)
|
||||
* 最坏:O(n)
|
||||
* 平均:O(n)
|
||||
*/
|
||||
return node(index).element;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E set(int index, E element) {
|
||||
/*
|
||||
* 最好:O(1)
|
||||
* 最坏:O(n)
|
||||
* 平均:O(n)
|
||||
*/
|
||||
Node<E> node = node(index);
|
||||
E old = node.element;
|
||||
node.element = element;
|
||||
return old;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(int index, E element) {
|
||||
/*
|
||||
* 最好:O(1)
|
||||
* 最坏:O(n)
|
||||
* 平均:O(n)
|
||||
*/
|
||||
rangeCheckForAdd(index);
|
||||
|
||||
if (index == 0) {
|
||||
first = new Node<>(element, first);
|
||||
} else {
|
||||
Node<E> prev = node(index - 1);
|
||||
prev.next = new Node<>(element, prev.next);
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E remove(int index) {
|
||||
/*
|
||||
* 最好:O(1)
|
||||
* 最坏:O(n)
|
||||
* 平均:O(n)
|
||||
*/
|
||||
rangeCheck(index);
|
||||
|
||||
Node<E> node = first;
|
||||
if (index == 0) {
|
||||
first = first.next;
|
||||
} else {
|
||||
Node<E> prev = node(index - 1);
|
||||
node = prev.next;
|
||||
prev.next = node.next;
|
||||
}
|
||||
size--;
|
||||
return node.element;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int indexOf(E element) {
|
||||
if (element == null) {
|
||||
Node<E> node = first;
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (node.element == null) return i;
|
||||
|
||||
node = node.next;
|
||||
}
|
||||
} else {
|
||||
Node<E> node = first;
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (element.equals(node.element)) return i;
|
||||
|
||||
node = node.next;
|
||||
}
|
||||
}
|
||||
return ELEMENT_NOT_FOUND;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取index位置对应的节点对象
|
||||
* @param index
|
||||
* @return
|
||||
*/
|
||||
private Node<E> node(int index) {
|
||||
rangeCheck(index);
|
||||
|
||||
Node<E> node = first;
|
||||
for (int i = 0; i < index; i++) {
|
||||
node = node.next;
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder string = new StringBuilder();
|
||||
string.append("size=").append(size).append(", [");
|
||||
Node<E> node = first;
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (i != 0) {
|
||||
string.append(", ");
|
||||
}
|
||||
|
||||
string.append(node.element);
|
||||
|
||||
node = node.next;
|
||||
}
|
||||
string.append("]");
|
||||
|
||||
// Node<E> node1 = first;
|
||||
// while (node1 != null) {
|
||||
//
|
||||
//
|
||||
// node1 = node1.next;
|
||||
// }
|
||||
return string.toString();
|
||||
}
|
||||
}
|
@@ -0,0 +1,127 @@
|
||||
package com.mj.single;
|
||||
|
||||
import com.mj.AbstractList;
|
||||
|
||||
/**
|
||||
* 增加一个虚拟头结点
|
||||
* @author MJ Lee
|
||||
*
|
||||
* @param <E>
|
||||
*/
|
||||
public class SingleLinkedList2<E> extends AbstractList<E> {
|
||||
private Node<E> first;
|
||||
|
||||
public SingleLinkedList2() {
|
||||
first = new Node<>(null, null);
|
||||
}
|
||||
|
||||
private static class Node<E> {
|
||||
E element;
|
||||
Node<E> next;
|
||||
public Node(E element, Node<E> next) {
|
||||
this.element = element;
|
||||
this.next = next;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
size = 0;
|
||||
first = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E get(int index) {
|
||||
return node(index).element;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E set(int index, E element) {
|
||||
Node<E> node = node(index);
|
||||
E old = node.element;
|
||||
node.element = element;
|
||||
return old;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(int index, E element) {
|
||||
rangeCheckForAdd(index);
|
||||
|
||||
Node<E> prev = index == 0 ? first : node(index - 1);
|
||||
prev.next = new Node<>(element, prev.next);
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E remove(int index) {
|
||||
rangeCheck(index);
|
||||
|
||||
Node<E> prev = index == 0 ? first : node(index - 1);
|
||||
Node<E> node = prev.next;
|
||||
prev.next = node.next;
|
||||
|
||||
size--;
|
||||
return node.element;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int indexOf(E element) {
|
||||
if (element == null) {
|
||||
Node<E> node = first;
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (node.element == null) return i;
|
||||
|
||||
node = node.next;
|
||||
}
|
||||
} else {
|
||||
Node<E> node = first;
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (element.equals(node.element)) return i;
|
||||
|
||||
node = node.next;
|
||||
}
|
||||
}
|
||||
return ELEMENT_NOT_FOUND;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取index位置对应的节点对象
|
||||
* @param index
|
||||
* @return
|
||||
*/
|
||||
private Node<E> node(int index) {
|
||||
rangeCheck(index);
|
||||
|
||||
Node<E> node = first.next;
|
||||
for (int i = 0; i < index; i++) {
|
||||
node = node.next;
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder string = new StringBuilder();
|
||||
string.append("size=").append(size).append(", [");
|
||||
Node<E> node = first.next;
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (i != 0) {
|
||||
string.append(", ");
|
||||
}
|
||||
|
||||
string.append(node.element);
|
||||
|
||||
node = node.next;
|
||||
}
|
||||
string.append("]");
|
||||
|
||||
// Node<E> node1 = first;
|
||||
// while (node1 != null) {
|
||||
//
|
||||
//
|
||||
// node1 = node1.next;
|
||||
// }
|
||||
return string.toString();
|
||||
}
|
||||
}
|
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
17
数据结构(双语)/数据结构 day16【瑞客论坛 www.ruike1.com】/代码/04-栈/.project
Normal file
17
数据结构(双语)/数据结构 day16【瑞客论坛 www.ruike1.com】/代码/04-栈/.project
Normal file
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>04-栈</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
@@ -0,0 +1,11 @@
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
|
||||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||
org.eclipse.jdt.core.compiler.compliance=1.8
|
||||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.source=1.8
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,17 @@
|
||||
package com.mj;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Stack<Integer> stack = new Stack<>();
|
||||
stack.push(11);
|
||||
stack.push(22);
|
||||
stack.push(33);
|
||||
stack.push(44);
|
||||
|
||||
while (!stack.isEmpty()) {
|
||||
System.out.println(stack.pop());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,34 @@
|
||||
package com.mj;
|
||||
|
||||
import com.mj.list.ArrayList;
|
||||
import com.mj.list.List;
|
||||
|
||||
public class Stack<E> {
|
||||
private List<E> list = new ArrayList<>();
|
||||
|
||||
public void clear() {
|
||||
list.clear();
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return list.size();
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return list.isEmpty();
|
||||
}
|
||||
|
||||
public void push(E element) {
|
||||
list.add(element);
|
||||
}
|
||||
|
||||
|
||||
public E pop() {
|
||||
return list.remove(list.size() - 1);
|
||||
}
|
||||
|
||||
|
||||
public E top() {
|
||||
return list.get(list.size() - 1);
|
||||
}
|
||||
}
|
@@ -0,0 +1,56 @@
|
||||
package com.mj.list;
|
||||
|
||||
public abstract class AbstractList<E> implements List<E> {
|
||||
/**
|
||||
* 元素的数量
|
||||
*/
|
||||
protected int size;
|
||||
/**
|
||||
* 元素的数量
|
||||
* @return
|
||||
*/
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否为空
|
||||
* @return
|
||||
*/
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否包含某个元素
|
||||
* @param element
|
||||
* @return
|
||||
*/
|
||||
public boolean contains(E element) {
|
||||
return indexOf(element) != ELEMENT_NOT_FOUND;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加元素到尾部
|
||||
* @param element
|
||||
*/
|
||||
public void add(E element) {
|
||||
add(size, element);
|
||||
}
|
||||
|
||||
protected void outOfBounds(int index) {
|
||||
throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size);
|
||||
}
|
||||
|
||||
protected void rangeCheck(int index) {
|
||||
if (index < 0 || index >= size) {
|
||||
outOfBounds(index);
|
||||
}
|
||||
}
|
||||
|
||||
protected void rangeCheckForAdd(int index) {
|
||||
if (index < 0 || index > size) {
|
||||
outOfBounds(index);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,154 @@
|
||||
package com.mj.list;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public class ArrayList<E> extends AbstractList<E> {
|
||||
/**
|
||||
* 所有的元素
|
||||
*/
|
||||
private E[] elements;
|
||||
private static final int DEFAULT_CAPACITY = 10;
|
||||
|
||||
public ArrayList(int capaticy) {
|
||||
capaticy = (capaticy < DEFAULT_CAPACITY) ? DEFAULT_CAPACITY : capaticy;
|
||||
elements = (E[]) new Object[capaticy];
|
||||
}
|
||||
|
||||
public ArrayList() {
|
||||
this(DEFAULT_CAPACITY);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除所有元素
|
||||
*/
|
||||
public void clear() {
|
||||
for (int i = 0; i < size; i++) {
|
||||
elements[i] = null;
|
||||
}
|
||||
size = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取index位置的元素
|
||||
* @param index
|
||||
* @return
|
||||
*/
|
||||
public E get(int index) { // O(1)
|
||||
rangeCheck(index);
|
||||
|
||||
return elements[index];
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置index位置的元素
|
||||
* @param index
|
||||
* @param element
|
||||
* @return 原来的元素ֵ
|
||||
*/
|
||||
public E set(int index, E element) { // O(1)
|
||||
rangeCheck(index);
|
||||
|
||||
E old = elements[index];
|
||||
elements[index] = element;
|
||||
return old;
|
||||
}
|
||||
|
||||
/**
|
||||
* 在index位置插入一个元素
|
||||
* @param index
|
||||
* @param element
|
||||
*/
|
||||
public void add(int index, E element) {
|
||||
/*
|
||||
* 最好:O(1)
|
||||
* 最坏:O(n)
|
||||
* 平均:O(n)
|
||||
*/
|
||||
rangeCheckForAdd(index);
|
||||
|
||||
ensureCapacity(size + 1);
|
||||
|
||||
for (int i = size; i > index; i--) {
|
||||
elements[i] = elements[i - 1];
|
||||
}
|
||||
elements[index] = element;
|
||||
size++;
|
||||
} // size是数据规模
|
||||
|
||||
/**
|
||||
* 删除index位置的元素
|
||||
* @param index
|
||||
* @return
|
||||
*/
|
||||
public E remove(int index) {
|
||||
/*
|
||||
* 最好:O(1)
|
||||
* 最坏:O(n)
|
||||
* 平均:O(n)
|
||||
*/
|
||||
rangeCheck(index);
|
||||
|
||||
E old = elements[index];
|
||||
for (int i = index + 1; i < size; i++) {
|
||||
elements[i - 1] = elements[i];
|
||||
}
|
||||
elements[--size] = null;
|
||||
return old;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看元素的索引
|
||||
* @param element
|
||||
* @return
|
||||
*/
|
||||
public int indexOf(E element) {
|
||||
if (element == null) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (elements[i] == null) return i;
|
||||
}
|
||||
} else {
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (element.equals(elements[i])) return i;
|
||||
}
|
||||
}
|
||||
return ELEMENT_NOT_FOUND;
|
||||
}
|
||||
|
||||
/**
|
||||
* 保证要有capacity的容量
|
||||
* @param capacity
|
||||
*/
|
||||
private void ensureCapacity(int capacity) {
|
||||
int oldCapacity = elements.length;
|
||||
if (oldCapacity >= capacity) return;
|
||||
|
||||
// 新容量为旧容量的1.5倍
|
||||
int newCapacity = oldCapacity + (oldCapacity >> 1);
|
||||
E[] newElements = (E[]) new Object[newCapacity];
|
||||
for (int i = 0; i < size; i++) {
|
||||
newElements[i] = elements[i];
|
||||
}
|
||||
elements = newElements;
|
||||
|
||||
System.out.println(oldCapacity + "扩容为" + newCapacity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
// size=3, [99, 88, 77]
|
||||
StringBuilder string = new StringBuilder();
|
||||
string.append("size=").append(size).append(", [");
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (i != 0) {
|
||||
string.append(", ");
|
||||
}
|
||||
|
||||
string.append(elements[i]);
|
||||
|
||||
// if (i != size - 1) {
|
||||
// string.append(", ");
|
||||
// }
|
||||
}
|
||||
string.append("]");
|
||||
return string.toString();
|
||||
}
|
||||
}
|
@@ -0,0 +1,175 @@
|
||||
package com.mj.list;
|
||||
|
||||
import com.mj.list.AbstractList;
|
||||
|
||||
public class LinkedList<E> extends AbstractList<E> {
|
||||
private Node<E> first;
|
||||
private Node<E> last;
|
||||
|
||||
private static class Node<E> {
|
||||
E element;
|
||||
Node<E> prev;
|
||||
Node<E> next;
|
||||
public Node(Node<E> prev, E element, Node<E> next) {
|
||||
this.prev = prev;
|
||||
this.element = element;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
if (prev != null) {
|
||||
sb.append(prev.element);
|
||||
} else {
|
||||
sb.append("null");
|
||||
}
|
||||
|
||||
sb.append("_").append(element).append("_");
|
||||
|
||||
if (next != null) {
|
||||
sb.append(next.element);
|
||||
} else {
|
||||
sb.append("null");
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
size = 0;
|
||||
first = null;
|
||||
last = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E get(int index) {
|
||||
return node(index).element;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E set(int index, E element) {
|
||||
Node<E> node = node(index);
|
||||
E old = node.element;
|
||||
node.element = element;
|
||||
return old;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(int index, E element) {
|
||||
rangeCheckForAdd(index);
|
||||
|
||||
// size == 0
|
||||
// index == 0
|
||||
if (index == size) { // 往最后面添加元素
|
||||
Node<E> oldLast = last;
|
||||
last = new Node<>(oldLast, element, null);
|
||||
if (oldLast == null) { // 这是链表添加的第一个元素
|
||||
first = last;
|
||||
} else {
|
||||
oldLast.next = last;
|
||||
}
|
||||
} else {
|
||||
Node<E> next = node(index);
|
||||
Node<E> prev = next.prev;
|
||||
Node<E> node = new Node<>(prev, element, next);
|
||||
next.prev = node;
|
||||
|
||||
if (prev == null) { // index == 0
|
||||
first = node;
|
||||
} else {
|
||||
prev.next = node;
|
||||
}
|
||||
}
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E remove(int index) {
|
||||
rangeCheck(index);
|
||||
|
||||
Node<E> node = node(index);
|
||||
Node<E> prev = node.prev;
|
||||
Node<E> next = node.next;
|
||||
|
||||
if (prev == null) { // index == 0
|
||||
first = next;
|
||||
} else {
|
||||
prev.next = next;
|
||||
}
|
||||
|
||||
if (next == null) { // index == size - 1
|
||||
last = prev;
|
||||
} else {
|
||||
next.prev = prev;
|
||||
}
|
||||
|
||||
size--;
|
||||
return node.element;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int indexOf(E element) {
|
||||
if (element == null) {
|
||||
Node<E> node = first;
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (node.element == null) return i;
|
||||
|
||||
node = node.next;
|
||||
}
|
||||
} else {
|
||||
Node<E> node = first;
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (element.equals(node.element)) return i;
|
||||
|
||||
node = node.next;
|
||||
}
|
||||
}
|
||||
return ELEMENT_NOT_FOUND;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取index位置对应的节点对象
|
||||
* @param index
|
||||
* @return
|
||||
*/
|
||||
private Node<E> node(int index) {
|
||||
rangeCheck(index);
|
||||
|
||||
if (index < (size >> 1)) {
|
||||
Node<E> node = first;
|
||||
for (int i = 0; i < index; i++) {
|
||||
node = node.next;
|
||||
}
|
||||
return node;
|
||||
} else {
|
||||
Node<E> node = last;
|
||||
for (int i = size - 1; i > index; i--) {
|
||||
node = node.prev;
|
||||
}
|
||||
return node;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder string = new StringBuilder();
|
||||
string.append("size=").append(size).append(", [");
|
||||
Node<E> node = first;
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (i != 0) {
|
||||
string.append(", ");
|
||||
}
|
||||
|
||||
string.append(node);
|
||||
|
||||
node = node.next;
|
||||
}
|
||||
string.append("]");
|
||||
return string.toString();
|
||||
}
|
||||
}
|
@@ -0,0 +1,70 @@
|
||||
package com.mj.list;
|
||||
|
||||
public interface List<E> {
|
||||
static final int ELEMENT_NOT_FOUND = -1;
|
||||
/**
|
||||
* 清除所有元素
|
||||
*/
|
||||
void clear();
|
||||
|
||||
/**
|
||||
* 元素的数量
|
||||
* @return
|
||||
*/
|
||||
int size();
|
||||
|
||||
/**
|
||||
* 是否为空
|
||||
* @return
|
||||
*/
|
||||
boolean isEmpty();
|
||||
|
||||
/**
|
||||
* 是否包含某个元素
|
||||
* @param element
|
||||
* @return
|
||||
*/
|
||||
boolean contains(E element);
|
||||
|
||||
/**
|
||||
* 添加元素到尾部
|
||||
* @param element
|
||||
*/
|
||||
void add(E element);
|
||||
|
||||
/**
|
||||
* 获取index位置的元素
|
||||
* @param index
|
||||
* @return
|
||||
*/
|
||||
E get(int index);
|
||||
|
||||
/**
|
||||
* 设置index位置的元素
|
||||
* @param index
|
||||
* @param element
|
||||
* @return 原来的元素ֵ
|
||||
*/
|
||||
E set(int index, E element);
|
||||
|
||||
/**
|
||||
* 在index位置插入一个元素
|
||||
* @param index
|
||||
* @param element
|
||||
*/
|
||||
void add(int index, E element);
|
||||
|
||||
/**
|
||||
* 删除index位置的元素
|
||||
* @param index
|
||||
* @return
|
||||
*/
|
||||
E remove(int index);
|
||||
|
||||
/**
|
||||
* 查看元素的索引
|
||||
* @param element
|
||||
* @return
|
||||
*/
|
||||
int indexOf(E element);
|
||||
}
|
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
17
数据结构(双语)/数据结构 day16【瑞客论坛 www.ruike1.com】/代码/05-队列/.project
Normal file
17
数据结构(双语)/数据结构 day16【瑞客论坛 www.ruike1.com】/代码/05-队列/.project
Normal file
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>05-队列</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
@@ -0,0 +1,11 @@
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
|
||||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||
org.eclipse.jdt.core.compiler.compliance=1.8
|
||||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.source=1.8
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,44 @@
|
||||
package com.mj;
|
||||
|
||||
import com.mj.list.LinkedList;
|
||||
import com.mj.list.List;
|
||||
|
||||
public class Deque<E> {
|
||||
private List<E> list = new LinkedList<>();
|
||||
|
||||
public int size() {
|
||||
return list.size();
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return list.isEmpty();
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
list.clear();
|
||||
}
|
||||
|
||||
public void enQueueRear(E element) {
|
||||
list.add(element);
|
||||
}
|
||||
|
||||
public E deQueueFront() {
|
||||
return list.remove(0);
|
||||
}
|
||||
|
||||
public void enQueueFront(E element) {
|
||||
list.add(0, element);
|
||||
}
|
||||
|
||||
public E deQueueRear() {
|
||||
return list.remove(list.size() - 1);
|
||||
}
|
||||
|
||||
public E front() {
|
||||
return list.get(0);
|
||||
}
|
||||
|
||||
public E rear() {
|
||||
return list.get(list.size() - 1);
|
||||
}
|
||||
}
|
@@ -0,0 +1,96 @@
|
||||
package com.mj;
|
||||
|
||||
import com.mj.circle.CircleDeque;
|
||||
import com.mj.circle.CircleQueue;
|
||||
|
||||
public class Main {
|
||||
|
||||
static void test1() {
|
||||
Queue<Integer> queue = new Queue<>();
|
||||
queue.enQueue(11);
|
||||
queue.enQueue(22);
|
||||
queue.enQueue(33);
|
||||
queue.enQueue(44);
|
||||
|
||||
while (!queue.isEmpty()) {
|
||||
System.out.println(queue.deQueue());
|
||||
}
|
||||
|
||||
// Deque<Integer> queue = new Deque<>();
|
||||
// queue.enQueueFront(11);
|
||||
// queue.enQueueFront(22);
|
||||
// queue.enQueueRear(33);
|
||||
// queue.enQueueRear(44);
|
||||
//
|
||||
// /* 尾 44 33 11 22 头 */
|
||||
//
|
||||
// while (!queue.isEmpty()) {
|
||||
// System.out.println(queue.deQueueRear());
|
||||
// }
|
||||
}
|
||||
|
||||
static void test2() {
|
||||
CircleQueue<Integer> queue = new CircleQueue<Integer>();
|
||||
// 0 1 2 3 4 5 6 7 8 9
|
||||
for (int i = 0; i < 10; i++) {
|
||||
queue.enQueue(i);
|
||||
}
|
||||
// null null null null null 5 6 7 8 9
|
||||
for (int i = 0; i < 5; i++) {
|
||||
queue.deQueue();
|
||||
}
|
||||
// 15 16 17 18 19 5 6 7 8 9
|
||||
for (int i = 15; i < 20; i++) {
|
||||
queue.enQueue(i);
|
||||
}
|
||||
while (!queue.isEmpty()) {
|
||||
System.out.println(queue.deQueue());
|
||||
}
|
||||
System.out.println(queue);
|
||||
}
|
||||
|
||||
static void test3() {
|
||||
CircleDeque<Integer> queue = new CircleDeque<>();
|
||||
// 头5 4 3 2 1 100 101 102 103 104 105 106 8 7 6 尾
|
||||
|
||||
// 头 8 7 6 5 4 3 2 1 100 101 102 103 104 105 106 107 108 109 null null 10 9 尾
|
||||
for (int i = 0; i < 10; i++) {
|
||||
queue.enQueueFront(i + 1);
|
||||
queue.enQueueRear(i + 100);
|
||||
}
|
||||
|
||||
// 头 null 7 6 5 4 3 2 1 100 101 102 103 104 105 106 null null null null null null null 尾
|
||||
for (int i = 0; i < 3; i++) {
|
||||
queue.deQueueFront();
|
||||
queue.deQueueRear();
|
||||
}
|
||||
|
||||
// 头 11 7 6 5 4 3 2 1 100 101 102 103 104 105 106 null null null null null null 12 尾
|
||||
queue.enQueueFront(11);
|
||||
queue.enQueueFront(12);
|
||||
System.out.println(queue);
|
||||
while (!queue.isEmpty()) {
|
||||
System.out.println(queue.deQueueFront());
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
test2();
|
||||
test3();
|
||||
|
||||
// int n = 13;
|
||||
// int m = 7;
|
||||
//
|
||||
//// if (n >= m) {
|
||||
//// System.out.println(n - m);
|
||||
//// } else {
|
||||
//// System.out.println(n);
|
||||
//// }
|
||||
//
|
||||
// // m > 0, n >= 0, n < 2m
|
||||
// System.out.println(n - (n >= m ? m : 0));
|
||||
//
|
||||
// System.out.println(n % m);
|
||||
}
|
||||
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user