Lesson 31 of 50
cStacks (LIFO) Using Arrays
10 mins
Course progress62%
What you'll learn
Keep these ideas in mind as you work through the lesson.
Lesson 31 of 50
c10 mins
Keep these ideas in mind as you work through the lesson.
A stack follows last‑in‑first‑out order. Implemented easily with a fixed‑size array and an integer index.
#define MAX 100
int stack[MAX];
int top = -1; // empty stack
void push(int x) { if (top < MAX-1) stack[++top] = x; }
int pop(void) { if (top >= 0) return stack[top--]; return -1; }