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)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | #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)
1 2 3 4 5 6 7 | 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)
1 2 3 4 5 6 7 | 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

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.