Skip to content

Commit

Permalink
Merge pull request #7 from honokaBiu/master
Browse files Browse the repository at this point in the history
Sync
  • Loading branch information
conf1102 authored Mar 9, 2017
2 parents b8c763d + b36c1e0 commit 4116f15
Show file tree
Hide file tree
Showing 37 changed files with 1,082 additions and 45 deletions.
6 changes: 6 additions & 0 deletions group17/333333/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="bin"/>
</classpath>
1 change: 1 addition & 0 deletions group17/333333/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin/
17 changes: 17 additions & 0 deletions group17/333333/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>333</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>
6 changes: 6 additions & 0 deletions group17/365689789/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="bin"/>
</classpath>
1 change: 1 addition & 0 deletions group17/365689789/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin/
17 changes: 17 additions & 0 deletions group17/365689789/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>L365689789</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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
package org.korben.coding.basic.list;

import java.util.Objects;

/**
* Korben's LinkedList
*
* Created by Korben on 18/02/2017.
*/
public class KDoubleLinkedList<T> implements KList<T> {

private int size;

private Node<T> head;

private Node<T> last;

public KDoubleLinkedList() {
this.head = new Node<>(null);
}

@Override
public int size() {
return this.size;
}

@Override
public boolean isEmpty() {
return this.size == 0;
}

@Override
public boolean contains(Object o) {
Node node = this.head;
while (node.next != null) {
node = node.next;
if (Objects.equals(node.data, o)) {
return true;
}
}
return false;
}

@Override
public Object[] toArray() {
return new Object[0];
}

@Override
public boolean add(T o) {
if (this.last == null) {
this.last = new Node<>(o);
this.last.pre = this.head;
this.head.next = this.last;
} else {
Node<T> oldLast = this.last;
this.last = new Node<>(o);
this.last.pre = oldLast;
oldLast.next = this.last;
}
this.size++;
return true;
}

@Override
public boolean remove(T o) {
Node node = this.head;
while (node.next != null) {
node = node.next;
if (Objects.equals(node.data, o)) {
node.pre.next = node.next;
if (node.next != null) {
node.next.pre = node.pre;
}
this.size--;
return true;
}
}
return false;
}

@Override
public void clear() {
this.head.next = null;
this.last = null;

this.size = 0;
}

@Override
public T get(int index) {
return getNode(index).data;
}

@Override
public T set(int index, T element) {
Node<T> node = getNode(index);
node.data = element;
return element;
}

@Override
public void add(int index, T element) {
if (index <= 0 || index >= this.size) {
throw new IndexOutOfBoundsException();
}
Node<T> node = this.head;
for (int i = 0; i <= index; i++) {
node = node.next;
}
Node<T> pre = node.pre;
Node<T> newNode = new Node<>(element);
pre.next = newNode;
newNode.pre = pre;
newNode.next = node;
node.pre = newNode;

this.size++;
}

@Override
public T remove(int index) {
Node<T> node = getNode(index);
Node<T> pre = node.pre;
Node<T> next = node.next;
pre.next = next;
if (next != null) {
next.pre = pre;
}

this.size--;
return node.data;
}

@Override
public int indexOf(T o) {
Node node = this.head;
int index = 0;
while (node.next != null) {
node = node.next;
if (Objects.equals(node.data, o)) {
return index;
}
index++;
}
return -1;
}

@Override
public KIterator<T> iterator() {
throw new UnsupportedOperationException("方法未实现");
}

private Node<T> getNode(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException();
}

Node<T> node = this.head;
for (int i = 0; i <= index; i++) {
node = node.next;
}
return node;
}

private static class Node<T> {
T data;
Node<T> pre;
Node<T> next;

Node(T data) {
this.data = data;
}
}
}
Loading

0 comments on commit 4116f15

Please sign in to comment.