The simplest way to store a stack is an array plus one integer that remembers where the top is. This page gives the push and pop algorithms, a tested C implementation, and notes on the study material’s program.
The representation
An array items[0..MAXSTACK-1] holds the stack, with the bottom item in items[0]. An integer top holds the index of the top item :
empty stack: top == -1;
full stack: top == MAXSTACK - 1;
number of items: top + 1.
Intuition
top is like your finger on the last filled line of a numbered page. To add a line, move your finger down one line and write; to remove one, read the line under your finger and move your finger back up.
Some books number the array from 1, with top == 0 meaning empty and top == MAXSTACK meaning full; the algorithms in §4.3 use that convention. Both are correct as long as you use one consistently.
Algorithms
Definition 4.3 · PUSH(S, x)
If top == MAXSTACK - 1, report “stack overflow” and stop.
top = top + 1.
items[top] = x.
Definition 4.4 · POP(S)
If top == -1, report “stack underflow” and stop.
x = items[top].
top = top - 1.
Return x.
The order matters: push moves top before storing; pop reads before moving top. In C this is written compactly as items[++top] = x; and x = items[top--];.
Example 4.5 An array stack of capacity 3
Goal. Push A, B, C, D onto a stack of capacity 3, peek, then pop until underflow, printing the stack after each step.
Program. push and pop return 1 for success and 0 for overflow or underflow, so the caller can react.
C · stack_array.c Copy #include <stdio.h>
#define MAXSTACK 3 /* small, to show overflow quickly */
typedef struct {
char items[MAXSTACK];
int top; /* index of the top item; -1 = empty */
} Stack;
void init(Stack * s) { s->top = - 1 ; }
int is_empty( const Stack * s) { return s->top == - 1 ; }
int is_full( const Stack * s) { return s->top == MAXSTACK - 1 ; }
/* Push x. Returns 1 on success, 0 on overflow. O(1) */
int push(Stack * s, char x)
{
if (is_full(s))
return 0 ; /* overflow */
s->items[ ++ s->top] = x; /* first move top up, then store */
return 1 ;
}
/* Pop into *x. Returns 1 on success, 0 on underflow. O(1) */
int pop(Stack * s, char * x)
{
if (is_empty(s))
return 0 ; /* underflow */
* x = s->items[s->top -- ]; /* first read, then move top down */
return 1 ;
}
/* Read the top item without removing it. */
int peek( const Stack * s, char * x)
{
if (is_empty(s))
return 0 ;
* x = s->items[s->top];
return 1 ;
}
void show( const Stack * s)
{
printf( "bottom [" );
for ( int i = 0 ; i <= s->top; i ++ )
printf( " %c" , s->items[i]);
printf( " ] top (top = %d) \n " , s->top);
}
int main( void )
{
Stack s;
char c;
init( & s);
show( & s);
for ( char x = 'A' ; x <= 'D' ; x ++ ) {
if (push( & s, x))
printf( "push %c: " , x);
else
printf( "push %c: overflow! " , x);
show( & s);
}
if (peek( & s, & c))
printf( "peek: %c \n " , c);
while (pop( & s, & c)) {
printf( "pop -> %c: " , c);
show( & s);
}
printf( "pop: %s \n " , pop( & s, & c) ? "ok" : "underflow!" );
return 0 ;
}
Run it.
Output Copy bottom [ ] top (top = -1)
push A: bottom [ A ] top (top = 0)
push B: bottom [ A B ] top (top = 1)
push C: bottom [ A B C ] top (top = 2)
push D: overflow! bottom [ A B C ] top (top = 2)
peek: C
pop -> C: bottom [ A B ] top (top = 1)
pop -> B: bottom [ A ] top (top = 0)
pop -> A: bottom [ ] top (top = -1)
pop: underflow!
Result. D causes overflow because top is already MAXSTACK - 1 = 2; the items come out as C, B, A, and a fourth pop reports underflow.
Correction · Program 4.1 (array stack)
Study material says: Program 4.1 (§4.3.1) uses void main(); its menu() function calls itself again after every push, pop or show; and its variable top holds the index of the next free cell (starting at 0), while the algorithm of Figure 4.3 uses tos for the index of the top item .
Correct version: Use int main(void) and return 0, and run the menu in a loop (do { … } while (choice != 4);) instead of recursion. Either convention for top works, but state which one is used and keep the overflow and underflow tests consistent with it (next-free-cell: full when top == MAX, empty when top == 0; top-item: full when top == MAX - 1, empty when top == -1).
Why: C17 §5.1.2.2.1 defines main as returning int. Each recursive call to menu() adds a stack frame that is never released until the program ends, so a long session uses more and more memory for no reason. Mixing the two meanings of top is a common source of off-by-one errors.
Key idea
An array stack keeps its items in items[0..top]; push increments top then stores, pop reads then decrements, and both must check for overflow and underflow first.
TEE Exam note
For “write algorithms (or C functions) for push and pop using an array” write the declarations, both algorithms with the overflow/underflow check as step 1, and state the time complexity O ( 1 ) O(1) O ( 1 ) . State your convention for top (−1 or 0 for an empty stack) at the start; examiners accept either if it is consistent.
Common mistake
Storing before incrementing in push (items[top] = x; top++; with top starting at −1). The first push then writes to items[-1], outside the array.
Common mistake
Testing for overflow with top == MAXSTACK when top is the index of the top item. The last valid index is MAXSTACK - 1; with the wrong test, one push writes past the array.
Key takeaways
Array stack: items[] plus top, the index of the top item (-1 when empty).
Push: check overflow, top++, store. Pop: check underflow, read, top--.
All operations are O ( 1 ) O(1) O ( 1 ) ; the capacity is fixed when the array is declared.
Keep one convention for top and make every test match it.
Practice questions Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 1 · multiple choice An array stack uses top = -1 for empty and has MAXSTACK = 50. What is the value of top when the stack is full?
A 50 B 49 C 51 D 0
Check answer
Show answer Answer: B. 49
The items occupy indices 0 to 49, so the top item’s index is 49. 50 is the full condition for the “next free cell” convention.
Question 2 · multiple choice Which statement pair correctly pushes x when top is the index of the top item?
A top = top + 1; items[top] = x;B items[top] = x; top = top + 1;C items[top + 1] = x;D top = top - 1; items[top] = x;
Check answer
Show answer Answer: A. top = top + 1; items[top] = x;
Move to the next free cell first, then store. The second option overwrites the current top item (or writes to items[-1] on an empty stack); the third never updates top.
Question 3 · multiple choice What does this program print?
C · q4-array.c Copy #include <stdio.h>
int main( void )
{
int s[ 5 ], top = - 1 ;
s[ ++ top] = 4 ;
s[ ++ top] = 7 ;
s[ ++ top] = 9 ;
int a = s[top -- ];
s[ ++ top] = 2 ;
int b = s[top -- ];
int c = s[top -- ];
printf( "%d %d %d top=%d \n " , a, b, c, top);
return 0 ;
} A 9 7 4 top=-1 B 9 2 4 top=-1 C 9 2 7 top=0 D 4 7 9 top=2
Check answer
Show answer Answer: C. 9 2 7 top=0
After 4, 7, 9 are pushed, pop gives 9; push 2; pop gives 2; pop gives 7, leaving only 4 at index 0, so top is 0.
Question 4 · multiple choice In the study material’s algorithm, the stack array is indexed from 1 and tos = 0 means empty. Which test detects overflow?
A tos == MAXSTACK - 1B tos == -1C tos == 0D tos >= MAXSTACK
Check answer
Show answer Answer: D. tos >= MAXSTACK
With 1-based indexing the last cell is MAXSTACK, so the stack is full when tos reaches it. tos == MAXSTACK - 1 belongs to 0-based indexing.
Question 5 · multiple choice What is the main disadvantage of the array implementation of a stack?
Check answer
Show answer Answer: D. Its capacity is fixed in advance.
The array size is chosen at compile time (or allocation time), so space may be wasted or the stack may overflow. Push and pop are O ( 1 ) O(1) O ( 1 ) .
Question 6 · short answer
Write C functions for push and pop on an integer array stack of size MAX (with top = -1 for empty) that report overflow and underflow.
Show worked answer int stack[MAX], top = -1;
int push(int x) {
if (top == MAX - 1) { printf("Stack overflow\n"); return 0; }
stack[++top] = x; return 1;
}
int pop(int *x) {
if (top == -1) { printf("Stack underflow\n"); return 0; }
*x = stack[top--]; return 1;
}
Both functions are O ( 1 ) O(1) O ( 1 ) . Returning a success flag (and the popped value through a pointer) lets the caller tell an empty stack apart from a stored value such as 0 or −1.