Moved provided LinkedStringList to correct package

generic-observer
Selebrator 7 years ago
parent c204456691
commit 0c1768238e

@ -1,5 +1,7 @@
package _3._2; package _3._2;
import provided._3._2.LinkedStringList;
public class VersatileLinkedList extends LinkedStringList { public class VersatileLinkedList extends LinkedStringList {
public void add(int value) { public void add(int value) {

@ -1,95 +1,95 @@
package _3._2; package provided._3._2;
public class LinkedStringList { public class LinkedStringList {
private LinkedStringListElement start; private LinkedStringListElement start;
public int size() { public int size() {
int result = 0; int result = 0;
LinkedStringListElement tmp = start; LinkedStringListElement tmp = start;
while (tmp != null) { while (tmp != null) {
tmp = tmp.getNext(); tmp = tmp.getNext();
result++; result++;
} }
return result; return result;
} }
public String get(int index) { public String get(int index) {
if (start == null) { // list is empty if (start == null) { // list is empty
return null; return null;
} }
LinkedStringListElement current = start; LinkedStringListElement current = start;
int pos = 0; // counter for finding the right position int pos = 0; // counter for finding the right position
while (pos < index) { while (pos < index) {
if (current.getNext() == null) { if (current.getNext() == null) {
// list does not have enough elements // list does not have enough elements
return null; return null;
} }
current = current.getNext(); current = current.getNext();
pos++; pos++;
} }
return current.getValue(); return current.getValue();
} }
public void add(String value) { public void add(String value) {
LinkedStringListElement elem = new LinkedStringListElement(); LinkedStringListElement elem = new LinkedStringListElement();
elem.setValue(value); elem.setValue(value);
if (start == null) { // list is empty if (start == null) { // list is empty
start = elem; start = elem;
} else { } else {
LinkedStringListElement tmp = start; LinkedStringListElement tmp = start;
while (tmp.getNext() != null) { // find last element while (tmp.getNext() != null) { // find last element
tmp = tmp.getNext(); tmp = tmp.getNext();
} }
tmp.setNext(elem); tmp.setNext(elem);
} }
} }
public String remove(int index) { public String remove(int index) {
if (start == null) { // list is empty if (start == null) { // list is empty
return null; return null;
} }
if (index == 0) { // remove from the beginning of non-empty list if (index == 0) { // remove from the beginning of non-empty list
String result = start.getValue(); String result = start.getValue();
start = start.getNext(); start = start.getNext();
return result; return result;
} }
// remove from anywhere in a non-empty list // remove from anywhere in a non-empty list
LinkedStringListElement current = start; LinkedStringListElement current = start;
int pos = 0; // counter for finding the right position int pos = 0; // counter for finding the right position
while (pos < index - 1) { while (pos < index - 1) {
if (current.getNext() == null) { if (current.getNext() == null) {
// list does not have enough elements // list does not have enough elements
return null; return null;
} }
current = current.getNext(); current = current.getNext();
pos++; pos++;
} }
if (current.getNext() == null) { // not enough elements if (current.getNext() == null) { // not enough elements
return null; return null;
} }
String result = current.getNext().getValue(); String result = current.getNext().getValue();
current.setNext(current.getNext().getNext()); current.setNext(current.getNext().getNext());
return result; return result;
} }
} }
class LinkedStringListElement { class LinkedStringListElement {
private LinkedStringListElement next; private LinkedStringListElement next;
private String value; private String value;
public LinkedStringListElement getNext() { public LinkedStringListElement getNext() {
return next; return next;
} }
public void setNext(LinkedStringListElement next) { public void setNext(LinkedStringListElement next) {
this.next = next; this.next = next;
} }
public String getValue() { public String getValue() {
return value; return value;
} }
public void setValue(String value) { public void setValue(String value) {
this.value = value; this.value = value;
} }
} }
Loading…
Cancel
Save