Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create stack push & pop opeartion #68

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

aditi-disaster
Copy link

#include
#include
using namespace std;
#define SIZE 10
template
class stack
{
X *arr;
int top;
int capacity;
public:
stack(int size = SIZE); void push(X);
X pop();
X peek();
int size();
bool isEmpty(); bool isFull();
~stack(){
delete[] arr; }

};
template stack::stack(int size)
{
arr = new X[size];
capacity = size;
top = -1; }
template
void stack::push(X x)
{
if (isFull()) {
cout << "OverFlow\nProgram Terminated\n";
exit(EXIT_FAILURE); }
cout << "Inserting " << x << endl;
arr[++top] = x; }
template
X stack::pop()
{
if (isEmpty())
{
cout << "UnderFlow\nProgram Terminated\n";
exit(EXIT_FAILURE); }
cout << "Removing " << peek() << endl;
return arr[top--];
}
template
X stack::peek()
{
if (!isEmpty())
return arr[top];
else
exit(EXIT_FAILURE);
}
template
int stack::size()
{
return top + 1; }
template
bool stack::isEmpty() {
return top == -1; // or return size() == 0;
}
template
bool stack::isFull()
{
return top == capacity - 1;
}
int main() {
stack pt(2); pt.push("A"); pt.push("B"); pt.pop();
// or return size() == capacity;
pt.pop();
pt.push("C");
cout << "Top element is: " << pt.peek() << endl;

if (pt.isEmpty())
cout << "Stack Is Empty\n";
else
cout << "Stack Is Not Empty\n";
return 0; }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant