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

Stack #29

Merged
merged 1 commit into from
Oct 17, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Stack implementation
  • Loading branch information
SAchu47 committed Oct 17, 2019
commit 96ac491b9c6632273b25dd25ecdc7179f7274e22
125 changes: 125 additions & 0 deletions Data Structures/03 Stack/Stack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#include <iostream>
#include <cstdlib>
using namespace std;

// define size capacity of the stack
#define SIZE 10

// Class for stack
class stack
{
int *arr;
int top;
int capacity;

public:
// constructor
stack(int size = SIZE);
// destructor
~stack();

void push(int);
int pop();
int peek();

int size();
bool isEmpty();
bool isFull();
};

// Constructor to initialize stack
stack::stack(int size)
{
arr = new int[size];
capacity = size;
top = -1;
}

// Destructor to free memory allocated to the stack
stack::~stack()
{
delete arr;
}

//Function to add an element x in the stack
void stack::push(int x)
{
if (isFull())
{
cout << "OverFlow\nProgram Terminated\n";
exit(EXIT_FAILURE);
}

cout << "Inserting " << x << endl;
arr[++top] = x;
}

//Function to pop top element from the stack
int stack::pop()
{
// check for stack underflow
if (isEmpty())
{
cout << "UnderFlow\nProgram Terminated\n";
exit(EXIT_FAILURE);
}

cout << "Removing " << peek() << endl;

// decrease stack size by 1 and return the popped element if required
return arr[top--];
}

//Function to return top element in a stack
int stack::peek()
{
if (!isEmpty())
return arr[top];
else
exit(EXIT_FAILURE);
}

//Function to return the size of the stack
int stack::size()
{
return top + 1;
}

//Function to check if the stack is empty or not
bool stack::isEmpty()
{
return top == -1; // or return size() == 0;
}

//Function to check if the stack is full or not
bool stack::isFull()
{
return top == capacity - 1; // or return size() == capacity;
}

// Driver unction
int main()
{
stack pt(3);

pt.push(1);
pt.push(2);

pt.pop();
pt.pop();

pt.push(3);

cout << "Top element is: " << pt.peek() << endl;

cout << "Stack size is " << pt.size() << endl;

pt.pop();

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

return 0;
}