The stack is a linear data structure following the last-in-first-out (LIFO) order, meaning the most recently added element is the first to be removed. This tutorial will implement a basic stack in C using an array. We will cover the two primary operations performed on a stack: pushing an element (adding to the stack) and popping an element (removing from the stack).
Step 1: Define Constants and Variables
First, we need to define the maximum size of the stack and declare an array to hold the stack elements. The stack is represented by an array named stack
with a fixed size, defined by the constant MAX
. We also need an integer variable top
to keep track of the index of the topmost element in the stack.
Example:
#define MAX 10
int stack[MAX];
int top = -1;
In the above code, the top
variable is initialized to -1
to indicate that the stack is empty. When the top
is set to -1
, it means there are no elements in the stack, and the first element has not been added yet.
When you push the first element onto the stack, the top
is incremented using the pre-increment operator (++top
). Therefore, when the first element is added, the top
will become 0
, pointing to the first position in the array.
Step 2: Implement the push Function
The push()
function adds an element to the top of the stack. Before pushing the element, we need to check if the stack is full. If the stack is full (top == MAX - 1
), we display an error message. Otherwise, we increment the top
and add the element to the stack.
Example:
void push(int value) {
if (top == MAX - 1) {
printf("Error: Stack overflow!\n");
return;
}
stack[++top] = value;
}
The condition top == MAX - 1
checks if the stack is full. Since array indices start at 0, the maximum index in the array is MAX - 1
. If top
is equal to MAX - 1
, it means that the stack is at its maximum capacity, and no more elements can be added (pushed) onto the stack. In this case, a stack overflow error message is displayed to inform the user that the stack is full.
The stack[++top] = value;
increments the top
index and stores the new element (the value
variable) at the updated position in the stack
array.
Step 3: Implement the pop Function
The pop()
function removes the top element from the stack and returns it. Before popping the element, we must check if the stack is empty. If the stack is empty (top == -1
), we display an error message. Otherwise, we return the top element and decrement the top
.
int pop() {
if (top == -1) {
printf("Error: Stack underflow!\n");
return -1;
}
return stack[top--];
}
Step 4: Implement the main Function
The main()
function provides a simple menu-driven interface for users to interact with the stack. The user can choose between pushing an element, popping an element, or exiting the program. Error messages are displayed in case of a stack overflow or stack underflow.
Example:
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
int stack[MAX];
int top = -1;
void push(int value) {
if (top == MAX - 1) {
printf("Error: Stack overflow!\n");
return;
}
stack[++top] = value;
}
int pop() {
if (top == -1) {
printf("Error: Stack underflow!\n");
return -1;
}
return stack[top--];
}
int main() {
int choice, value;
while (1) {
printf("1. Push\n2. Pop\n3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to push: ");
scanf("%d", &value);
push(value);
break;
case 2:
value = pop();
if (value != -1) {
printf("Popped value: %d\n", value);
}
break;
case 3:
exit(0);
default:
printf("Invalid choice!\n");
}
}
return 0;
}
Conclusion
In this tutorial, we implemented a basic stack in C using an array. We covered the two primary operations, push and pop, and provided a simple interface for users to interact with the stack. This stack implementation is a foundation for understanding the basic concepts of stack implementation in C and can be further extended or modified as needed.