-3

I am working on a program in C++ for my class to prepend, append, insert, display items of a circular-list. I came across while loop functioning differently then a do-while loop for what to my mind would potentially output the same items. I am looking for an explanation to why the output is different with the code blocks below, thank you. If you have places were I can modify code to enhance it please let me know trying to build as much information as possible again Thank You.

Link to full code: Pastebin

    while(temp != head)
    {
        cout << temp->data << " ";
        temp = temp->next;
    } 
Output: Nothing
        do
        {
            cout << temp->data << " ";
            temp = temp->next;
        }while(temp != head);

Output: 10 20
  • When you have a while loop the condition is done before the first iteration of the loop. In your case temp is probably pointing to head so the code doesn't enter the while loop. In the do while example temp can point to head and the first iteration will work because the first time the check is made is after you update temp to not point at the head. Just make sure that temp is not NULL or you will be in trouble in both cases. – Jerry Jeremiah Jun 01 '22 at 21:15
  • 2
    It is **very** pertinent to this question that [in your real code](https://pastebin.com/UKjK8Lpx), both loops are preceeded by `Node* temp = head;` – Drew Dormann Jun 01 '22 at 21:16
  • 0) do something. check for a condition, if not satisfied, repeat. (1) check for a condition. if satisfied, we're done. Otherwise, do something and repeat. - Surely you can see that with one of the control structures, we always perform some task, yet with the other method, we may not perform any work. – enhzflep Jun 01 '22 at 21:19
  • Given the line `temp = head;` before these loops, why do you think the while loop would print anything? Or why do you think the do-while loop would print nothing? Without knowing what you were expecting from each loop, perhaps the best advice is to [step through your code in a debugger to see what is happening](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems). – Drew Dormann Jun 01 '22 at 21:25

3 Answers3

2

A while loop evaluates its condition before entering the body. So, a while loop may run 0 iterations.

A do..while loop evaluates its condition after leaving the body. So, a do..while loop will always run at least 1 iteration.

Using a while loop will not work in this situation, since the condition (temp != head) will always be false before the 1st iteration. If the list is empty, head will be NULL, so temp will then be set to NULL, thus (NULL != NULL) will be false. Otherwise, head will not be NULL, and temp will be set to point at the same Node that head is pointing at, thus (temp != head) will still be false.

Whereas, using a do..while loop instead, the condition (temp != head) will be false only after the last Node in the list has been iterated.

So, using a do..while loop is the way to go in this situation.

However, your code is not accounting for the possibility of an empty list (head is NULL), so you need to add that check to avoid dereferencing a NULL pointer, eg:

void displayData()
{
    if (head) // <-- add this!
    {
        Node* temp = head;
        do
        {
            cout << temp->data << " ";
            temp = temp->next;
        }
        while (temp != head);
    }
}   

Online Demo


Also, your appendNode() can be simplified:

void appendNode(int newVal)
{
    Node** temp = &head;
    if (head) {
        do {
            temp = &((*temp)->next);
        }
        while (*temp != head);
    }
    *temp = new Node;
    (*temp)->data = newVal;
    (*temp)->next = head;
}

Online Demo

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
1

the while loop doesn't run because you set temp to point to head right before the while condition resulting in a false condition.

in the do while loop the code runs before checking the condition and that is why you see different results.

do
{
    cout << temp->data << " ";
    temp = temp->next;
}while(temp != head);

is the same as

cout << temp->data << " ";
temp = temp->next;
while(temp != head)
{
    cout << temp->data << " ";
    temp = temp->next;
}

for other recommendations: You can remove line 24 because later in the code newNode->next is given a new value.

Trevor O
  • 11
  • 2
0

while loop checks the condition first and then executes the statement(s), whereas do while loop will execute the statement(s) at least once, then the condition is checked.

AleXelton
  • 767
  • 5
  • 27
Vũ Quách
  • 26
  • 1
  • 4