Added 3.2
parent
07f9fe9b4d
commit
4354dc9099
@ -0,0 +1,95 @@
|
||||
package _3._1._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;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,111 @@
|
||||
package _3._1._2;
|
||||
|
||||
public class VersatileLinkedList extends LinkedStringList {
|
||||
|
||||
public void add(int value) {
|
||||
String stringValue = Integer.toString(value);
|
||||
add(stringValue);
|
||||
}
|
||||
|
||||
public void add(boolean value) {
|
||||
if(value) {
|
||||
add("yes");
|
||||
} else {
|
||||
add("no");
|
||||
}
|
||||
}
|
||||
|
||||
public void add(LinkedStringList list) {
|
||||
for(int i = 0; i < list.size(); i++) {
|
||||
add(list.get(i));
|
||||
}
|
||||
}
|
||||
|
||||
public void add(LinkedStringList list, int start, int end) {
|
||||
if(!(start > end || start < 0 || end > list.size())) {
|
||||
for(int i = start; i < end; i++) {
|
||||
add(list.get(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public VersatileLinkedList reverse() {
|
||||
VersatileLinkedList list = new VersatileLinkedList();
|
||||
|
||||
for(int i = size() - 1; i >= 0; i--) {
|
||||
list.add(get(i));
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
public boolean equals(VersatileLinkedList list) {
|
||||
if(this.size() != list.size()) {
|
||||
return false;
|
||||
} else
|
||||
for(int i = 0; i < list.size(); i++) {
|
||||
if(!list.get(i).equals(this.get(i))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
VersatileLinkedList list1 = new VersatileLinkedList();
|
||||
VersatileLinkedList list2 = new VersatileLinkedList();
|
||||
list1.add(1);
|
||||
list1.add(true);
|
||||
list1.add("2");
|
||||
System.out.println(list1.size()); // Erwartete Ausgabe: 3
|
||||
list2.add(1);
|
||||
list2.add(true);
|
||||
list2.add("2");
|
||||
if(list1.equals(list2)) {
|
||||
System.out.println("Listen sind gleich");
|
||||
} else {
|
||||
System.out.println("Listen sind ungleich");
|
||||
}
|
||||
// Erwartete Ausgabe: Listen sind gleich
|
||||
|
||||
list2 = list2.reverse();
|
||||
if(list1.equals(list2)) {
|
||||
System.out.println("Listen sind gleich");
|
||||
} else {
|
||||
System.out.println("Listen sind ungleich");
|
||||
}
|
||||
// Erwartete Ausgabe: Listen sind ungleich
|
||||
|
||||
for(int i = 0; i < list1.size(); i++) {
|
||||
System.out.println(list1.get(i));
|
||||
}
|
||||
// Erwartete Ausgabe: 1, yes, 2
|
||||
|
||||
list1.add(list2);
|
||||
System.out.println(list1.size()); // Erwartete Ausgabe: 6
|
||||
for(int i = 0; i < list1.size(); i++) {
|
||||
System.out.println(list1.get(i));
|
||||
}
|
||||
// Erwartete Ausgabe: 1, yes, 2, 2, yes, 1
|
||||
|
||||
if(list1.equals(list2)) {
|
||||
System.out.println("Listen sind gleich");
|
||||
} else {
|
||||
System.out.println("Listen sind ungleich");
|
||||
}
|
||||
// Erwartete Ausgabe: Listen sind ungleich
|
||||
|
||||
list1.add(list2, 1, 2);
|
||||
for(int i = 0; i < list1.size(); i++) {
|
||||
System.out.println(list1.get(i));
|
||||
}
|
||||
// Erwartete Ausgabe: 1, yes, 2, 2, yes, 1, yes
|
||||
|
||||
list1.add(list2, 3, 2);
|
||||
for(int i = 0; i < list1.size(); i++) {
|
||||
System.out.println(list1.get(i));
|
||||
}
|
||||
// Erwartete Ausgabe: 1, yes, 2, 2, yes, 1, yes
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue