-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStacks.cpp
76 lines (64 loc) · 1.47 KB
/
Stacks.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
71
72
73
74
75
76
//Making a Stack using Dynamic Array :
#include<iostream>
using namespace std ;
class stack{
private:
int *data ;
int NextIndex ;
int capacity ;
public:
stack(int TotalSize){
data=new int[TotalSize];
NextIndex=0 ;
capacity=TotalSize;
}
//Return the Number of Elements present in my Stack
int size(){
return NextIndex;
}
bool isEmpty(){
if(NextIndex==0)
return true ;
else
{
return false ;
}
}
//Inserting an element into the Stack :
void push(int element){
if(NextIndex==capacity){
cout << "Stack is Full !! You cannot insert more Elements !!" << endl ;
return ;
}else{
data[NextIndex]=element ;
NextIndex++ ;
}
}
//Deleting an Element
int pop(){
if(NextIndex==0){
cout << "Sorry !! The Stack is Empty You cannot delete More Elements !!" << endl ;
}else{
NextIndex-- ;
return data[NextIndex];
}
}
int top(){
if(isEmpty())
cout << "Sorry !! The Stack is Empty " << endl ;
else
return data[NextIndex-1];
}
};
int main(){
stack s1(4) ;
s1.push(10);
s1.push(20);
s1.push(30);
s1.push(40);
s1.push(50);
cout << s1.top() << endl ;
cout << s1.pop() << endl ;
cout << s1.size() << endl ;
cout << s1.isEmpty() << endl ;
}