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 {
|
public class LinkedByteList implements ByteList {
|
||||||
|
|
||||||
private NodeLinked head;
|
|
||||||
private NodeLinked currNode;
|
private NodeLinked currNode;
|
||||||
private int size;
|
private int size;
|
||||||
|
private NodeLinked last;
|
||||||
public LinkedByteList() {
|
private NodeLinked first;
|
||||||
head = null;
|
int result;
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void add(byte value) {
|
public void add(byte value) {
|
||||||
NodeLinked newNode = new NodeLinked(value); // erzeuge neues Node
|
NodeLinked newNode = new NodeLinked(value); // erzeuge neues Node
|
||||||
if (head == null) {
|
if (first == null) {
|
||||||
head = newNode;
|
first = newNode;
|
||||||
} else {
|
} else {
|
||||||
NodeLinked last = head;
|
NodeLinked last = first;
|
||||||
while (last.next != null) {
|
while (last.next != null) {
|
||||||
last = last.next;
|
last = last.next;
|
||||||
}
|
}
|
||||||
@ -30,33 +28,78 @@ public class LinkedByteList implements ByteList {
|
|||||||
@Override
|
@Override
|
||||||
public void add(int index, byte value) {
|
public void add(int index, byte value) {
|
||||||
if (index == size) {
|
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
|
@Override
|
||||||
public void remove(int index) {
|
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
|
@Override
|
||||||
public void set(int index, byte value) {
|
public void set(int index, byte value) {
|
||||||
NodeLinked x = node(index);
|
NodeLinked x = node(index);
|
||||||
x.value = value;
|
x.item = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
NodeLinked node(int index) {
|
NodeLinked node(int index) {
|
||||||
NodeLinked x = head;
|
NodeLinked current = first;
|
||||||
for (int i = 0; i < index; i++) {
|
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
|
@Override
|
||||||
public byte get(int index) {
|
public byte get(int index) {
|
||||||
return node(index).value;
|
return node(index).item;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -66,7 +109,17 @@ public class LinkedByteList implements ByteList {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean contains(int value) {
|
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
|
@Override
|
||||||
@ -76,12 +129,12 @@ public class LinkedByteList implements ByteList {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void clear() {
|
public void clear() {
|
||||||
for (NodeLinked x = head; x != null;) {
|
for (NodeLinked x = first; x != null;) {
|
||||||
NodeLinked next = x.next;
|
NodeLinked next = x.next;
|
||||||
x.next = null;
|
x.next = null;
|
||||||
x = next;
|
x = next;
|
||||||
}
|
}
|
||||||
head = null;
|
first = null;
|
||||||
size = 0;
|
size = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -90,21 +143,12 @@ public class LinkedByteList implements ByteList {
|
|||||||
return null;
|
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
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
System.out.print("LinkedList: ");
|
System.out.print("LinkedList: ");
|
||||||
currNode = head;
|
currNode = first;
|
||||||
while (currNode != null) {
|
while (currNode != null) {
|
||||||
System.out.print(currNode.value + " ");
|
System.out.print(currNode.item + " ");
|
||||||
currNode = currNode.next;
|
currNode = currNode.next;
|
||||||
}
|
}
|
||||||
return String.valueOf(currNode).replace("null", "");
|
return String.valueOf(currNode).replace("null", "");
|
||||||
|
|||||||
@ -31,8 +31,13 @@ public class Main {
|
|||||||
list.add((byte) 5);
|
list.add((byte) 5);
|
||||||
list.add((byte) -100);
|
list.add((byte) -100);
|
||||||
|
|
||||||
|
|
||||||
System.out.println(list);
|
System.out.println(list);
|
||||||
|
|
||||||
|
// list.remove(4);
|
||||||
|
// list.contains(5);
|
||||||
|
System.out.println(list.contains(2));
|
||||||
|
|
||||||
// list.add(0, (byte) 99);
|
// list.add(0, (byte) 99);
|
||||||
// list.add(3, (byte) 99);
|
// list.add(3, (byte) 99);
|
||||||
//// System.out.println(list);
|
//// System.out.println(list);
|
||||||
|
|||||||
@ -2,11 +2,22 @@ package de.vivi.list;
|
|||||||
|
|
||||||
public class NodeLinked {
|
public class NodeLinked {
|
||||||
|
|
||||||
byte value;
|
byte item;
|
||||||
NodeLinked next;
|
NodeLinked next;
|
||||||
|
NodeLinked prev;
|
||||||
|
|
||||||
public NodeLinked(byte value) {
|
public NodeLinked(NodeLinked prev, byte element, NodeLinked next) {
|
||||||
this.value = value;
|
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;
|
this.next = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user