From 455469992ca3465700cb50abef0d753fa73068b3 Mon Sep 17 00:00:00 2001 From: Adrian Y <66759189+adryue@users.noreply.github.com> Date: Wed, 21 May 2025 14:43:18 -0700 Subject: [PATCH] Update antipatterns.md, fixing an unintentional error in the code There seems to be an unintentional error in the code for the linked lists sections. The text explains that the code is supposed to add up the elements of a linked list, yet for all 3 versions of the code (including the "correct code" version), the sum is only assigned to the value in each individual node, not adding up all the values. I fixed the code by changing `sum = p->data;` to `sum += p->data;` for all 3 versions. (This wasn't even the error that the example was trying to showcase! I was very confused when I first read through it.) --- _topics/antipatterns.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/_topics/antipatterns.md b/_topics/antipatterns.md index 0ee88c8..299f0ad 100644 --- a/_topics/antipatterns.md +++ b/_topics/antipatterns.md @@ -281,7 +281,7 @@ symptom that shows this code is wrong? sum += p->data; while (p->next != NULL) { p = p-> next; - sum = p->data; + sum += p->data; } return sum; } @@ -299,7 +299,7 @@ the symptom that shows this code is wrong? sum += p->data; while (p != NULL) { p = p-> next; - sum = p->data; + sum += p->data; } return sum; } @@ -312,14 +312,14 @@ The correct code is this: int sum=0; Node *p=head; while (p != NULL) { - sum = p->data; + sum += p->data; p = p-> next; } return sum; } ``` -Which can be expresssed more succinetly with a for loop. Using a for +Which can be expressed more succinctly with a for loop. Using a for loop is less error prone, since everything is more likely to end up where it should: