0% found this document useful (0 votes)
2 views3 pages

Stack Arr

This document contains a C program that implements a stack using an array. It provides functionalities such as push, pop, display, and peek, along with checks for stack emptiness and fullness. The program allows user interaction to perform these operations until the user chooses to exit.

Uploaded by

mdafzal.aiml
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 views3 pages

Stack Arr

This document contains a C program that implements a stack using an array. It provides functionalities such as push, pop, display, and peek, along with checks for stack emptiness and fullness. The program allows user interaction to perform these operations until the user chooses to exit.

Uploaded by

mdafzal.aiml
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/ 3

#include<stdio.

h>
#include<stdbool.h>
int stack[100],i,j,choice=0,max,top=‐1;

//function declarations
void push();
void pop();
void display();
void peek();
bool isEmpty();
bool isFull();

//Driver code
int main(void)
{
printf("Enter the number of elements in the stack: ");
scanf("%d",&max);
printf("*********Stack Implementation Using Array*********");
printf("\n‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐");
while(choice != 5)
{
printf("\nChose one from the below options...");
printf("\n1.Push");
printf("\n2.Pop");
printf("\n3.Display");
printf("\n4.Peek");
printf("\n5.Exit");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
peek();
break;
case 5:
printf("Exiting....");
break;
default:
printf("Invalid Choice...:(\n");
};
}
}

//Checking the emptiness of the stack


bool isEmpty()
{
if(top==‐1)
return true;
else
return false;
}

//Checking the fullness of the stack


bool isFull()
{
if(top==max‐1)
return true;
else
return false;
}

//Pushing an item onto the stack


void push()
{
int item;
if (isFull())
printf("\nOverflow...:(\n");
else
{
printf("Enter the value: ");
scanf("%d",&item);
top = top+1;
stack[top] = item;
printf("\n%d pushed successfully onto the stack", item);
}
}

//Poping an item from the stack


void pop()
{
if(isEmpty())
printf("\nUnderflow...:(\n");
else
{
printf("\n%d poped successfully from the stack", stack[top]);
top = top‐1;
}
}

//Displaying the items from the stack


void display()
{
if(isEmpty())
printf("\nStack is Empty...:(\n");
else
for (i=top;i>=0;i‐‐)
printf("%d\n",stack[i]);
}

//Peeking the item from top of the stack


void peek()
{
if(isEmpty())
printf("\nStack is Empty...:(\n");
else
printf("Item at top of the stack is: %d", stack[top]);
}

You might also like