0% found this document useful (0 votes)
31 views

Algorithm and Program of Stacks in C

The document describes an algorithm for implementing multiple stacks using a single array in C. It involves dividing the array into sections, with each section representing a separate stack. Functions are implemented for initializing the stacks, pushing/popping elements, peeking at top elements, and checking stack sizes and empty status.

Uploaded by

Shiv
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Algorithm and Program of Stacks in C

The document describes an algorithm for implementing multiple stacks using a single array in C. It involves dividing the array into sections, with each section representing a separate stack. Functions are implemented for initializing the stacks, pushing/popping elements, peeking at top elements, and checking stack sizes and empty status.

Uploaded by

Shiv
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Algorithm for Basic Stack Operations in C:

Step 1: Include Necessary Headers

Include the necessary header files like <stdio.h> for input/output functions and <stdbool.h> for
boolean data type.

Step 2: Define Constants:

Define a constant MAX_SIZE to specify the maximum size of the stack.

Step 3: Declare Stack and Top:

Declare an integer array stack to represent the stack.

Initialize an integer variable top to -1, indicating an empty stack.

Step 4: Implement the push Operation:

Define a push function that takes an integer element as a parameter.

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.

Step 5: Implement the pop Operation:

Define a pop function.

Check if the stack is not empty (i.e., top is not -1).

If the stack is not empty, return the top element from the stack and decrement top.

Step 6: Implement the peek Operation:

Define a peek function.

Check if the stack is not empty (i.e., top is not -1).

If the stack is not empty, return the top element without removing it.

Step 7: Implement the is_empty Function:

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.

Step 9: Main Function:

In the main function, demonstrate the usage of the stack operations:

Push some elements onto the stack.

Print the stack elements.

Peek at the top element.

Print the stack size.

Pop an element from the stack.

Print the stack after the pop operation.

Step 10: Exit Program:

Return 0 to indicate successful program execution.

C Program

#include

#include

#define MAX_SIZE 100

// Step 1: Define a stack array and a top variable to keep track of the top element.

int stack[MAX_SIZE];

int top = -1;

// Step 2: Implement the push operation to add elements to the stack.

void push(int element) {

if (top >= MAX_SIZE - 1) {

printf("Stack overflow. Cannot push.\n");


return;

stack[++top] = element;

// Step 3: Implement the pop operation to remove and return elements from the stack.

int pop() {

if (top == -1) {

printf("Stack is empty. Cannot pop.\n");

return -1; // Return a sentinel value to indicate an empty stack

return stack[top--];

// Step 4: Implement the peek operation to view the top element without removing it.

int peek() {

if (top == -1) {

printf("Stack is empty. Cannot peek.\n");

return -1; // Return a sentinel value to indicate an empty stack

return stack[top];

// Step 5: Implement a function to check if the stack is empty.

bool is_empty() {

return top == -1;

}
// Step 6: Implement a function to get the size of the stack.

int size() {

return top + 1;

int main() {

// Step 7: Example usage:

push(1);

push(2);

push(3);

printf("Stack: ");

for (int i = 0; i <= top; i++) {

printf("%d ", stack[i]);

printf("\n");

printf("Top element: %d\n", peek());

printf("Stack size: %d\n", size());

pop();

printf("After pop, Stack: ");

for (int i = 0; i <= top; i++) {

printf("%d ", stack[i]);

printf("\n");

return 0;

}
Algorithm for Multiple Stacks Implementation using a Single Array in C:

Step 1: Include Necessary Headers:

Include the necessary header files like <stdio.h> and <stdbool.h> for input/output functions and
boolean data types.

Step 2: Define Constants:

Define constants for the maximum size of the array (MAX_SIZE) and the number of stacks to be
implemented (NUM_STACKS).

Step 3: Define a Stack Structure:

Define a structure named Stack to represent each stack.

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).

Step 4: Initialize an Array of Stacks:

Declare an array of Stack structures (stacks) with a size of NUM_STACKS.

Initialize each stack by setting its top to -1 to indicate an empty stack.

Step 5: Implement Initialization Function:

Create a function called initializeStacks to initialize all the stacks.

In this function, loop through each stack in the stacks array and set their top to -1.

Step 6: Implement Push Operation:

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 stackNum is valid (between 0 and NUM_STACKS - 1).

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.

Step 7: Implement Pop Operation:

Create a pop function that takes the stack number (stackNum) as a parameter.

Check if the stackNum is valid.


Check if the selected stack is not empty (i.e., top is not -1).

If the stack is not empty, return and remove the top element from the selected stack.

Step 8: Implement Peek Operation:

Create a peek function that takes the stack number (stackNum) as a parameter.

Check if the stackNum is valid.

Check if the selected stack is not empty.

If the stack is not empty, return the top element of the selected stack without removing it.

Step 9: Implement Is Empty Function:

Create an is_empty function that takes the stack number (stackNum) as a parameter.

Check if the stackNum is valid.

Return true if the selected stack is empty (i.e., top is -1), and false otherwise.

Step 9: Implement Size Function:

Create a size function that takes the stack number (stackNum) as a parameter.

Check if the stackNum is valid.

Return the current size of the selected stack (i.e., top + 1).

Step 10 : Main Function:

In the main function:

Initialize the stacks using initializeStacks.

Demonstrate the usage of stack operations:

Push elements onto different stacks.

Print the top element and size of each stack.

Pop elements from a stack.

Print the updated top element and size of the stack.

Step 11: Exit Program:

Return 0 to indicate successful program execution.


Implementing multiple stacks using a single array involves dividing the array into multiple
sections, each representing a separate stack. You'll also need to keep track of the top elements
and sizes of each stack. Here's a C program that demonstrates the implementation of multiple
stacks using a single array:

#include <stdio.h>

#include <stdbool.h>

#define MAX_SIZE 100

#define NUM_STACKS 3

// Define a structure to represent each stack.

typedef struct {

int stack[MAX_SIZE];

int top;

} Stack;

// Initialize an array of Stack structures.

Stack stacks[NUM_STACKS];

// Initialize the stacks.

void initializeStacks() {

for (int i = 0; i < NUM_STACKS; i++) {

stacks[i].top = -1;

}
// Push an element onto a specific stack.

void push(int stackNum, int element) {

if (stackNum < 0 || stackNum >= NUM_STACKS) {

printf("Invalid stack number.\n");

return;

Stack *currentStack = &stacks[stackNum];

if (currentStack->top >= MAX_SIZE - 1) {

printf("Stack %d overflow. Cannot push.\n", stackNum);

return;

currentStack->stack[++(currentStack->top)] = element;

// Pop an element from a specific stack.

int pop(int stackNum) {

if (stackNum < 0 || stackNum >= NUM_STACKS) {

printf("Invalid stack number.\n");

return -1;

}
Stack *currentStack = &stacks[stackNum];

if (currentStack->top == -1) {

printf("Stack %d is empty. Cannot pop.\n", stackNum);

return -1;

return currentStack->stack[(currentStack->top)--];

// Peek at the top element of a specific stack.

int peek(int stackNum) {

if (stackNum < 0 || stackNum >= NUM_STACKS) {

printf("Invalid stack number.\n");

return -1;

Stack *currentStack = &stacks[stackNum];

if (currentStack->top == -1) {

printf("Stack %d is empty. Cannot peek.\n", stackNum);

return -1;

return currentStack->stack[currentStack->top];
}

// Check if a specific stack is empty.

bool is_empty(int stackNum) {

if (stackNum < 0 || stackNum >= NUM_STACKS) {

printf("Invalid stack number.\n");

return false;

return stacks[stackNum].top == -1;

// Get the size of a specific stack.

int size(int stackNum) {

if (stackNum < 0 || stackNum >= NUM_STACKS) {

printf("Invalid stack number.\n");

return -1;

return stacks[stackNum].top + 1;

int main() {

// Initialize the stacks.

initializeStacks();
// Example usage:

push(0, 1); // Push to stack 0

push(1, 2); // Push to stack 1

push(0, 3); // Push to stack 0

printf("Stack 0: Top element = %d, Size = %d\n", peek(0), size(0));

printf("Stack 1: Top element = %d, Size = %d\n", peek(1), size(1));

pop(0); // Pop from stack 0

printf("After pop from Stack 0, Top element = %d, Size = %d\n", peek(0), size(0));

return 0;

You might also like