- #1
evinda
Gold Member
MHB
- 3,836
- 0
Hi! (Wave)
I am looking at the operations of a static queue.
I have to find the complexity of the above algorithm. Isn't it $\Theta(1)$? (Thinking)
Could you explain me this command: [m](Q->A)[(Q->Front + Q->Length –1)% N] = x; [/m]? Also, the algorithm [m] Enqueue [/m] of the queue, as a linked list, is this:
Could you explain me why both pointers [m] Q->Back->next[/m] and [m] Q->Back[/m] have to point to [m]P[/m] ? (Thinking)At the algorithm [m] Dequeue[/m]:
why are these lines needed?
I am looking at the operations of a static queue.
Code:
pointer MakeEmptyQueue(void)
pointer Q; /* temporary pointer */
Q = NewCell(Queue); /* malloc() */
Q->Front = 0;
Q->Length = 0;
return Q;
I have to find the complexity of the above algorithm. Isn't it $\Theta(1)$? (Thinking)
Code:
void Enqueue(pointer Q, Type x)
if (Q->Length == N) then error
else {
Q->Length = Q->Length+1;
(Q->A)[(Q->Front + Q->Length –1)% N] = x;
}
Could you explain me this command: [m](Q->A)[(Q->Front + Q->Length –1)% N] = x; [/m]? Also, the algorithm [m] Enqueue [/m] of the queue, as a linked list, is this:
Code:
void Enqueue(Type x, pointer Q)
pointer P; /* temporary pointer */
P = NewCell(Node);
P->data = x;
P->next = NULL;
if (IsEmptyQueue(Q)) then Q->Front = P;
else Q->Back->next = P;
Q->Back = P;
Could you explain me why both pointers [m] Q->Back->next[/m] and [m] Q->Back[/m] have to point to [m]P[/m] ? (Thinking)At the algorithm [m] Dequeue[/m]:
Code:
Type Dequeue(pointer Q)
if (IsEmptyQueue(Q)) then error;
else {
x = (Q->Front)->data;
Q->Front = (Q->Front)->next;
if (Q->Front == NULL) then
Q->Back = NULL;
return x;
}
why are these lines needed?
Code:
if (Q->Front == NULL) then
Q->Back = NULL;
Last edited: