From 0c1768238ef3c9a20788ae58ef4f16e7d62e76c1 Mon Sep 17 00:00:00 2001 From: Selebrator Date: Tue, 30 Apr 2019 08:20:19 +0200 Subject: [PATCH] Moved provided LinkedStringList to correct package --- src/main/java/_3/_2/VersatileLinkedList.java | 2 + .../_3/_2/LinkedStringList.java | 190 +++++++++--------- 2 files changed, 97 insertions(+), 95 deletions(-) rename src/main/java/{ => provided}/_3/_2/LinkedStringList.java (94%) diff --git a/src/main/java/_3/_2/VersatileLinkedList.java b/src/main/java/_3/_2/VersatileLinkedList.java index a174ca3..74e13cb 100644 --- a/src/main/java/_3/_2/VersatileLinkedList.java +++ b/src/main/java/_3/_2/VersatileLinkedList.java @@ -1,5 +1,7 @@ package _3._2; +import provided._3._2.LinkedStringList; + public class VersatileLinkedList extends LinkedStringList { public void add(int value) { diff --git a/src/main/java/_3/_2/LinkedStringList.java b/src/main/java/provided/_3/_2/LinkedStringList.java similarity index 94% rename from src/main/java/_3/_2/LinkedStringList.java rename to src/main/java/provided/_3/_2/LinkedStringList.java index db3f0b8..04fe491 100644 --- a/src/main/java/_3/_2/LinkedStringList.java +++ b/src/main/java/provided/_3/_2/LinkedStringList.java @@ -1,95 +1,95 @@ -package _3._2; - -public class LinkedStringList { - - private LinkedStringListElement start; - - public int size() { - int result = 0; - LinkedStringListElement tmp = start; - while (tmp != null) { - tmp = tmp.getNext(); - result++; - } - return result; - } - - public String get(int index) { - if (start == null) { // list is empty - return null; - } - LinkedStringListElement current = start; - int pos = 0; // counter for finding the right position - while (pos < index) { - if (current.getNext() == null) { - // list does not have enough elements - return null; - } - current = current.getNext(); - pos++; - } - return current.getValue(); - } - - public void add(String value) { - LinkedStringListElement elem = new LinkedStringListElement(); - elem.setValue(value); - if (start == null) { // list is empty - start = elem; - } else { - LinkedStringListElement tmp = start; - while (tmp.getNext() != null) { // find last element - tmp = tmp.getNext(); - } - tmp.setNext(elem); - } - } - - public String remove(int index) { - if (start == null) { // list is empty - return null; - } - if (index == 0) { // remove from the beginning of non-empty list - String result = start.getValue(); - start = start.getNext(); - return result; - } - // remove from anywhere in a non-empty list - LinkedStringListElement current = start; - int pos = 0; // counter for finding the right position - while (pos < index - 1) { - if (current.getNext() == null) { - // list does not have enough elements - return null; - } - current = current.getNext(); - pos++; - } - if (current.getNext() == null) { // not enough elements - return null; - } - String result = current.getNext().getValue(); - current.setNext(current.getNext().getNext()); - return result; - } - -} -class LinkedStringListElement { - - private LinkedStringListElement next; - private String value; - - public LinkedStringListElement getNext() { - return next; - } - public void setNext(LinkedStringListElement next) { - this.next = next; - } - public String getValue() { - return value; - } - public void setValue(String value) { - this.value = value; - } - -} \ No newline at end of file +package provided._3._2; + +public class LinkedStringList { + + private LinkedStringListElement start; + + public int size() { + int result = 0; + LinkedStringListElement tmp = start; + while (tmp != null) { + tmp = tmp.getNext(); + result++; + } + return result; + } + + public String get(int index) { + if (start == null) { // list is empty + return null; + } + LinkedStringListElement current = start; + int pos = 0; // counter for finding the right position + while (pos < index) { + if (current.getNext() == null) { + // list does not have enough elements + return null; + } + current = current.getNext(); + pos++; + } + return current.getValue(); + } + + public void add(String value) { + LinkedStringListElement elem = new LinkedStringListElement(); + elem.setValue(value); + if (start == null) { // list is empty + start = elem; + } else { + LinkedStringListElement tmp = start; + while (tmp.getNext() != null) { // find last element + tmp = tmp.getNext(); + } + tmp.setNext(elem); + } + } + + public String remove(int index) { + if (start == null) { // list is empty + return null; + } + if (index == 0) { // remove from the beginning of non-empty list + String result = start.getValue(); + start = start.getNext(); + return result; + } + // remove from anywhere in a non-empty list + LinkedStringListElement current = start; + int pos = 0; // counter for finding the right position + while (pos < index - 1) { + if (current.getNext() == null) { + // list does not have enough elements + return null; + } + current = current.getNext(); + pos++; + } + if (current.getNext() == null) { // not enough elements + return null; + } + String result = current.getNext().getValue(); + current.setNext(current.getNext().getNext()); + return result; + } + +} +class LinkedStringListElement { + + private LinkedStringListElement next; + private String value; + + public LinkedStringListElement getNext() { + return next; + } + public void setNext(LinkedStringListElement next) { + this.next = next; + } + public String getValue() { + return value; + } + public void setValue(String value) { + this.value = value; + } + +}