codegurukool.com · Free coding tutorials & online compiler

Home › C Tutorial › C stacks and queues (DSA)

C stacks and queues (DSA)

Last-in first-out and first-in first-out.

A stack pushes and pops at one end (LIFO). A queue adds at the back and removes from the front (FIFO). Both fit in an array.

Example code

#include <stdio.h>
int main() {
  int s[10], top = -1;
  s[++top] = 1; s[++top] = 2; s[++top] = 3;
  printf("pop %d\n", s[top--]);
  int q[10], f = 0, r = 0;
  q[r++] = 1; q[r++] = 2;
  printf("dequeue %d\n", q[f++]);
  return 0;
}

Run this code in the online editor

← C sorting (DSA)C linked lists (DSA) →