Home » C Programs » C Program to Implement Stack Using Array

C Program to Implement Stack Using Array

Basic c program to implement stack using array. A stack is a group of elements of the same kind in a linear data structure. The Last In First Out (LIFO) method is used in Stack, which means that the last piece inserted is the first one to be pulled out. The insertion and deletion of elements in stacks occurs only at one endpoint.

Array to Implementation of Stack

An array is a collection of data elements of the same type stored in contiguous memory regions. 

Arrays are a derived data type in the C programming language that may hold primitive data types like int, char, double, float, and so on.  An array is a grouping of variables with the same data type. In an array, you can store a collection of data of the same kind. Any of the data types can contain an array. 

The size of the array must be constant. To store array elements in memory, contiguous (adjacent) memory regions are always used.

Push and Pop Definition in C

A stack is an abstract data type in computer science that serves as a collection of components. 

It has two major operations: push, which adds an element to the collection, and pop, which removes an element from the collection. Pop, which removes the most recently introduced but not yet removed piece. The push and pop operation were completely used here c program to implement stack using array.

For Loop Statement in C

A for loop is a control flow statement that allows code to be executed repeatedly by specifying iteration. When the number of iterations is known before entering the loop, for loops are commonly used. While loops that increment and test a loop variable can be thought of as short hands for for loops.

Push and Pop

PUSH: Pushes a new item into the stack. An overflow condition occurs when the stack is completely full. 

Stacks are a type of container adaptor that works on the LIFO (Last In First Out) principle, in which a new element is added at one end and (top) an element is removed from the opposite end. stack:: The function push() is used to push an element to the top of the stack.

POP: Removes an item from the stack by popping it. Items are popped in the opposite order that they were pushed. It’s called an Underflow condition when the stack is empty.

C Program to Implement Stack Using Array

//c program to implement stack using array
#include<stdio.h>
int stack_array[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{
top=-1;
printf("\n Enter the size of stack_array[MAX=100]:");
scanf("%d",&n);
printf("\n\t stack_array operations using array");
printf("\n\t--------------------------------");
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");
do
{
printf("\n Select your choice");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("\n\t Exit Point");
break;
}
default:
{
printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");
}

}
}
while(choice!=4);
return 0;
}
void push()
{
if(top>=n-1)
{
printf("\n\tstack_array is over flow");


}
else
{
printf(" Enter a value to be pushed");
scanf("%d",&x);
top++;
stack_array[top]=x;
}
}
void pop()
{
if(top<=-1)
{
printf("\n\t stack_array is under flow");
}
else
{
printf("\n\t The popped elements is %d",stack_array[top]);
top--;
}
}
void display()
{
if(top>=0)
{
printf("\n The elements in stack_array \n");
for(i=top; i>=0; i--)
printf("\n%d",stack_array[i]);
printf("\n Press Next Choice");
}
else
{
printf("\n The stack_array is empty");
}
}

Output 

Enter the size of stack_array[MAX=100]:45

stack_array operations using array
——————————–
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Select your choice