forked from pabulaner/vivi
LinkedList remove und contain
This commit is contained in:
parent
acc84590fd
commit
657bcf5463
@ -4,21 +4,19 @@ import java.util.*;
|
||||
|
||||
public class LinkedByteList implements ByteList {
|
||||
|
||||
private NodeLinked head;
|
||||
private NodeLinked currNode;
|
||||
private int size;
|
||||
|
||||
public LinkedByteList() {
|
||||
head = null;
|
||||
}
|
||||
private NodeLinked last;
|
||||
private NodeLinked first;
|
||||
int result;
|
||||
|
||||
@Override
|
||||
public void add(byte value) {
|
||||
NodeLinked newNode = new NodeLinked(value); // erzeuge neues Node
|
||||
if (head == null) {
|
||||
head = newNode;
|
||||
if (first == null) {
|
||||
first = newNode;
|
||||
} else {
|
||||
NodeLinked last = head;
|
||||
NodeLinked last = first;
|
||||
while (last.next != null) {
|
||||
last = last.next;
|
||||
}
|
||||
@ -30,33 +28,78 @@ public class LinkedByteList implements ByteList {
|
||||
@Override
|
||||
public void add(int index, byte value) {
|
||||
if (index == size) {
|
||||
|
||||
linkLast(value);
|
||||
} else {
|
||||
linkBefore(value, node(index));
|
||||
}
|
||||
}
|
||||
|
||||
void linkBefore(byte value, NodeLinked succ) {
|
||||
NodeLinked pred = succ.prev;
|
||||
NodeLinked newNode = new NodeLinked(value, succ);
|
||||
succ.prev = newNode;
|
||||
if (pred == null)
|
||||
first = newNode;
|
||||
else
|
||||
pred.next = newNode;
|
||||
size++;
|
||||
}
|
||||
|
||||
void linkLast(byte value) {
|
||||
NodeLinked l = last;
|
||||
NodeLinked newNode = new NodeLinked(value);
|
||||
last = newNode;
|
||||
size++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(int index) {
|
||||
size--;
|
||||
NodeLinked current = first;
|
||||
if (index == 0) {
|
||||
size--;
|
||||
first = current.next;
|
||||
}
|
||||
|
||||
NodeLinked temp = null;
|
||||
for (int i = 1; i <= index; i++) {
|
||||
temp = current;
|
||||
current = current.next;
|
||||
}
|
||||
|
||||
temp.next = current.next;
|
||||
size--;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(int index, byte value) {
|
||||
NodeLinked x = node(index);
|
||||
x.value = value;
|
||||
x.item = value;
|
||||
}
|
||||
|
||||
NodeLinked node(int index) {
|
||||
NodeLinked x = head;
|
||||
NodeLinked current = first;
|
||||
for (int i = 0; i < index; i++) {
|
||||
x = x.next;
|
||||
current = current.next;
|
||||
}
|
||||
return x;
|
||||
return current;
|
||||
}
|
||||
// NodeLinked node(int index) {
|
||||
// if (index < (size >> 1)) {
|
||||
// NodeLinked x = first;
|
||||
// for (int i = 0; i < index; i++)
|
||||
// x = x.next;
|
||||
// return x;
|
||||
// } else {
|
||||
// NodeLinked x = last;
|
||||
// for (int i = size - 1; i > index; i--)
|
||||
// x = x.prev;
|
||||
// return x;
|
||||
// }
|
||||
// }
|
||||
|
||||
@Override
|
||||
public byte get(int index) {
|
||||
return node(index).value;
|
||||
return node(index).item;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -66,7 +109,17 @@ public class LinkedByteList implements ByteList {
|
||||
|
||||
@Override
|
||||
public boolean contains(int value) {
|
||||
return false;
|
||||
byte byteValue = (byte) value;
|
||||
boolean isEqual = false;
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (get(i) == byteValue) {
|
||||
isEqual = true;
|
||||
break;
|
||||
} else {
|
||||
isEqual = false;
|
||||
}
|
||||
}
|
||||
return isEqual;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -76,12 +129,12 @@ public class LinkedByteList implements ByteList {
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
for (NodeLinked x = head; x != null;) {
|
||||
for (NodeLinked x = first; x != null;) {
|
||||
NodeLinked next = x.next;
|
||||
x.next = null;
|
||||
x = next;
|
||||
}
|
||||
head = null;
|
||||
first = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
@ -90,21 +143,12 @@ public class LinkedByteList implements ByteList {
|
||||
return null;
|
||||
}
|
||||
|
||||
public Byte[] toArray() {
|
||||
Byte[] result = new Byte[size];
|
||||
int i = 0;
|
||||
for (NodeLinked x = head; x != null; x = x.next) {
|
||||
result[i++] = x.value;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
System.out.print("LinkedList: ");
|
||||
currNode = head;
|
||||
currNode = first;
|
||||
while (currNode != null) {
|
||||
System.out.print(currNode.value + " ");
|
||||
System.out.print(currNode.item + " ");
|
||||
currNode = currNode.next;
|
||||
}
|
||||
return String.valueOf(currNode).replace("null", "");
|
||||
|
||||
@ -31,8 +31,13 @@ public class Main {
|
||||
list.add((byte) 5);
|
||||
list.add((byte) -100);
|
||||
|
||||
|
||||
System.out.println(list);
|
||||
|
||||
// list.remove(4);
|
||||
// list.contains(5);
|
||||
System.out.println(list.contains(2));
|
||||
|
||||
// list.add(0, (byte) 99);
|
||||
// list.add(3, (byte) 99);
|
||||
//// System.out.println(list);
|
||||
|
||||
@ -2,11 +2,22 @@ package de.vivi.list;
|
||||
|
||||
public class NodeLinked {
|
||||
|
||||
byte value;
|
||||
byte item;
|
||||
NodeLinked next;
|
||||
NodeLinked prev;
|
||||
|
||||
public NodeLinked(byte value) {
|
||||
this.value = value;
|
||||
public NodeLinked(NodeLinked prev, byte element, NodeLinked next) {
|
||||
this.item = element;
|
||||
this.next = null;
|
||||
this.prev = null;
|
||||
}
|
||||
|
||||
public NodeLinked(byte element) {
|
||||
this.item = element;
|
||||
}
|
||||
|
||||
public NodeLinked(byte element, NodeLinked next) {
|
||||
this.item = element;
|
||||
this.next = null;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user