Im pretty new to programming,so I need to filter a list by a predicate and make a new list of the results without changing the list I filtered. My Problem is, that always all elements of the list to be filtered get put into the newList, and i dont know why.
Edit: I am not allowed to use build in - functions like filter.
fun filter(predicate: (Order) -> Boolean): OrderProcessing {
var currentNode = first
var newList: OrderProcessing = OrderProcessing()
while (currentNode != null) {
var next = currentNode.next
if (predicate(currentNode.order)) {
if (newList.first != null) {
var newerNode = newList.first
while (newerNode != null) {
newerNode = newerNode.next
}
currentNode.next = null
newerNode?.next = currentNode
println(currentNode.next)
}
else newList.first = currentNode
}
currentNode = next
}
println(newList)
return newList