Bird
Raised Fist0
DSA Cprogramming~30 mins

Queue vs Stack When to Use Which in DSA C - Build Both Approaches

Choose your learning style10 modes available
Queue vs Stack: When to Use Which
📖 Scenario: Imagine you are managing a line at a coffee shop and a stack of plates in the kitchen. You want to understand how to use a queue and a stack to handle these situations correctly.
🎯 Goal: You will create a simple program in C that shows how to add and remove items from a queue and a stack. This will help you see when to use each data structure.
📋 What You'll Learn
Create an array-based queue with fixed size 5
Create an array-based stack with fixed size 5
Add 3 items to both queue and stack
Remove 1 item from both queue and stack
Print the current state of queue and stack after operations
💡 Why This Matters
🌍 Real World
Queues are used in places like waiting lines, task scheduling, and printer job management. Stacks are used in undo features, expression evaluation, and backtracking algorithms.
💼 Career
Understanding when to use queues or stacks helps in writing efficient programs for real-world problems like managing tasks, processing data streams, and implementing algorithms.
Progress0 / 4 steps
1
Create the Queue and Stack arrays
Create an integer array called queue with size 5 and an integer array called stack with size 5. Also create integer variables front and rear for the queue, both set to -1, and an integer variable top for the stack set to -1.
DSA C
Hint

Use arrays for queue and stack. Initialize front, rear, and top to -1 to show they are empty.

2
Add 3 items to the Queue and Stack
Add the integers 10, 20, and 30 to the queue using rear to track the last item, and to the stack using top to track the last item. Update front to 0 when adding the first item to the queue.
DSA C
Hint

For the queue, set front and rear to 0 before adding the first item. For the stack, increase top before adding each item.

3
Remove 1 item from the Queue and Stack
Remove one item from the queue by increasing front by 1. Remove one item from the stack by decreasing top by 1.
DSA C
Hint

For the queue, move front forward by 1 to remove the first item. For the stack, move top backward by 1 to remove the last item.

4
Print the current state of Queue and Stack
Print the current items in the queue from front to rear using a for loop with variable i. Then print the current items in the stack from top down to 0 using a for loop with variable j. Use printf to print each item separated by spaces. Print "Queue:" before the queue items and "Stack:" before the stack items.
DSA C
Hint

Use for loops to print queue items from front to rear and stack items from top down to 0. Print labels before each list.

Practice

(1/5)
1. Which data structure should you use when you want to process tasks in the exact order they arrive, like customers waiting in line at a store?
easy
A. Queue, because it processes elements in First-In-First-Out order
B. Stack, because it processes elements in Last-In-First-Out order
C. Array, because it allows random access
D. Tree, because it organizes data hierarchically

Solution

  1. Step 1: Understand the order of processing tasks

    Tasks arriving in order and processed in the same order means First-In-First-Out (FIFO).
  2. Step 2: Match FIFO with the correct data structure

    Queue follows FIFO, so it fits the scenario of customers waiting in line.
  3. Final Answer:

    Queue, because it processes elements in First-In-First-Out order -> Option A
  4. Quick Check:

    FIFO = Queue [OK]
Hint: FIFO means use Queue, like a line of customers [OK]
Common Mistakes:
  • Confusing FIFO with LIFO
  • Choosing Stack for ordered processing
  • Thinking arrays maintain insertion order automatically
2. Which of the following is the correct way to push an element onto a stack in C?
easy
A. push(element, stack);
B. stack.push(element);
C. stack->push(element);
D. push(stack, element);

Solution

  1. Step 1: Recall typical C stack function syntax

    In C, stack operations are usually functions taking the stack and element as arguments.
  2. Step 2: Identify correct function call order

    push(stack, element) is the standard way to push an element onto a stack in C.
  3. Final Answer:

    push(stack, element); -> Option D
  4. Quick Check:

    Function call with stack first = push(stack, element) [OK]
Hint: In C, function calls usually have data structure first [OK]
Common Mistakes:
  • Using object.method() syntax which is not C style
  • Swapping argument order in function call
  • Assuming stack is a pointer with methods
