Implement a Stack to store a character in the node. Implement the following operations
push, pop and peek:.
void push (char c);
char pop();
char peek();
int isEmpty();
void printStack();
#include <bits/stdc++.h>
using namespace std;
#define SIZE 100
class stack
{
int *arr;
int top;
int capacity;
public:
stack(int size = SIZE);
~stack();
void push(int);
int pop();
int peek();
int size();
bool isEmpty();
bool isFull();
};
stack::stack(int size)
{
arr = new int[size];
capacity = size;
top = -1;
}
stack::~stack() {
delete[] arr;
}
void stack::push(int x)
{
if (isFull())
{
exit(EXIT_FAILURE);
}
cout << "Insert " << x << endl;
arr[++top] = x;
}
int stack::pop()
{
if (isEmpty())
{
exit(EXIT_FAILURE);
}
cout << "Remove " << peek() << endl;
return arr[top--];
}
int stack::peek()
{
if (!isEmpty()) {
return arr[top];
}
else {
exit(EXIT_FAILURE);
}
}
bool stack::isEmpty() {
return top == -1;
}
Comments
Leave a comment