A stack is a linear data structure that follows the Last In First Out (LIFO) principle. The last element inserted into the stack is the first one to be removed. A stack supports two primary operations:
- Push: Add an element to the top of the stack.
- Pop: Remove the element from the top of the stack.
In this program, we will implement a stack using arrays. We will also implement helper functions to check if the stack is empty or full, peek at the top element, and display all the elements in the stack.
Program Explanation
We will use an array to represent the stack and an integer variable top to keep track of the index of the top element in the stack. The stack operations follow these rules:
- Push: Add an element at the
top
of the stack and increment thetop
pointer. - Pop: Remove an element from the
top
of the stack and decrement thetop
pointer. - The stack is considered full if the
top
pointer reaches the maximum size of the stack. It is considered empty if thetop
pointer is -1.
C Program Code
#include <stdio.h>
#include <stdlib.h>
#define MAX 5 // Define the maximum size of the stack
// Stack structure
struct Stack {
int arr[MAX];
int top;
};
// Initialize the stack
void initStack(struct Stack *s) {
s->top = -1;
}
// Check if the stack is full
int isFull(struct Stack *s) {
return s->top == MAX - 1;
}
// Check if the stack is empty
int isEmpty(struct Stack *s) {
return s->top == -1;
}
// Push an element onto the stack
void push(struct Stack *s, int value) {
if (isFull(s)) {
printf("Stack is full. Cannot push %d.\n", value);
return;
}
s->arr[++(s->top)] = value;
printf("Pushed %d\n", value);
}
// Pop an element from the stack
int pop(struct Stack *s) {
if (isEmpty(s)) {
printf("Stack is empty. Cannot pop.\n");
return -1;
}
return s->arr[(s->top)--];
}
// Peek at the top element of the stack
int peek(struct Stack *s) {
if (isEmpty(s)) {
printf("Stack is empty.\n");
return -1;
}
return s->arr[s->top];
}
// Display the elements of the stack
void displayStack(struct Stack *s) {
if (isEmpty(s)) {
printf("Stack is empty.\n");
return;
}
printf("Stack elements: ");
for (int i = 0; i <= s->top; i++) {
printf("%d ", s->arr[i]);
}
printf("\n");
}
// Main driver function
int main() {
struct Stack s;
initStack(&s);
push(&s, 10); // Push 10
push(&s, 20); // Push 20
push(&s, 30); // Push 30
push(&s, 40); // Push 40
push(&s, 50); // Push 50 (stack is full now)
displayStack(&s); // Display the stack
printf("Top element: %d\n", peek(&s)); // Peek at the top element
pop(&s); // Pop the top element
pop(&s); // Pop another element
displayStack(&s); // Display the stack again
push(&s, 60); // Push 60
displayStack(&s); // Display the stack after pushing 60
return 0;
}
Code Explanation
1. Stack Structure: The stack is represented as an array of fixed size MAX
, and the top
pointer keeps track of the index of the top element in the stack. The stack is initialized by setting top
to -1, indicating that the stack is empty.
2. Push Operation: The function push()
inserts elements at the top
of the stack. If the stack is full (i.e., top == MAX - 1
), new elements cannot be added.
3. Pop Operation: The function pop()
removes elements from the top
of the stack. If the stack is empty (i.e., top == -1
), no elements can be removed.
4. Peek Operation: The function peek()
returns the value of the top element without removing it.
5. Display Operation: The function displayStack()
prints the elements of the stack from the bottom to the top.
Sample Output
Pushed 10
Pushed 20
Pushed 30
Pushed 40
Pushed 50
Stack elements: 10 20 30 40 50
Top element: 50
Popped 50
Popped 40
Stack elements: 10 20 30
Pushed 60
Stack elements: 10 20 30 60
Conclusion
This C program demonstrates how to implement a stack using arrays. The stack operates using the Last In First Out (LIFO) principle and supports operations such as push, pop, peek, and display. The implementation is straightforward and allows easy manipulation of stack elements using array indexing.