Skip to content
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
81 changes: 81 additions & 0 deletions DataStructures/stackimplement.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include<iostream>
#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"<<endl;
return (-999);
}
else
{
return a[top];
}
}

bool Stack :: isEmpty()
{
return (top == -1);
}

void Stack::display()
{
while(!Stack::isEmpty())
{
cout<<Stack::pop()<<"|";
}
}

int main()
{
Stack s;
//add your code to suit your needs
}