# Two Bugs in LinkedList data structure in Absolute Beginner's Guide to Algorithms

**URL:** <https://forum.kirupa.com/t/two-bugs-in-linkedlist-data-structure-in-absolute-beginners-guide-to-algorithms/665046>\
**Category:** Uncategorized\
**Created:** [February 18, 2024, 12:09am UTC](https://forum.kirupa.com/t/two-bugs-in-linkedlist-data-structure-in-absolute-beginners-guide-to-algorithms/665046 "2024-02-18T00:09:00Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![timfrobisher](https://avatars.discourse-cdn.com/v4/letter/t/df705f/32.png) [@timfrobisher](https://forum.kirupa.com/u/timfrobisher)\
**Post date:** [February 18, 2024, 12:09am UTC](https://forum.kirupa.com/t/two-bugs-in-linkedlist-data-structure-in-absolute-beginners-guide-to-algorithms/665046/1 "2024-02-18T00:09:00Z")

</div>

I found two bugs in the LinkedList data structure in Absolute Beginner’s Guide to Algorithms. Both involve not updating the tail pointer.

Fixed Code:

```
removeData(data) {
	if (this.size === 0) {
		throw new Error("List is empty");	
	}
	
	if (this.head.data === data) {
		this.head = this.head.next;
		this.size--;
		return;	
	}
	let current = this.head;
	
	while (current.next) {
		if (current.next.data === data) {
			if(current.next === this.tail) {
				this.tail = current;
			}
			current.next = current.next.next;
			this.size--;
			return;	
		}
		
		current = current.next;
	}
	
	throw new Error(`Node with data '${data}' not found in list!`);
}

addAfter(afterData, data) {
	const newNode = new LinkedListNode(data);
	
	if (this.size === 0) {
		this.head = newNode;
		this.size++;
		return;	
	}
	
	let current = this.head;
	
	while (current) {
		if (current.data === afterData) {
			newNode.next = current.next;
			if(current === this.tail) {
				this.tail = newNode;
			}
			current.next = newNode;
			this.size++;
			return;	
		}
		
		current = current.next;
	}
	
	throw new Error(`Node with data '${afterData}' not found in list!`);
}

```

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [February 19, 2024, 4:38am UTC](https://forum.kirupa.com/t/two-bugs-in-linkedlist-data-structure-in-absolute-beginners-guide-to-algorithms/665046/2 "2024-02-19T04:38:36Z")

</div>

@timfrobisher - thanks for pointing this out! You are totally right :facepalm:

I have posted the updated code here: [kirupa/data\_structures\_algorithms/linkedlist.htm at master · kirupa/kirupa · GitHub](https://github.com/kirupa/kirupa/blob/master/data_structures_algorithms/linkedlist.htm)

I will update the corresponding article and file the corrections tomorrow.

---

<div class="post-metadata">

**Author:** ![timfrobisher](https://avatars.discourse-cdn.com/v4/letter/t/df705f/32.png) [@timfrobisher](https://forum.kirupa.com/u/timfrobisher)\
**Post date:** [February 21, 2024, 7:21pm UTC](https://forum.kirupa.com/t/two-bugs-in-linkedlist-data-structure-in-absolute-beginners-guide-to-algorithms/665046/3 "2024-02-21T19:21:24Z")

</div>

Hi,

I found another issue with the book but don’t feel like it warrants a separate post. The issue is with the Towers of Hanoi explanation. There is a rather confusing formatting problem. I am using the Kindle version on the PC Kindle reader. The issue may be specific to this version.

(Imagine that I inserted an image of the induction proof here. I did. But I had to delete it since new users can only insert one embedded media item per post)

In any case, the proof states that the number of turns required to move the discs is 2n-1 instead of 2^n - 1 then concludes with the following blurb:  
 ![image](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/3/3/33bb452a1dd9a56d882b4c7c8e80be7b82ebe781.png)

As formatted, this literally states that because the process takes 263 seconds, the process takes 585 billion years.

Let’s do the timewarp again!

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [February 21, 2024, 7:59pm UTC](https://forum.kirupa.com/t/two-bugs-in-linkedlist-data-structure-in-absolute-beginners-guide-to-algorithms/665046/4 "2024-02-21T19:59:35Z")

</div>

That is indeed a formatting error. It should be 2^64 - 1, but I think superscripts are getting ignored in the Kindle edition. Let me flag this with the right team to see if they can quickly fix this.

---

<div class="post-metadata">

**Author:** ![timfrobisher](https://avatars.discourse-cdn.com/v4/letter/t/df705f/32.png) [@timfrobisher](https://forum.kirupa.com/u/timfrobisher)\
**Post date:** [February 22, 2024, 2:22am UTC](https://forum.kirupa.com/t/two-bugs-in-linkedlist-data-structure-in-absolute-beginners-guide-to-algorithms/665046/5 "2024-02-22T02:22:03Z")

</div>

Hi,

A couple more issues.

The main one is a bad variable in the breadthFirstTraversal function:

observed.enqueue(root);

But observed doesn’t exist. It should be

discovered.enqueue(root);

The other issue is with the call to Stack.length in the depthFirstTraversal function:

while (discovered.length \> 0) {

This works fine using the code provided online. However, there is no get length() function in the Stack implementation from the book. So, this will fail if using the book implementation of the Stack.

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [February 22, 2024, 6:18am UTC](https://forum.kirupa.com/t/two-bugs-in-linkedlist-data-structure-in-absolute-beginners-guide-to-algorithms/665046/6 "2024-02-22T06:18:51Z")

</div>

Thanks for flagging this! Here is the updated source with the traversal fully working: [kirupa/data\_structures\_algorithms/binary\_tree\_traversal.htm at master · kirupa/kirupa · GitHub](https://github.com/kirupa/kirupa/blob/master/data_structures_algorithms/binary_tree_traversal.htm)

I’ll make the updates in a few moments to the errata!

---

<div class="post-metadata">

**Author:** ![timfrobisher](https://avatars.discourse-cdn.com/v4/letter/t/df705f/32.png) [@timfrobisher](https://forum.kirupa.com/u/timfrobisher)\
**Post date:** [February 23, 2024, 11:54pm UTC](https://forum.kirupa.com/t/two-bugs-in-linkedlist-data-structure-in-absolute-beginners-guide-to-algorithms/665046/7 "2024-02-23T23:54:24Z")

</div>

Ok. I’m done with the book, so this is my last comment. (Really). And it is pretty minor.

In the selection sort chapter, when you were very clearly discussing selection sort, you accidentally wrote, “From a memory point of view, insertion sort is very good.”

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [February 24, 2024, 5:43am UTC](https://forum.kirupa.com/t/two-bugs-in-linkedlist-data-structure-in-absolute-beginners-guide-to-algorithms/665046/8 "2024-02-24T05:43:07Z")

</div>

Gah! I will flag that for fixing in the next printings 🙂

Thank you so much for your feedback! I’d like to send you an autographed copy of the book with possibly a little doodle as well. If you are OK with that, please DM me your address and I’ll ship it over to you quickly!
