0% found this document useful (0 votes)
34 views5 pages

Q 1. Write A Program To Implementation of The Stack Using Array

This document contains a C program to implement a stack using an array. The program defines functions for push(), pop(), and display() operations on the stack. It uses an array and a top index variable to track the stack. The main() function contains a do-while loop that prompts the user for push, pop, or quit choices and calls the corresponding functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views5 pages

Q 1. Write A Program To Implementation of The Stack Using Array

This document contains a C program to implement a stack using an array. The program defines functions for push(), pop(), and display() operations on the stack. It uses an array and a top index variable to track the stack. The main() function contains a do-while loop that prompts the user for push, pop, or quit choices and calls the corresponding functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Q 1. Write a program to implementation of the stack using array.

Algorithm
push( s[], d)
if top==size-1 then
flag0
else
flag1
++top
s[top]d
pop( s[])
popped_element;
if top==-1 then
popped_element0
flag0
else
flag1
popped_elements[top]
--top
return(popped_element);

Program
#include<stdio.h>
#include<conio.h>
#define size 20
int stack[size];
int top=-1;
int flag=0;
void push(int *,int);
int pop(int *);
void display(int *);
void main()
{
int q=0,data;
char choice;
int top=-1;
clrscr();
do
{

printf("\n\npush->i\npop->p\nquit->q");
printf("\n\nENTER the choice ->");
do
{
choice=getchar();
choice=tolower(choice);
}
while(strchr("ipq",choice)==NULL);
printf("\n\nYOUR CHOICE IS %c",choice);
switch(choice)
{
case'i':
printf("\n\nInput the element to push: ");
scanf("%d",&data);
push(stack,data);
if(flag)
{
printf("\n\nAfter inserting");
display(stack);
if(top==size-1)
printf("\n\nStack is full");
}
else
printf("\n\nStack is overflow after pushing");
break;
case'p':
data=pop(stack);
if(flag)
{
printf("\n\nData is popped:%d",data);
printf("\n\nRest data in stack is as follows");
display(stack);
}
else
printf("\n\nStack underflow");
break;
case'q':
q=1;
}
}
while(!q);
getch();
}
void push(int s[],int d)
{
if(top==size-1)
{
flag=0;
}
else
{
flag=1;
++top;
s[top]=d;
}
}
int pop(int s[])
{
int popped_element;
if(top==-1)
{
popped_element=0;
flag=0;
}
else
{
flag=1;
popped_element=s[top];
--top;
}
return(popped_element);
}
void display(int s[])
{
int i;
if(top==-1)
{
printf("\n\nstack is empty");
}
else
{
for(i=top;i>=0;i--)
printf("\n%d",s[i]);
}
}

OUTPUT

You might also like