Stack Data Structure C Program

By | November 27, 2015

C Program For Stack Data Structure using Array

Learn Implementation of Stack Data Structure in C Programming Language. This Code For Stack in Data Structure using C Programming is based on Array Implementation. The Stack Data Structure can be either accomplished through Linked Lists or Arrays. The program below is a Static Implementation of Stack using Array in C Programming along with a complete explanation.

All about Stack Data Structures

A Stack is a Data Structure that stores in elements within it. It works on LIFO Principle. LIFO is an abbreviated form of Last In First Out. In Stacks, Data Elements are Inserted and Deleted from the same end which is generally referred as TOP. The TOP is a reference pointer that enables data processing operations such as INSERT, DELETE, PEEK. A Stack is an example of Linear Data Structure in which all the elements are arranged in a linear sequence.

Here, we have written codes to Insert, Delete, Display and Peek Operations in a stack. A Stack can also be implemented using Linked Lists in C Programming Language.

Also Read: List of 100 C Programs For Interviews

C Program For Stack Data Structure using Array (Static Array)

#include<stdio.h>
#include<stdlib.h>

#define MAX 10
int top = -1;
int stack_array[MAX];

int pop();
int peek();
int isEmpty();
int isFull();
void display();
int element;
void push(int element);

int main()
{
    int option,element;
    while(1)
    {
        printf("\n1. Push / Insert An Element in Stack");
        printf("\n2. Pop / Delete An Element From Stack");
        printf("\n3. Display All Contents of Stack");
        printf("\n4. Display Top-Most Content of Stack");
        printf("\n5. Exit\n");
        printf("\nSelect an Option:\t");
        scanf("%d", &option);
        switch(option)
        {
            case 1: 
                printf("\nEnter The Element To Be Inserted:\t");
                scanf("%d", &element);
                push(element);
                break;
            
            case 2: 
                element = pop();
                printf("Element Deleted:\t%d",element);
                break;

            case 3: 
                display();
                break;

            case 4: 
                printf("Top-Most Element of Stack:\t%d",peek());
                break;

            case 5: exit(1);
        }
    }
    return 0;
}

int pop()
{
    if(isEmpty())
    {   
        printf("\nStack Undeflow\n");
        exit(1);
    }   
    else
    {
        element = stack_array[top];
        top = top - 1;
        return element;
    }
}

int peek()
{
    if(isEmpty())
    {
        printf("\nStack Undeflow\n");
        return 1;
    }
    else
    {
        return stack_array[top];
    }
}

int isEmpty()
{
    if(top == -1)
        return 1;
    else
        return 0;
}

int isFull()
{
    if(top == MAX - 1)
        return 1;
    else
        return 0;
}

void display()
{
    int count;
    if(isEmpty())
    {
        printf("\nStack is Empty\n");
        return;
    }
    else
    {
        for(count = top; count >= 0; count--)
        {
            printf("%d\t",stack_array[count]);
        }
    }
}

void push(int element)
{
    if(isFull())
    {
        printf("\nStack Overflow\n");
        return;
    }
    else
    {
        top = top + 1;
        stack_array[top] = element;
    }
}

Condition To Check If Stack is Empty (Stack Underflow Condition)

int isEmpty()
{
    if(top == -1)
        return 1;
    else
        return 0;
}

Also Read: List of Data Structures Programs in C Programming

 

Condition To Check If Stack is Full (Stack Overflow Condition)

int isFull()
{
    if(top == MAX - 1)
        return 1;
    else
            return 0;
}

Application of Stack Data Structure in Real World

  • Evaluation of an Expression or Mathematical Expression
  • Depth First Search Algorithm
  • Processor Scheduling Algorithms
  • Conversion of an Infix Expression To Postfix Expression

Output

C Program For Stack Data Structure using Static Arrays

In case you have any Doubts or Compilation Errors in this C Program For Stack Data Structure using Static Arrays, mention about it in the Comment Section.

Let's Discuss