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

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy