C Program to Implement Stack Using Array

Introduction

A stack is a linear data structure that follows the Last In, First Out (LIFO) principle. It means that the last element inserted into the stack will be the first one to be removed. The primary operations on a stack are push (to insert an element), pop (to remove an element), and peek or top (to view the top element of the stack without removing it). An array can be used to implement a stack where one end of the array is used as the top of the stack.

Example:

  • Push Operation: Adding elements [10, 20, 30] to the stack results in 30 being at the top.
  • Pop Operation: Removing the top element (30) from the stack leaves 20 at the top.

Problem Statement

Create a C program that:

  • Implements a stack using an array.
  • Performs stack operations such as push, pop, and peek.
  • Displays the stack contents after each operation.

Solution Steps

  1. Include the Standard Libraries: Use #include <stdio.h> and #include <stdlib.h> for standard input-output functions.
  2. Define Constants and Global Variables: Define the maximum size of the stack and the stack array along with a variable to keep track of the top of the stack.
  3. Implement the Stack Operations:
  • Push: Add an element to the top of the stack.
  • Pop: Remove and return the top element from the stack.
  • Peek/Top: Return the top element without removing it.
  • IsEmpty: Check if the stack is empty.
  • IsFull: Check if the stack is full.
  1. Create a Main Function: In the main() function, allow the user to perform stack operations interactively.

C Program to Implement Stack Using Array

#include <stdio.h> #include <stdlib.h> // Step 2: Define Constants and Global Variables #define MAX 100 // Maximum size of the stack int stack[MAX]; int top = -1; // Initialize top of the stack as -1 indicating an empty stack // Function to check if the stack is empty int isEmpty() { return top == -1; } // Function to check if the stack is full int isFull() { return top == MAX - 1; } // Function to push an element onto the stack void push(int value) { if (isFull()) { printf("Stack overflow! Cannot push %d onto the stack.\n", value); return; } stack[++top] = value; // Increment top and then add the element printf("%d pushed onto the stack.\n", value); } // Function to pop an element from the stack int pop() { if (isEmpty()) { printf("Stack underflow! Cannot pop from an empty stack.\n"); return -1; // Return -1 to indicate stack underflow } return stack[top--]; // Return the top element and then decrement top } // Function to peek the top element of the stack int peek() { if (isEmpty()) { printf("Stack is empty! Cannot peek.\n"); return -1; // Return -1 to indicate an empty stack } return stack[top]; } // Function to display the stack elements void display() { if (isEmpty()) { printf("Stack is empty!\n"); return; } printf("Stack elements: "); for (int i = top; i >= 0; i--) { printf("%d ", stack[i]); } printf("\n"); } int main() { int choice, value; while (1) { // Step 4: Provide a Menu for Stack Operations printf("\nStack Operations Menu:\n"); printf("1. Push\n"); printf("2. Pop\n"); printf("3. Peek\n"); printf("4. Display\n"); printf("5. Exit\n"); printf("Enter your choice: "); scanf("%d", &choice); switch (choice) { case 1: printf("Enter value to push onto the stack: "); scanf("%d", &value); push(value); break; case 2: value = pop(); if (value != -1) { printf("Popped value: %d\n", value); } break; case 3: value = peek(); if (value != -1) { printf("Top value: %d\n", value); } break; case 4: display(); break; case 5: exit(0); default: printf("Invalid choice! Please enter a valid option.\n"); } } return 0; // Return 0 to indicate successful execution } 

Explanation

Step 2: Define Constants and Global Variables

  • The maximum size of the stack (MAX) is defined as 100.
  • The stack is implemented as an array stack[MAX], and the variable top is initialized to -1 to indicate that the stack is empty.

Function to Check if the Stack is Empty

  • The isEmpty function returns 1 if top is -1, indicating the stack is empty, otherwise returns 0.

Function to Check if the Stack is Full

  • The isFull function returns 1 if top is equal to MAX - 1, indicating the stack is full, otherwise returns 0.

Function to Push an Element onto the Stack

  • The push function adds an element to the top of the stack if the stack is not full. It increments top and then stores the element at stack[top].

Function to Pop an Element from the Stack

  • The pop function removes and returns the top element from the stack if the stack is not empty. It returns stack[top] and then decrements top.

Function to Peek the Top Element of the Stack

  • The peek function returns the top element of the stack without removing it. It is useful for viewing the element at the top of the stack.

Function to Display the Stack Elements

  • The display function prints all the elements in the stack from top to bottom.

Main Function

  • The main function provides a menu-driven interface allowing the user to perform various stack operations interactively.

Output Example

Example Output:

Stack Operations Menu: 1. Push 2. Pop 3. Peek 4. Display 5. Exit Enter your choice: 1 Enter value to push onto the stack: 10 10 pushed onto the stack. Stack Operations Menu: 1. Push 2. Pop 3. Peek 4. Display 5. Exit Enter your choice: 1 Enter value to push onto the stack: 20 20 pushed onto the stack. Stack Operations Menu: 1. Push 2. Pop 3. Peek 4. Display 5. Exit Enter your choice: 4 Stack elements: 20 10 Stack Operations Menu: 1. Push 2. Pop 3. Peek 4. Display 5. Exit Enter your choice: 2 Popped value: 20 

Conclusion

This C program demonstrates how to implement a stack using an array. It includes basic stack operations such as push, pop, peek, and display, making it a useful example for beginners learning about data structures in C programming.

Leave a Comment

Scroll to Top