-
-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathstackimplement.cpp
More file actions
81 lines (72 loc) · 1.14 KB
/
stackimplement.cpp
File metadata and controls
81 lines (72 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
}