Stack operations
The stack ADT is defined by a small set of operations. All access happens at the top: you push to add, pop to remove, and often peek (or top) to read without removing. This page summarizes each operation, its preconditions, and typical failure cases—before you wire them up with an array or linked list in C.
- Core operations — push, pop, peek (top), and optional size
- Queries & safety — empty/full checks, overflow, underflow
1) Core operations
These are the operations every stack implementation must support in some form. Names vary (push / pop / peek / top), but the LIFO contract stays the same.
| Operation | What it does | Preconditions / notes | In this tutorial |
|---|---|---|---|
| Push | Inserts a new element at the top of the stack. The new item becomes the only one you can pop or peek next. | On a fixed-capacity array stack, pushing when full causes overflow—check isFull first or handle the error. |
Stack using array · Stack using linked list |
| Pop | Removes the element at the top and typically returns its value (or updates state so the next item is now on top). | Requires a non-empty stack. Popping when empty is underflow—always guard with isEmpty or equivalent in C. |
Stack using array · Stack using linked list |
| Peek / Top | Returns (or exposes) the top element without removing it. The stack’s size and order stay unchanged. | Same as pop for emptiness: calling peek on an empty stack is undefined unless you check isEmpty first. |
Stack using array · Stack using linked list |
| Size (optional) | Returns the number of elements currently in the stack. Helpful for debugging, bounds checks, and loop control. | Usually O(1) if you keep a count or top index; otherwise you may traverse (linked list without a counter). |
— |
2) Queries & error conditions
Robust C code tests state before pop and peek, and before push on bounded storage. Linked stacks rarely “overflow” from capacity (they can still fail if malloc fails).
| Query / condition | Purpose | Typical use in C | In this tutorial |
|---|---|---|---|
| isEmpty | True when there are no elements; pop and peek must not run. | Return 1 if top == -1 (array) or head == NULL (linked), before any top access. |
Stack using array |
| isFull | True when the stack cannot accept another push (fixed-size array implementation). | Compare top to capacity - 1 before push; dynamic arrays may resize instead of reporting full. |
Stack using array · Dynamic array stack |
| Overflow | Push when the structure is already at maximum capacity (static array). | Signal an error, return an error code, or grow storage (dynamic array / realloc pattern). | Stack using dynamic array |
| Underflow | Pop or peek when the stack is empty. | Avoid undefined behavior: check isEmpty, print a message, or return a sentinel / error code. |
Introduction to Stack |
| Initialize / create | Sets top pointer or index and (for arrays) capacity so the stack starts in a valid empty state. | top = -1 or top = 0 depending on convention; allocate array or list head to NULL. |
Stack using array · Stack using linked list |
Abstractly, a stack is fully described by push, pop, peek, and isEmpty; isFull appears when storage is bounded. Complexity for these is usually O(1) per operation on both array and linked implementations when implemented carefully.
Key takeaway
Treat empty before pop/peek and full before push (array) as non-negotiable invariants in C. Once the operations are clear, the next steps are concrete data layouts: array + top index, resizable buffer, or linked nodes at the top.
Next up: Stack using array · Stack using dynamic array · Stack using linked list · Stack time complexity · Stack applications