Repository navigation
Add linked-list exercise #85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -37,6 +37,7 @@ | |
| "octal", | ||
| "luhn", | ||
| "pig-latin", | ||
| "linked-list", | ||
| "pascals-triangle" | ||
| ], | ||
| "deprecated": [ | ||
|
|
||
| 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" | ||
| } |
| 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; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| public final class Element { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need these methods?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Though, you're right, we don't need the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| 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; | ||
| } | ||
| } | ||
| 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); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Elementshould be generic as well and instead of storing anObjectshould store an object of typeT. Then this cast becomes unnecessary.There was a problem hiding this comment.
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.