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

stack-programC--

This document contains a C++ implementation of a stack data structure with functions for pushing, popping, checking if the stack is full or empty, and printing stack elements. The main function provides a user interface to interact with the stack, allowing users to insert, delete, and view elements. The stack has a maximum size of 10 and uses an array for storage.

Uploaded by

kkr795707
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

stack-programC--

This document contains a C++ implementation of a stack data structure with functions for pushing, popping, checking if the stack is full or empty, and printing stack elements. The main function provides a user interface to interact with the stack, allowing users to insert, delete, and view elements. The stack has a maximum size of 10 and uses an array for storage.

Uploaded by

kkr795707
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

/* Below Stack implementation program is written in C++ language */

# include<iostream>
using namespace std;
const int n=10;
int top;
int Stack[n]; //Maximum size of Stack
// declaring all the function
void push(int x);
int pop();
bool isFull();
bool isEmpty();
void printStack();
// function to insert data into stack
void push(int x)
{
if (isFull()) cout << "Stack Overflow \n";
else
{
top = top + 1;
Stack[top] = x;

}
}
// function to remove data from the top of the stack
int pop()
{
int d;
if(isEmpty()) cout << "Stack Underflow \n";
else
{
d = Stack[top];
top = top -1;
return d;
}
}
// function to check if stack is full
bool isFull()
{
if(top >= n ) return true;
else return false;
}

// function to check if stack is empty


bool isEmpty()
{
if(top < 0 ) return true;
else return false;
}
// function to print stack elements
void printStack()
{
if (isEmpty()) cout << "Stack Underflow \n";
else
for (int i=0; i<=top; i++)
cout<<"stack["<<i<<"]="<<Stack[i]<<endl;
}
// main function
int main()
{
int c,m;
top=-1;
do
{
cout<<"Enter 1 to insert new element "<<endl;
cout<<"Enter 2 to delete top element "<<endl;
cout<<"Enter 3 to print stack elements "<<endl;
cout<<"Enter 4 to Exit "<<endl;
cout<<"Enter your choice :";
cin>>c;
switch (c)
{
case 1 : cout<<"Enter new elements : ";
cin>>m;
push(m);
break;
case 2 : m = pop();
if (! isEmpty())cout<<"deleting element
"<<m<<endl;break;
case 3 : printStack();break;
case 4 : exit;break;
default : cout<<" Error choice !!"<<endl;
}
} while (c != 4);
}

You might also like