Algorithm and Program of Stacks in C
Algorithm and Program of Stacks in C
Include the necessary header files like <stdio.h> for input/output functions and <stdbool.h> for
boolean data type.
Check if the stack is not full (i.e., top is less than MAX_SIZE - 1).
If the stack is not full, increment top and push the element onto the stack.
If the stack is not empty, return the top element from the stack and decrement top.
If the stack is not empty, return the top element without removing it.
Define an is_empty function that checks if the stack is empty by examining the value of top.
Return true if the stack is empty (i.e., top is -1), and false otherwise.
Step 8: Implement the size Function:
Define a size function that returns the current size of the stack by adding 1 to top.
C Program
#include
#include
// Step 1: Define a stack array and a top variable to keep track of the top element.
int stack[MAX_SIZE];
stack[++top] = element;
// Step 3: Implement the pop operation to remove and return elements from the stack.
int pop() {
if (top == -1) {
return stack[top--];
// Step 4: Implement the peek operation to view the top element without removing it.
int peek() {
if (top == -1) {
return stack[top];
bool is_empty() {
}
// Step 6: Implement a function to get the size of the stack.
int size() {
return top + 1;
int main() {
push(1);
push(2);
push(3);
printf("Stack: ");
printf("\n");
pop();
printf("\n");
return 0;
}
Algorithm for Multiple Stacks Implementation using a Single Array in C:
Include the necessary header files like <stdio.h> and <stdbool.h> for input/output functions and
boolean data types.
Define constants for the maximum size of the array (MAX_SIZE) and the number of stacks to be
implemented (NUM_STACKS).
Inside the Stack structure, declare an integer array to hold stack elements (stack) and an integer
variable to keep track of the top of the stack (top).
In this function, loop through each stack in the stacks array and set their top to -1.
Create a push function that takes two parameters: the stack number (stackNum) and the element
to be pushed onto the stack (element).
Check if the selected stack is not full (i.e., top is less than MAX_SIZE - 1).
If the stack is not full, increment the top for the selected stack and push the element onto the
stack.
Create a pop function that takes the stack number (stackNum) as a parameter.
If the stack is not empty, return and remove the top element from the selected stack.
Create a peek function that takes the stack number (stackNum) as a parameter.
If the stack is not empty, return the top element of the selected stack without removing it.
Create an is_empty function that takes the stack number (stackNum) as a parameter.
Return true if the selected stack is empty (i.e., top is -1), and false otherwise.
Create a size function that takes the stack number (stackNum) as a parameter.
Return the current size of the selected stack (i.e., top + 1).
#include <stdio.h>
#include <stdbool.h>
#define NUM_STACKS 3
typedef struct {
int stack[MAX_SIZE];
int top;
} Stack;
Stack stacks[NUM_STACKS];
void initializeStacks() {
stacks[i].top = -1;
}
// Push an element onto a specific stack.
return;
return;
currentStack->stack[++(currentStack->top)] = element;
return -1;
}
Stack *currentStack = &stacks[stackNum];
if (currentStack->top == -1) {
return -1;
return currentStack->stack[(currentStack->top)--];
return -1;
if (currentStack->top == -1) {
return -1;
return currentStack->stack[currentStack->top];
}
return false;
return -1;
return stacks[stackNum].top + 1;
int main() {
initializeStacks();
// Example usage:
printf("After pop from Stack 0, Top element = %d, Size = %d\n", peek(0), size(0));
return 0;