In which case will the command be executed?

  • MHB
  • Thread starter evinda
  • Start date
In summary, the algorithm tries to find the first node of the list with the smallest data, and then changes the pointer to that node.
  • #1
evinda
Gold Member
MHB
3,836
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
  • #2
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)
 
  • #3
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.
 
  • #4
I like Serena said:
Hi! (Happy)

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

A ok... I see... (Nod)
 
  • #5
What johng said. (Blush)
 
  • #6
I like Serena said:
What johng said. (Blush)

Oh yes, right... But also if L was NULL at the beginning, right?
 
  • #7
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)
 
  • #8
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)
 

FAQ: In which case will the command be executed?

When is a command executed?

A command is executed when it is called by the program or when its conditions are met. This can vary depending on the type of command and the specific programming language being used.

How do I know if a command will be executed?

You can determine if a command will be executed by looking at the program's logic and flow. If the conditions for the command are met, it will be executed. You can also use debugging tools to track the execution of commands.

What happens if the conditions for a command are not met?

If the conditions for a command are not met, it will not be executed. The program will move on to the next line of code or follow a different logic path. In some cases, an error may occur if the program expects the command to be executed.

Can a command be executed more than once?

Yes, a command can be executed multiple times depending on the program's logic and conditions. For example, a loop can execute a command multiple times until a specific condition is met.

Is the order of execution of commands important?

Yes, the order of execution of commands can be crucial in determining the outcome of a program. Some commands may need to be executed before others in order for the program to function correctly. It is important to carefully consider the order of commands when writing code.

Similar threads

Replies
9
Views
2K
Replies
2
Views
2K
Replies
1
Views
1K
Replies
1
Views
1K
Replies
2
Views
1K
Replies
1
Views
1K
Replies
3
Views
1K
Back
Top