fork 自 sduonline/sc-resources
增加淘宝群内容,修改部分文件组织
此提交包含在:
@@ -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);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,32 @@
|
||||
package com.mj;
|
||||
|
||||
import com.mj.list.LinkedList;
|
||||
import com.mj.list.List;
|
||||
|
||||
public class Queue<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 enQueue(E element) {
|
||||
list.add(element);
|
||||
}
|
||||
|
||||
public E deQueue() {
|
||||
return list.remove(0);
|
||||
}
|
||||
|
||||
public E front() {
|
||||
return list.get(0);
|
||||
}
|
||||
}
|
@@ -0,0 +1,130 @@
|
||||
package com.mj.circle;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public class CircleDeque<E> {
|
||||
private int front;
|
||||
private int size;
|
||||
private E[] elements;
|
||||
private static final int DEFAULT_CAPACITY = 10;
|
||||
|
||||
public CircleDeque() {
|
||||
elements = (E[]) new Object[DEFAULT_CAPACITY];
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
for (int i = 0; i < size; i++) {
|
||||
elements[index(i)] = null;
|
||||
}
|
||||
front = 0;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从尾部入队
|
||||
* @param element
|
||||
*/
|
||||
public void enQueueRear(E element) {
|
||||
ensureCapacity(size + 1);
|
||||
|
||||
elements[index(size)] = element;
|
||||
size++;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从头部出队
|
||||
* @param element
|
||||
*/
|
||||
public E deQueueFront() {
|
||||
E frontElement = elements[front];
|
||||
elements[front] = null;
|
||||
front = index(1);
|
||||
size--;
|
||||
return frontElement;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从头部入队
|
||||
* @param element
|
||||
*/
|
||||
public void enQueueFront(E element) {
|
||||
ensureCapacity(size + 1);
|
||||
|
||||
front = index(-1);
|
||||
elements[front] = element;
|
||||
size++;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从尾部出队
|
||||
* @param element
|
||||
*/
|
||||
public E deQueueRear() {
|
||||
int rearIndex = index(size - 1);
|
||||
E rear = elements[rearIndex];
|
||||
elements[rearIndex] = null;
|
||||
size--;
|
||||
return rear;
|
||||
}
|
||||
|
||||
public E front() {
|
||||
return elements[front];
|
||||
}
|
||||
|
||||
public E rear() {
|
||||
return elements[index(size - 1)];
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder string = new StringBuilder();
|
||||
string.append("capcacity=").append(elements.length)
|
||||
.append(" size=").append(size)
|
||||
.append(" front=").append(front)
|
||||
.append(", [");
|
||||
for (int i = 0; i < elements.length; i++) {
|
||||
if (i != 0) {
|
||||
string.append(", ");
|
||||
}
|
||||
|
||||
string.append(elements[i]);
|
||||
}
|
||||
string.append("]");
|
||||
return string.toString();
|
||||
}
|
||||
|
||||
private int index(int index) {
|
||||
index += front;
|
||||
if (index < 0) {
|
||||
return index + elements.length;
|
||||
}
|
||||
return index - (index >= elements.length ? elements.length : 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 保证要有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[index(i)];
|
||||
}
|
||||
elements = newElements;
|
||||
|
||||
// 重置front
|
||||
front = 0;
|
||||
}
|
||||
}
|
@@ -0,0 +1,91 @@
|
||||
package com.mj.circle;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public class CircleQueue<E> {
|
||||
private int front;
|
||||
private int size;
|
||||
private E[] elements;
|
||||
private static final int DEFAULT_CAPACITY = 10;
|
||||
|
||||
public CircleQueue() {
|
||||
elements = (E[]) new Object[DEFAULT_CAPACITY];
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
for (int i = 0; i < size; i++) {
|
||||
elements[index(i)] = null;
|
||||
}
|
||||
front = 0;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public void enQueue(E element) {
|
||||
ensureCapacity(size + 1);
|
||||
|
||||
elements[index(size)] = element;
|
||||
size++;
|
||||
}
|
||||
|
||||
public E deQueue() {
|
||||
E frontElement = elements[front];
|
||||
elements[front] = null;
|
||||
front = index(1);
|
||||
size--;
|
||||
return frontElement;
|
||||
}
|
||||
|
||||
public E front() {
|
||||
return elements[front];
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder string = new StringBuilder();
|
||||
string.append("capcacity=").append(elements.length)
|
||||
.append(" size=").append(size)
|
||||
.append(" front=").append(front)
|
||||
.append(", [");
|
||||
for (int i = 0; i < elements.length; i++) {
|
||||
if (i != 0) {
|
||||
string.append(", ");
|
||||
}
|
||||
|
||||
string.append(elements[i]);
|
||||
}
|
||||
string.append("]");
|
||||
return string.toString();
|
||||
}
|
||||
|
||||
private int index(int index) {
|
||||
index += front;
|
||||
return index - (index >= elements.length ? elements.length : 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 保证要有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[index(i)];
|
||||
}
|
||||
elements = newElements;
|
||||
|
||||
// 重置front
|
||||
front = 0;
|
||||
}
|
||||
}
|
@@ -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,173 @@
|
||||
package com.mj.list;
|
||||
|
||||
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);
|
||||
}
|
新增問題並參考
封鎖使用者