'Swift + Not Deleting Current Item in Single Linked List
I have a linked list that is not deleting the current item based off the inserted value which in the case below is the value of 5.0. I want to delete 5.0 from my inserted values but the console is still printing out all inserted values of 5.0, 8.0, and 10.0. I tried changing the conditions in func delete() and tried several changes in the block of code that is executed in func delete() as well. I suspect something is backwards with head?.nextNodeAddress = head?.nextNodeAddress inside func delete(). Also, does the current value actually get deleted in a linked list when setting head (e.g. pointer) to the next node address or does it just overlook the current node value that matches the inserted value?
Note: I did not include func insert(value:) because it did not seem to pertain to the issue. The value insert as expected.
//Node Class:
class Node {
let value: Double
var nextNodeAddress: Node?
init(value: Double, next: Node?) {
self.value = value
self.nextNodeAddress = next
}
}
//Linked List
class LinkedList {
var head: Node?
func delete(value: Double) {
if head?.value != value {
head?.nextNodeAddress = head?.nextNodeAddress
}
}
func displayListItems() {
var current = head
while current != nil {
print(current?.value ?? "")
current = current?.nextNodeAddress
}
}
}
let boltSize = LinkedList()
boltSize.insert(value: 5.0)
boltSize.insert(value: 8.0)
boltSize.insert(value: 10.0)
boltSize.delete(value: 5.0)
boltSize.displayListItems()
Solution 1:[1]
As Rob stated above, it's best to add your insert function as well so that we can rule out any bugs in the insert.
However, I does not seem like the delete function does anything.
Let's trace it.
After your three inserts your linked list looks like this assuming your insert works fine.
node(5.0) -> node(8.0) -> node(10.0) and Head is pointing to node(5.0)
Then you call boltSize.delete(value: 5.0)
and let's look at what the function does
func delete(value: Double) {
if head?.value != value {
head?.nextNodeAddress = head?.nextNodeAddress
}
}
- You pass the function
5.0 - The it compares
Head.value!=5.0which is5.0 != 5.0- false - So your next line of code does not run anyways and nothing gets deleted
Let's take another example, say you want to delete 8.0
- You pass the function
8.0 - The it compares
Head.value!=5.0which is5.0 != 8.0- true - So inside the
ifblockhead?.nextNodeAddress = head?.nextNodeAddress- Head's next node address isNode(8.0)and this line keeps itNode(8.0) - The function ends
So as you can see two issues:
- There is no deletion happening
- Your delete code does not have a loop to reach the correct node to delete as it ends after one iteration and comparison with the head
This is how I would approach the deletion and actually is similar to what you did in your display function:
func delete(_ value: Int)
{
// Check if head itself has the data to be deleted
if head?.value == value
{
// The new head is the node after head
head = head?.nextNodeAddress
// We can exit, nothing more to do
return
}
// Create a temp node pointer that will iterate through
// your linked list starting starting from the head
var currentNode = head
// Loop through the list till you reach the end
while currentNode?.nextNodeAddress != nil
{
// Check if the next node has the value we want to delete
if currentNode?.nextNodeAddress?.value == value
{
// Retrieve the next node
let nextNode = currentNode?.nextNodeAddress
// Assign current node's next to next node's next
// This way, there is no link to the next node anymore
currentNode?.nextNodeAddress = nextNode?.nextNodeAddress
// The delete is complete and we can exit
return
}
// Since the node was not found in this iteration, advance
// the pointer
currentNode = currentNode?.nextNodeAddress
}
}
This is probably more verbose way to clarify what is going on, you can feel free to shorten it as you see fit.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Shawn Frank |
