Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"octal",
"luhn",
"pig-latin",
"linked-list",
"pascals-triangle"
],
"deprecated": [
Expand Down
12 changes: 12 additions & 0 deletions linked-list/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apply plugin: "java"
apply plugin: "eclipse"
apply plugin: "idea"

repositories {
mavenCentral()
}

dependencies {
testCompile "junit:junit:4.10"
testCompile "org.assertj:assertj-core:3.2.0"
}
Empty file.
45 changes: 45 additions & 0 deletions linked-list/src/example/java/Deque.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
public final class Deque<T> {
private Element head;

public void push(T value) {
if (head == null) {
head = new Element(value, null, null);
head.setNext(head);
head.setPrev(head);
return;
}

Element oldTail = head.getPrev();
Element tail = new Element(value, oldTail, head);
oldTail.setNext(tail);
head.setPrev(tail);
}

public T pop() {
head = head.getPrev();
return shift();
}

public void unshift(T value) {
push(value);
head = head.getPrev();
}

public T shift() {
Object value = head.getValue();

Element newHead = head.getNext();
Element newTail = head.getPrev();

if (newHead == head) {
head = null;
}
else {
newHead.setPrev(newTail);
newTail.setNext(newHead);
head = newHead;
}

return (T) value;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Element should be generic as well and instead of storing an Object should store an object of type T. Then this cast becomes unnecessary.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're absolutely right. I had that in a previous implementation. Don't know why I got rid of it.

}
}
52 changes: 52 additions & 0 deletions linked-list/src/example/java/Element.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
public final class Element {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class should be package private. Or just be a private static inner class of Deque.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will make it package private.

private final Object value;
private Element prev;
private Element next;

public Element(Object value, Element prev, Element next) {
this.value = value;
this.prev = prev;
this.next = next;
}

public Element getPrev() {
return prev;
}

public void setPrev(Element prev) {
this.prev = prev;
}

public Element getNext() {
return next;
}

public void setNext(Element next) {
this.next = next;
}

public Object getValue() {
return value;
}

@Override
public boolean equals(Object o) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need these methods?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the java implementation (heavily inspired by the ruby implementation) creates a circular list, we need the next-setters during shift(). The value-getter is for shifting and popping.

Though, you're right, we don't need the equals() or hashCode() implementations. We compare pointers when checking for circular references not the values.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could also say that we unwrap the next/prev fields into public-scoped fields and use them directly since Element has tighter scoping. I'm don't have a strong opinion either way.

if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Element element = (Element) o;

if (value != null ? !value.equals(element.value) : element.value != null) return false;
if (prev != null ? !prev.equals(element.prev) : element.prev != null) return false;
return next != null ? next.equals(element.next) : element.next == null;

}

@Override
public int hashCode() {
int result = value != null ? value.hashCode() : 0;
result = 31 * result + (prev != null ? prev.hashCode() : 0);
result = 31 * result + (next != null ? next.hashCode() : 0);
return result;
}
}
Empty file added linked-list/src/main/java/.keep
Empty file.
Empty file added linked-list/src/test/java/.keep
Empty file.
61 changes: 61 additions & 0 deletions linked-list/src/test/java/DequeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import org.junit.Before;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class DequeTest {
private Deque<Integer> subject;

@Before
public void setUp() {
subject = new Deque<>();
}

@Test
public void testPushPop() {
subject.push(10);
subject.push(20);
assertThat(subject.pop()).isEqualTo(20);
assertThat(subject.pop()).isEqualTo(10);
}

@Test
public void testPushShift() {
subject.push(10);
subject.push(20);
assertThat(subject.shift()).isEqualTo(10);
assertThat(subject.shift()).isEqualTo(20);
}

@Test
public void testUnshiftShift() {
subject.unshift(10);
subject.unshift(20);
assertThat(subject.shift()).isEqualTo(20);
assertThat(subject.shift()).isEqualTo(10);
}

@Test
public void testUnshiftPop() {
subject.unshift(10);
subject.unshift(20);
assertThat(subject.pop()).isEqualTo(10);
assertThat(subject.pop()).isEqualTo(20);
}

@Test
public void testExample() {
subject.push(10);
subject.push(20);
assertThat(subject.pop()).isEqualTo(20);

subject.push(30);
assertThat(subject.shift()).isEqualTo(10);

subject.unshift(40);
subject.push(50);
assertThat(subject.shift()).isEqualTo(40);
assertThat(subject.pop()).isEqualTo(50);
assertThat(subject.shift()).isEqualTo(30);
}
}