Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
#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; }