Skip to content
This repository was archived by the owner on Oct 22, 2021. It is now read-only.
Open
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
83 changes: 83 additions & 0 deletions Data Structures/Stack/Stack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include <iostream>
#include <bits/stdc++.h>

using namespace std;

#define MAX 100

class Stack {
int top;
public:
int a[MAX]; // Maximum size of Stack

Stack() { top = -1; }
bool push(int x);
int pop();
int peek();
bool isEmpty();
};

bool Stack::push(int x) {
// Overflow Condition
if (top >= (MAX - 1)) {
cout << "Stack Overflow";
return false;
}
else {
a[++top] = x;
return true;
}
}

int Stack::pop() {
// Underflow Conditions
if (top < 0) {
cout << "Stack Underflow";
return 0;
}
else {
int x = a[top--];
return x;
}
}

int Stack::peek() {
if (top < 0) {
cout << "Stack is Empty";
return 0;
}
else {
int x = a[top];
return x;
}
}

bool Stack::isEmpty() {
return (top < 0);
}

// Driver program to test above functions
int main() {
class Stack stk;

// Adding Item to Stack
stk.push(1);
stk.push(2);
stk.push(3);
cout<< "1, 2, 3 added to stack\n";

// Pop up Stack
cout << stk.pop() << " Popped from stack\n";

// Get the top values
cout << stk.peek() << " is the peek value\n";

// isEmpty
if (stk.isEmpty() < 0){
cout<< "Empty\n";
}
else {
cout << "Not empty\n";
}
return 0;
}