0% found this document useful (0 votes)
2 views

Prg_4

The document outlines a C program that implements stack operations using arrays, including push, pop, and display functionalities. It provides an algorithm for each operation and includes the complete source code for the program. The program allows users to interactively perform stack operations until they choose to exit.

Uploaded by

darkface00505
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Prg_4

The document outlines a C program that implements stack operations using arrays, including push, pop, and display functionalities. It provides an algorithm for each operation and includes the complete source code for the program. The program allows users to interactively perform stack operations until they choose to exit.

Uploaded by

darkface00505
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Sss

STACK OPERATIONS

4. Write a C Program to perform Stack operations using Arrays

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 :

You might also like