In which case will the command be executed?

  • Context: MHB 
  • Thread starter Thread starter evinda
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around an algorithm for inserting an element into a sorted linked list, specifically focusing on the conditions under which a particular command in the algorithm is executed. Participants explore the implications of the algorithm's logic and the state of the list before the insertion.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • Some participants propose that the command if (pq == NULL) then L = p; will execute only if the list L was initially NULL.
  • Others argue that this command will also execute if the new element x is smaller than the data of the first node, leading to L pointing to the new first node p.
  • A later reply clarifies that the variable pq remains NULL unless the loop condition is satisfied, indicating that if the loop does not execute, it implies either q was NULL from the start or L->data >= x.

Areas of Agreement / Disagreement

Participants generally agree that the command executes under specific conditions related to the initial state of L and the value of x. However, there are nuances in the interpretations of these conditions, indicating some level of disagreement on the exact circumstances.

Contextual Notes

Participants discuss the implications of the algorithm's logic without resolving all assumptions about the initial state of the list and the values involved.

evinda
Gold Member
MHB
Messages
3,741
Reaction score
0
Hello! (Wave)

I am looking at an algorithm, that inserts an element in a sorted list:

Code:
void llinsert(Type x, pointer L)
       pointer C, ptr; 
       q = L;
       pq = NULL;
       while (q != NULL) and (q->data < x) {
                pq = q;
                q = q->next;
       }
       if (q != NULL) and (q->data == x) then return;

       p = NewCell(Node); 
       p->data = x;
       p->next = q;
       if (pq == NULL) then L = p;
       else pq->next = p;

In which case will the command [m] if (pq == NULL) then L = p; [/m] be executed? :confused:
 
Technology news on Phys.org
evinda said:
Hello! (Wave)

I am looking at an algorithm, that inserts an element in a sorted list:

Code:
void llinsert(Type x, pointer L)
       pointer C, ptr; 
       q = L;
       pq = NULL;
       while (q != NULL) and (q->data < x) {
                pq = q;
                q = q->next;
       }
       if (q != NULL) and (q->data == x) then return;

       p = NewCell(Node); 
       p->data = x;
       p->next = q;
       if (pq == NULL) then L = p;
       else pq->next = p;

In which case will the command [m] if (pq == NULL) then L = p; [/m] be executed? :confused:

Hi! (Happy)

Apparently only if L was NULL to begin with. (Mmm)
 
I think I like Serena meant to say:
If x is smaller that the data of the first node of the list, then the pointer to the first node of the list (namely L) is changed to point to the new first node *p.
 
I like Serena said:
Hi! (Happy)

Apparently only if L was NULL to begin with. (Mmm)

A ok... I see... (Nod)
 
What johng said. (Blush)
 
I like Serena said:
What johng said. (Blush)

Oh yes, right... But also if L was NULL at the beginning, right?
 
evinda said:
Oh yes, right... But also if L was NULL at the beginning, right?

Yep. (Wink)

Variable pq is initially set to NULL, and it only changes if the condition [m](q != NULL) and (q->data < x)[/m] is true at least once.
If that never happens, then that means that either q was NULL at the beginning (meaning L is NULL), or we initially had that [m]q->data >= x[/m], meaning [m]L->data >= x[/m]. (Nerd)
 
johng said:
I think I like Serena meant to say:
If x is smaller that the data of the first node of the list, then the pointer to the first node of the list (namely L) is changed to point to the new first node *p.

I like Serena said:
Yep. (Wink)

Variable pq is initially set to NULL, and it only changes if the condition [m](q != NULL) and (q->data < x)[/m] is true at least once.
If the never happens, then that means that either q was NULL at the beginning (meaning L is NULL), or we initially had that [m]q->data >= x[/m], meaning [m]L->data >= x[/m]. (Nerd)

I see.. Thanks a lot! (Smile)
 

Similar threads

  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
9
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
11
Views
4K