From bd39ed7c81c7ab2e776df1a06ddda2221ac4a613 Mon Sep 17 00:00:00 2001 From: Shubhrmcf07 <45631324+Shubhrmcf07@users.noreply.github.com> Date: Sun, 6 Oct 2019 20:20:19 +0530 Subject: [PATCH] Create stackimplement.cpp --- DataStructures/stackimplement.cpp | 81 +++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 DataStructures/stackimplement.cpp diff --git a/DataStructures/stackimplement.cpp b/DataStructures/stackimplement.cpp new file mode 100644 index 0000000..e83078d --- /dev/null +++ b/DataStructures/stackimplement.cpp @@ -0,0 +1,81 @@ +#include +#define max 100 //defining maximumsize of stack array globally +using namespace std; +class Stack +{ + int top; + int a[max]; //array implementation +public: + Stack() + { + top = -1; //signifies that initially the stack is empty + } + + void push(int); + int pop(); + int topele(); + bool isEmpty(); + void display(); +}; + +void Stack::push(int ele) //adding elements to the stack +{ + if(top == max - 1) + { + cout<<"Stack is full"; + } + + else + { + top = top + 1; + a[top] = ele; + } +} + +int Stack :: pop() //Removing elements from stack. Note- The last element added will be removed first +{ + if(top == -1) + { + cout<<"Stack is empty"; + return (-999); + } + else + { + int x = a[top]; + top = top - 1; + + return x; + } +} + +int Stack::topele() //returns the top element +{ + if(top == -1) + { + cout<<"Stack is empty"<