Bird
0
0
DSA Cprogramming~10 mins

Implement Stack Using Queue in DSA C - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to initialize the queue size in the stack structure.

DSA C
typedef struct {
    int arr[100];
    int front, rear;
} Queue;

typedef struct {
    Queue q;
    int [1];
} Stack;

void initStack(Stack* s) {
    s->[1] = 0;
    s->q.front = 0;
    s->q.rear = -1;
}
Drag options to blanks, or click blank then click option'
Asize
Blength
Ccount
Dtop
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'size' or 'length' which are not updated properly.
Confusing queue indices with stack top.
2fill in blank
medium

Complete the code to enqueue an element into the queue.

DSA C
void enqueue(Queue* q, int val) {
    q->rear++;
    q->arr[q->[1]] = val;
}
Drag options to blanks, or click blank then click option'
Arear
Bfront
Ctop
Dsize
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'front' instead of 'rear' causes overwriting the wrong position.
Using 'top' which is unrelated to queue indices.
3fill in blank
hard

Fix the error in the pop function to correctly remove the top element from the stack using the queue.

DSA C
int pop(Stack* s) {
    if (s->top == 0) return -1; // empty
    int i, val;
    for (i = 0; i < s->top - 1; i++) {
        val = s->q.arr[s->q.front];
        s->q.front++;
        s->q.arr[++s->q.rear] = val;
    }
    val = s->q.arr[s->q.front];
    s->q.front++;
    s->top--;
    [1];
    return val;
}
Drag options to blanks, or click blank then click option'
As->q.front = 0
Bs->q.rear = s->q.front - 1
Cs->q.rear = s->q.front
Ds->q.front = s->q.rear
Attempts:
3 left
💡 Hint
Common Mistakes
Not updating rear causes incorrect queue state.
Setting rear equal to front causes empty queue misinterpretation.
4fill in blank
hard

Fill both blanks to implement the push function that adds an element to the stack using the queue.

DSA C
void push(Stack* s, int val) {
    s->q.arr[++s->q.[1]] = val;
    s->top++;
    int i, size = s->top;
    for (i = 0; i < size - 1; i++) {
        int temp = s->q.arr[s->q.[2]];
        s->q.front++;
        s->q.arr[++s->q.rear] = temp;
    }
}
Drag options to blanks, or click blank then click option'
Arear
Bfront
Ctop
Dsize
Attempts:
3 left
💡 Hint
Common Mistakes
Using front instead of rear to insert new element.
Not rotating elements correctly after insertion.
5fill in blank
hard

Fill all three blanks to implement the isEmpty function that checks if the stack is empty.

DSA C
int isEmpty(Stack* s) {
    if (s->[1] == 0 && s->q.[2] > s->q.[3]) {
        return 1;
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
Atop
Bfront
Crear
Dsize
Attempts:
3 left
💡 Hint
Common Mistakes
Checking only top or only queue indices.
Using size which is not maintained.