From ebe5d59daa681f6485ca06d73e80fb6a97522dc8 Mon Sep 17 00:00:00 2001 From: Shera Date: Mon, 29 May 2017 15:25:36 +0300 Subject: [PATCH 1/4] Added Java implementation --- Java/DoubleLinkedList.java | 36 ++++++++++++++++++++++++++++++++++++ Java/SingleLinkedList.java | 28 ++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 Java/DoubleLinkedList.java create mode 100644 Java/SingleLinkedList.java diff --git a/Java/DoubleLinkedList.java b/Java/DoubleLinkedList.java new file mode 100644 index 0000000..9ce9845 --- /dev/null +++ b/Java/DoubleLinkedList.java @@ -0,0 +1,36 @@ + +public class DoubleLinkedList { + + private Node last = null; + + private class Node { + T item; + Node next; + Node previous; + } + + public boolean isEmpty() { return last == null; } + + public void push(T item) { + Node oldLast = last; + last = new Node(); + last.item = item; + last.previous = oldLast; + if(oldLast!=null) + { + oldLast.next = last; + } + } + + public T pop() { + T item = null; + if(!isEmpty()) { + item = last.item; + last = last.previous; + if(last!=null) { + last.next = null; + } + } + return item; + } +} diff --git a/Java/SingleLinkedList.java b/Java/SingleLinkedList.java new file mode 100644 index 0000000..8e9326f --- /dev/null +++ b/Java/SingleLinkedList.java @@ -0,0 +1,28 @@ + +public class SingleLinkedList { + + private Node last = null; + + private class Node { + T item; + Node previous; + } + + public boolean isEmpty() { return last == null; } + + public void push(T item) { + Node oldLast = last; + last = new Node(); + last.item = item; + last.previous = oldLast; + } + + public T pop() { + T item = null; + if(!isEmpty()) { + item = last.item; + last = last.previous; + } + return item; + } +} \ No newline at end of file From b0151ce59edca96eb13982154ce5eef3c2fc5064 Mon Sep 17 00:00:00 2001 From: Shera Date: Mon, 29 May 2017 16:31:46 +0300 Subject: [PATCH 2/4] Code style formatted --- Java/DoubleLinkedList.java | 19 ++++++++++--------- Java/SingleLinkedList.java | 14 ++++++++------ 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/Java/DoubleLinkedList.java b/Java/DoubleLinkedList.java index 9ce9845..f20232b 100644 --- a/Java/DoubleLinkedList.java +++ b/Java/DoubleLinkedList.java @@ -4,30 +4,31 @@ public class DoubleLinkedList { private Node last = null; private class Node { - T item; - Node next; - Node previous; + private T item; + private Node next; + private Node previous; } - public boolean isEmpty() { return last == null; } + public boolean isEmpty() { + return last == null; + } - public void push(T item) { + public void push(final T item) { Node oldLast = last; last = new Node(); last.item = item; last.previous = oldLast; - if(oldLast!=null) - { + if (oldLast != null) { oldLast.next = last; } } public T pop() { T item = null; - if(!isEmpty()) { + if (!isEmpty()) { item = last.item; last = last.previous; - if(last!=null) { + if (last != null) { last.next = null; } } diff --git a/Java/SingleLinkedList.java b/Java/SingleLinkedList.java index 8e9326f..10cb941 100644 --- a/Java/SingleLinkedList.java +++ b/Java/SingleLinkedList.java @@ -4,13 +4,15 @@ public class SingleLinkedList { private Node last = null; private class Node { - T item; - Node previous; + private T item; + private Node previous; } - public boolean isEmpty() { return last == null; } + public boolean isEmpty() { + return last == null; + } - public void push(T item) { + public void push(final T item) { Node oldLast = last; last = new Node(); last.item = item; @@ -19,10 +21,10 @@ public void push(T item) { public T pop() { T item = null; - if(!isEmpty()) { + if (!isEmpty()) { item = last.item; last = last.previous; } return item; } -} \ No newline at end of file +} From 35ce15dbc9641e40305c86afe0946fd9f9e12d38 Mon Sep 17 00:00:00 2001 From: Shera Date: Wed, 21 Jun 2017 18:20:51 +0300 Subject: [PATCH 3/4] added LinkedList interface --- Java/DoubleLinkedList.java | 2 +- Java/LinkedList.java | 10 ++++++++++ Java/SingleLinkedList.java | 2 +- 3 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 Java/LinkedList.java diff --git a/Java/DoubleLinkedList.java b/Java/DoubleLinkedList.java index f20232b..8d2d879 100644 --- a/Java/DoubleLinkedList.java +++ b/Java/DoubleLinkedList.java @@ -1,5 +1,5 @@ -public class DoubleLinkedList { +public class DoubleLinkedList implements LinkedList { private Node last = null; diff --git a/Java/LinkedList.java b/Java/LinkedList.java new file mode 100644 index 0000000..d7f2803 --- /dev/null +++ b/Java/LinkedList.java @@ -0,0 +1,10 @@ +/** + * Created by Shera on 21.06.2017. + */ +public interface LinkedList { + + boolean isEmpty(); + void push(T item); + T pop(); + +} diff --git a/Java/SingleLinkedList.java b/Java/SingleLinkedList.java index 10cb941..faf1beb 100644 --- a/Java/SingleLinkedList.java +++ b/Java/SingleLinkedList.java @@ -1,5 +1,5 @@ -public class SingleLinkedList { +public class SingleLinkedList implements LinkedList { private Node last = null; From a27d85db39ea70726b127a763bcf5b9de4dc7012 Mon Sep 17 00:00:00 2001 From: Katerina Shaidurova Date: Wed, 21 Jun 2017 18:24:08 +0300 Subject: [PATCH 4/4] Update LinkedList.java --- Java/LinkedList.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Java/LinkedList.java b/Java/LinkedList.java index d7f2803..0571ed4 100644 --- a/Java/LinkedList.java +++ b/Java/LinkedList.java @@ -1,6 +1,4 @@ -/** - * Created by Shera on 21.06.2017. - */ + public interface LinkedList { boolean isEmpty();