In which case will the command be executed?

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

The discussion centers on the execution of the command if (pq == NULL) then L = p; within the llinsert algorithm for inserting an element into a sorted linked list. This command executes only when the list pointer L is initially NULL, indicating that the list is empty. If the new element x is smaller than the data of the first node, the pointer L is updated to point to the new node p. The variable pq remains NULL if the list is empty or if x is greater than or equal to the first node's data.

PREREQUISITES
  • Understanding of linked list data structures
  • Familiarity with pointer manipulation in C/C++
  • Knowledge of algorithmic complexity and insertion operations
  • Basic understanding of conditional statements in programming
NEXT STEPS
  • Study linked list insertion algorithms in C/C++
  • Learn about pointer operations and memory management in C/C++
  • Explore edge cases in linked list implementations
  • Investigate algorithm complexity analysis for linked list operations
USEFUL FOR

Software developers, computer science students, and anyone interested in understanding linked list algorithms and pointer manipulation in C/C++ programming.

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
2K
  • · 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