⚙️ C programming basics · Class 10 · Lesson 14 of 18 · Free
Stacks & Queues in C – LIFO and FIFO with Arrays and Lists
⏱ About 40 minutes · ▶ run real C here — nothing to install · no sign-in
In this lesson you will
- Build a stack with push and pop, and check for full and empty
- Use a stack to check if brackets in an expression are balanced
- Build a circular queue and a linked-list queue with enqueue and dequeue
In the school canteen, clean plates are stacked: the last plate put on top is the first one taken. At the ticket counter it is the opposite: the first person in the line is served first. These two everyday rules are the stack and the queue, two of the most used data structures in all of programming.
What is a stack in C?
A stack is LIFO: Last In, First Out. You can only add (push) and remove (pop) at one end, called the top.
Here the stack is a struct (Lesson 4) with an array and an int top. top is the index of the top item, and -1 means empty.
push and pop return 1 for success and 0 for failure. Never pop an empty stack or push onto a full one: that would read or write outside the array. Try it: push four plates and see the "full" message.
Took plate 3 Took plate 2 Took plate 1 Stack is empty now
💡 `.top = -1` sets top; the items array is filled with zeros.
How do you check balanced brackets using a stack in C?
A classic use of a stack: are the brackets in a formula balanced? Every ( [ { must be closed by the matching ) ] } in the right order.
Push every opening bracket. For a closing bracket, pop and check that it matches. At the end the stack must be empty.
Compilers do this check on your C code too. Try it: add your own formula to the list, with a missing bracket.
(a+b)*[c-d] balanced
{(x+1)] NOT balanced
((2*3) NOT balanced
fee[(i+1)] balanced
What is a queue in C and what is a circular queue?
A queue is FIFO: First In, First Out. You add (enqueue) at the rear and remove (dequeue) at the front.
If front and rear only move forward, the array runs out even when the front part is empty again. A circular queue fixes this: when an index reaches the end, it wraps back to 0 using % (remainder).
We also keep a count, so full (count == CAP) and empty (count == 0) are easy to test. Try it: change CAP to 2 and run again.
Token 101: in line Token 102: in line Token 103: in line Token 104: line full, wait Served 101 Token 104 joins now Served 102 Served 103 Served 104
How do you make a queue with a linked list in C?
An array queue has a fixed size. A linked-list queue (Lesson 13) can grow as long as memory lasts.
Keep two pointers: front, to remove from, and rear, to add to. Both operations are instant: no walking through the list.
When the last node leaves, rear must become NULL too. Try it: enqueue "Print notes" after the loop and dequeue it.
Printing: Maths worksheet Printing: Science chart Printing: Fee receipt
Stack vs queue: which one should you use?
Put the same items into a stack and a queue, then take them out. The stack gives them back reversed; the queue keeps their order.
Use a stack when the newest item matters first: undo in an editor, the Back button, function calls (Lesson 11's call stack). Use a queue when fairness matters: tickets, print jobs, tasks waiting their turn.
Try it: add a fourth word to both and run again.
Stack gives: third second first Queue gives: first second third
🤔 Predict the output
Think like C: read the code and choose what it prints. You get the reason straight away.
Question 1. Push 1, 2, 3, pop, push 4, pop, pop. What does this print?
int s[5], top = -1;
s[++top] = 1;
s[++top] = 2;
s[++top] = 3;
printf("%d ", s[top--]);
s[++top] = 4;
printf("%d ", s[top--]);
printf("%d\n", s[top--]);Question 2. Enqueue A, B, C, dequeue, enqueue D, dequeue. What does this print?
char q[4];
int front = 0, rear = 0;
q[rear++] = 'A';
q[rear++] = 'B';
q[rear++] = 'C';
printf("%c ", q[front++]);
q[rear++] = 'D';
printf("%c\n", q[front++]);Question 3. In a circular queue with 5 places, rear is at the last index (4). Where does it move next?
int cap = 5, rear = 4;
rear = (rear + 1) % cap;
printf("%d\n", rear);🧩 Your challenge
Make the canteen token counter. Commands come one per line: `join NAME` adds a student to the queue (at most 3 waiting), `serve` serves the first student, `quit` stops. Print "> " before reading each command, then what happens. Example start for the inputs below: > join Asha Asha joined > join Ravi Ravi joined > serve Serving Asha ... > join Om Line full, Om must wait
> join Asha Asha joined > join Ravi Ravi joined > serve Serving Asha > join Meena Meena joined > join Kiran Kiran joined > join Om Line full, Om must wait > serve Serving Ravi > serve Serving Meena > serve Serving Kiran > serve Nobody is waiting > quit
⚠️ Common mistakes (and how to fix them)
❌ Wrong
#include <stdio.h>
struct Stack { int items[5]; int top; };
int pop(struct Stack *s) {
return s->items[s->top];
}
int main(void) {
struct Stack s = { { 10, 20, 30 }, 2 };
printf("%d ", pop(&s));
printf("%d\n", pop(&s));
return 0;
}
Compiles and runs, but prints: 30 30 ⏎
✅ Right
#include <stdio.h>
struct Stack { int items[5]; int top; };
int pop(struct Stack *s) {
return s->items[s->top--];
}
int main(void) {
struct Stack s = { { 10, 20, 30 }, 2 };
printf("%d ", pop(&s));
printf("%d\n", pop(&s));
return 0;
}
This pop only looks at the top (that is peek). It never moves top down, so every pop returns the same item. Pop must also do top--.
❌ Wrong
#include <stdio.h>
struct Stack { int items[5]; int top; };
void push(struct Stack s, int x) {
s.items[++s.top] = x;
}
int main(void) {
struct Stack s = { .top = -1 };
push(s, 7);
push(s, 8);
printf("Items on stack: %d\n", s.top + 1);
return 0;
}
Compiles and runs, but prints: Items on stack: 0 ⏎
✅ Right
#include <stdio.h>
struct Stack { int items[5]; int top; };
void push(struct Stack *s, int x) {
s->items[++s->top] = x;
}
int main(void) {
struct Stack s = { .top = -1 };
push(&s, 7);
push(&s, 8);
printf("Items on stack: %d\n", s.top + 1);
return 0;
}
Passing the struct by value gives push a copy. The copy changes, then disappears. Pass a pointer (&s) so push changes the real stack.
❌ Wrong
#include <stdio.h>
#define CAP 3
int q[CAP], front = 0, rear = 0;
int enqueue(int x) {
if (rear == CAP) return 0; /* no wrap around */
q[rear++] = x;
return 1;
}
int main(void) {
enqueue(1); enqueue(2); enqueue(3);
front++; /* serve one: a place is free */
printf("%s\n", enqueue(4) ? "4 joined" : "Queue full!");
return 0;
}
Compiles and runs, but prints: Queue full! ⏎
✅ Right
#include <stdio.h>
#define CAP 3
int q[CAP], front = 0, count = 0;
int enqueue(int x) {
if (count == CAP) return 0;
q[(front + count) % CAP] = x; /* wraps around */
count++;
return 1;
}
int main(void) {
enqueue(1); enqueue(2); enqueue(3);
front = (front + 1) % CAP; /* serve one: a place is free */
count--;
printf("%s\n", enqueue(4) ? "4 joined" : "Queue full!");
return 0;
}
Without wrap-around, rear reaches the end and the queue says "full" even though the front place is free. Use % CAP so the indexes go round in a circle.
📌 Remember
- A stack is LIFO: push and pop happen at the top.
- A queue is FIFO: enqueue at the rear, dequeue at the front.
- Always check for full before push/enqueue and for empty before pop/dequeue.
- A circular queue wraps its indexes with % so freed places are reused.
- Linked-list stacks and queues grow with malloc and have no fixed size.
- Stacks power undo, Back buttons and bracket checks; queues power tickets and print jobs.
Finished the quiz and the challenge?
Questions students ask
What is the difference between a stack and a queue?
A stack takes items out in reverse order (last in, first out). A queue keeps the order (first in, first out). Both add and remove in constant time.
What is stack overflow and underflow?
Overflow means pushing onto a full stack; underflow means popping an empty one. In C nothing stops you, so you must check, or you read and write outside the array.
Does C have a built-in stack or queue?
No. The C standard library has no stack or queue type, so you write your own as in this lesson. C++ has std::stack and std::queue ready to use.
Where are stacks used in real programs?
Undo and redo in editors, the Back button in a browser, checking brackets in compilers, and the call stack that remembers where each function must return.