Prg_4
Prg_4
STACK OPERATIONS
AIM :
To write a C Program to perform Stack operations using Arrays
ALGORITHM :
push(stack[MAX],element)
Step 1: Start
Step 2: If top = MAX-1
Display message ”Stack Full” and
Stop
Step 3: top = top + 1
Step 4: stack[top] = element
Step 5: Stop
pop(stack[MAX],element)
Step 1: Start
Step 2: If top = NULL
Display message ”Stack Empty” and
Stop
Step 3: Return stack[top] and set top = top - 1
Step 4: Stop
PROGRAM :
#include<stdio.h>
#include<conio.h>
int stack[100], choice, n, top, x, i;
void push(void);
void pop(void);
void display(void);
void main()
{
clrscr();
top=-1;
printf("\n Enter the size of STACK[MAX=100]:");
scanf("%d", &n);
do
{
clrscr();
printf("\n\n\t\t\tSTACK OPERATION USING ARRAY");
printf("\n\t\t\t--------------------------");
printf("\n\n\t\t\t 1.PUSH");
printf("\n\t\t\t 2.POP");
printf("\n\t\t\t 3.DISPLAY");
printf("\n\t\t\t 4.EXIT");
printf("\n\t\t\tENTER CHOICE :");
scanf("%d",&choice);
switch(choice)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: printf("\n\t\t\tEXIT POINT");
break;
default: printf("\n\t\t\tINVALID CHOICE");
}
} while(choice!=4);
}
void push()
{
if(top>=n-1)
{
printf("\n\n\t\t\tSTACK OVERFLOW");
}
else
{
printf("\n\t\t\tENTERT THE ELEMENT TO BE INSERTED : ");
scanf("%d",&x);
top++;
stack[top]=x;
printf("\n\t\t\tELEMENT INSERTED…");
getch();
}
}
void pop()
{
if(top<=-1)
{
printf("\n\t\t\tSTACK UNDERFLOW");
}
else
{
printf("\n\t\t\tDELETED ELEMENT : %d",stack[top]);
top--;
}
getch();
}
void display()
{
if(top>=0)
{
printf("\n\t\t\tELEMENTS IN STACK : \n");
for(i=0; i<=top; i++)
printf("\n\t\t\t%d ",stack[i]);
}
else
{
printf("\n\t\t\tSTACK IS EMPTY");
}
getch();
}
OUTPUT :