Written by Sai Sandeep Thota on January 14, 2013 in C > Programming

C Program to Implement Stack using Arrays

GET ALERTS:

Get our Latest updates delivered to your mailbox!

A Stack can be defined as a list of items in which additions can be done from only one direction (usually known as top). Stacks can be implemented in two ways using C. First one is Arrays and the second is Linked List. In a Stack a item which is usually pushed first is popped out at the last. That means Last in First Out[LIFO] or First in Last Out[FILO]. Stack has two functions, They are Push and Pop.

Push is used to insert elements in a list, whereas Pop is used to remove elements from the list. We have declared two global variables stack and top and assigned max with a value of 50 entries. Stack variable is used to populate the elements of the stack and top is used to check the status of the Stack (Empty/Full).

Push Function
1. We do insertions if the Stack is having some free space, so for doing insertions we check the condition top==max that means if top of the Stack is full we need to display Overflow.
2. If the above condition fails, then insertions can be done. So user will give an input which is populated into the stack and Top position of Stack is incremented. If we are inserting an element for the first time it will be stored in the position 0 as we have declared top=0.

Pop Function
1. We can remove an element/item form Stack iff there are some elements in the Stack. If there are no elements we cannot remove, so for checking this we used the condition top==-1, that means if the stack position is -1 then we will not be able to perform pop operation.
2. If the above condition fails then, We can perform the Pop operation by decrementing the Top position of Stack.

C Program to implement Stack using Arrays

#include<conio.h>
#include<stdio.h>
#define max 50
void push();
void pop();
void display();
int menu();
int stack[max], top=0;

void main()
{
	int ch;
	clrscr();
	do{
		ch=menu();
		switch(ch)
		{
			case 1: push();
				break;
			case 2: pop();
				break;
			case 3: display();
				break;
			case 4: exit();
			default: printf("\nEnter a valid choice!!");
		}
	}while(1);
}

int menu()
{
	int ch;
	printf("\nStack");
	printf("\n1.Push\n2.Pop\n3.Display\n4.Exit");
	printf("\nEnter your Choice:");
	scanf("%d",&ch);
	return ch;
}

void push()
{
	if(top==max)
		printf("\nOverflow");
	else
	{
		int element;
		printf("\nEnter Element:");
		scanf("%d",&element);
		printf("\nElement(%d) has been pushed at %d", element, top);
		stack[top++]=element;
	}
}

void pop()
{
	if(top==-1)
		printf("\nUnderflow");
	else
	{
		top--;
		printf("\nELement has been popped out!");
	}
}

void display()
{
	if(top==0)
		printf("\nStack is Empty!!");
	else
	{
		int i;
		for(i=0;i<max;i++)
			printf("%d",stack[i]);
	}
}

Output:
Stack using Arrays C Program

{ 1 comment… read it below or add one }

Nikhil November 25, 2016 at 3:45 pm

In start of program it looks like that we should initialize top=-1 not with 0 because it might make issue in pop() function while checking for an element.

Reply

Leave a Comment

Previous post:

Next post: