Skip to content

Commit 02c0f56

Browse files
authored
Update 01 Queue using array enqueue & dequeue .cpp
1 parent 3632363 commit 02c0f56

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

Diff for: Queue/01 Queue using array enqueue & dequeue .cpp

+13-11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// deletion will be always done from front
2+
// insertion will be always done from rear
13
#include<stdio.h>
24
#include<stdlib.h>
35

@@ -18,25 +20,25 @@ void Create(struct Queue* q, int size)
1820
}
1921
void enqueue(struct Queue* q, int x)
2022
{
21-
if (q->rear == q->size - 1)
23+
if (q->rear == q->size - 1) // condition for checking either queue is full or not
2224
printf("queue is full");
23-
else
25+
else // if queue is not full then we can insert element in queue
2426
{
25-
q->rear++;
26-
q->Q[q->rear] = x;
27+
q->rear++; // increatment the rear and then insert element
28+
q->Q[q->rear] = x;// inserting element in the queue
2729
}
2830
}
2931
int dequeue(struct Queue* q)
3032
{
31-
int x = -1;
32-
if (q->front == q->rear)
33+
int x = -1; // assign x with -1 intially
34+
if (q->front == q->rear) // condition for checking the either queue is empty or not
3335
printf("Queue is empty");
34-
else
36+
else // if there is element in the queue
3537
{
36-
q->front++;
37-
x=q->Q[q->front];
38+
q->front++; // increment the front and then take out the front element
39+
x=q->Q[q->front]; // store deleted element in the x
3840
}
39-
return x;
41+
return x; // and return it
4042
}
4143
void Display(struct Queue q)
4244
{
@@ -56,4 +58,4 @@ int main()
5658
Display(q);
5759
printf("%d ",dequeue(&q));
5860
return 0;
59-
}
61+
}

0 commit comments

Comments
 (0)