3. What will be the output of the following C code snippet using a stack?
#include 
#include 

#define MAX 5

typedef struct {
    int items[MAX];
    int top;
} Stack;

void push(Stack *s, int val) {
    if (s->top == MAX - 1) return;
    s->items[++s->top] = val;
}

int pop(Stack *s) {
    if (s->top == -1) return -1;
    return s->items[s->top--];
}

int main() {
    Stack s;
    s.top = -1;
    push(&s, 10);
    push(&s, 20);
    push(&s, 30);
    printf("%d ", pop(&s));
    printf("%d ", pop(&s));
    return 0;
}
medium
A. 10 20
B. 30 20
C. 20 10
D. 30 10

Solution

  1. Step 1: Trace push operations on stack

    Push 10, stack: [10]; push 20, stack: [10,20]; push 30, stack: [10,20,30]. Top points to 30.
  2. Step 2: Trace pop operations and output

    First pop returns 30, second pop returns 20, so output is "30 20 ".
  3. Final Answer:

    30 20 -> Option B
  4. Quick Check:

    LIFO pop order = 30 then 20 [OK]
Hint: Stack pops last pushed first (LIFO) [OK]
Common Mistakes:
  • Assuming FIFO order for stack
  • Mixing push and pop order
  • Ignoring initial top = -1
4. The following C code tries to implement a queue but has a bug. What is the main issue?
#include 
#define MAX 3

int queue[MAX];
int front = -1, rear = -1;

void enqueue(int val) {
    if (rear == MAX - 1) {
        printf("Queue full\n");
        return;
    }
    queue[++rear] = val;
    if (front == -1) front = 0;
}

int dequeue() {
    if (front == -1 || front > rear) {
        printf("Queue empty\n");
        return -1;
    }
    return queue[front++];
}

int main() {
    enqueue(1);
    enqueue(2);
    enqueue(3);
    dequeue();
    enqueue(4);
    printf("%d\n", queue[rear]);
    return 0;
}
medium
A. The enqueue function uses wrong index for insertion
B. The dequeue function returns wrong element
C. The queue does not reset front and rear after dequeueing all elements, causing overflow
D. The queue size is too large

Solution

  1. Step 1: Understand enqueue and dequeue behavior

    Enqueue adds at rear, dequeue removes from front. After dequeueing all, front > rear but variables not reset.
  2. Step 2: Identify problem with overflow after dequeue

    After dequeueing, rear stays at max index, so enqueue thinks queue is full even if space is free.
  3. Final Answer:

    The queue does not reset front and rear after dequeueing all elements, causing overflow -> Option C
  4. Quick Check:

    Queue pointers not reset = overflow bug [OK]
Hint: Reset front and rear when queue becomes empty [OK]
Common Mistakes:
  • Ignoring front and rear reset after emptying queue
  • Thinking enqueue index is wrong
  • Assuming dequeue returns wrong element
5. You need to design a system that supports undo and redo operations for text editing. Which data structure(s) should you use and why?
hard
A. Two stacks: one for undo and one for redo, because stacks support LIFO order needed for undo/redo
B. One queue, because it processes commands in order
C. One stack only, because redo is not needed
D. Two queues, because they maintain order of commands

Solution

  1. Step 1: Understand undo/redo operation order

    Undo reverses last action (LIFO), redo re-applies undone actions in reverse order.
  2. Step 2: Choose data structures matching LIFO behavior

    Two stacks allow pushing actions for undo and redo separately, supporting LIFO order for both.
  3. Final Answer:

    Two stacks: one for undo and one for redo, because stacks support LIFO order needed for undo/redo -> Option A
  4. Quick Check:

    Undo/Redo = Two stacks (LIFO) [OK]
Hint: Undo/Redo needs two stacks for LIFO operations [OK]
Common Mistakes:
  • Using queues which are FIFO, not suitable for undo
  • Ignoring redo functionality
  • Using only one stack which can't handle redo
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard