-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack class.cpp
72 lines (58 loc) · 1.34 KB
/
Stack class.cpp
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
//I have a stack file but in that we used stack library to perform operations but here we will impliment it using class so this new file is created
//Implementaion of stack in array
#include <iostream>
using namespace std;
class stack{
private:
int* arr;
int top;
int maxsize;
public:
//constructor
stack(int size){
arr = new int[size];
maxsize = size;
top = -1;
}
//function to add
void push(int value){
if(top == maxsize - 1){
cout << "Stack is full" << endl;
}
else{
top++;
arr[top] = value;
/*cout << value << " pushed into the stack" << endl;*/
}
}
void print(){
if(top == -1){
cout << "Stack is empty" << endl;
}else{
for(int i = top; i >= 0; i--){
cout << arr[i] << endl;
}
cout << endl;
}
}
/* void pop(){
if(top == -1){
cout << "Stack is empty" << endl;
}else{
int popv = arr[top] ;
top--;
return popv;
}
} */
};
int main(){
cout << "Implementation of stack using class: " << endl;
stack arr(5);
arr.push(1);
arr.push(2);
arr.push(3);
arr.push(4);
arr.push(5);
arr.print();
return 0;
}