-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathstack.cpp
More file actions
77 lines (64 loc) · 1.33 KB
/
stack.cpp
File metadata and controls
77 lines (64 loc) · 1.33 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
#include<bits/stdc++.h>
#define SIZE 100 //default stack size
using namespace std;
//Stack Implementation
class Stack {
int *arr; //pointer to stack
int top; //index of topmost element in stack
int capacity; //maximum size of stack
public:
Stack(int size = SIZE);
~Stack();
void push(int);
int pop();
int size();
bool isEmpty();
bool isFull();
};
//Constructor for class stack initialized with size of stack
Stack::Stack(int size) {
arr = new int[size];
capacity = size;
top = -1;
}
//destructor for class stack
Stack::~Stack() {
delete arr;
}
//function to push element into stack
void Stack::push(int x) {
if(isFull()) {
cout<<"Stack Overflow!!!"<<endl;
}
else {
cout<<"Inserting "<<x<<" into the stack..."<<endl;
top++;
arr[top] = x;
}
}
//function to pop element from stack
int Stack::pop() {
if(isEmpty()) {
cout<<"Stack Empty!!!"<<endl;
}
else {
cout<<"Removing "<<arr[top]<<" from the stack..."<<endl;
return arr[top];
top--;
}
}
//function to return stack size
int Stack::size() {
return top+1;
}
//function to check whether stack is empty
bool Stack::isEmpty() {
return top==-1;
}
//function to check whether stack has reached maximum capacity
bool Stack::isFull() {
return top == capacity-1;
}
int main() {
return 0;
}