forked from Sanchitbajaj02/CppPrograms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack Using Array.cpp
More file actions
101 lines (101 loc) · 1.99 KB
/
Stack Using Array.cpp
File metadata and controls
101 lines (101 loc) · 1.99 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include<iostream>
using namespace std;
class Stack
{
private:
int *a = new int[5] ;
int capacity = 5 ;
int size = 0 ;
public:
bool isEmpty()
{
if(this->size == 0)
return true ;
else
return false ;
}
int Size()
{
return this->size;
}
void push(int element)
{
if(this->size == this->capacity)
{
capacity *= 2 ;
int *a = new int[capacity] ;
for(int i=0;i<this->size;i++)
a[i+1] = this->a[i] ;
//delete this->[]a ;
this->a = a ;
delete a ;
this->a[0] = element ;
size++ ;
}
else
{
for(int i=size;i>0;i--)
this->a[i] = this->a[i-1] ;
this->a[0] = element ;
size++ ;
}
}
int top()
{
if(this->size == 0)
{
cout<<"The Stack is Empty"<<endl;
return -1;
}
else
{
int x = this->a[0] ;
return x ;
}
}
void pop()
{
if(this->size == 0)
{
cout<<"The Stack is Empty"<<endl;
return;
}
else
{
for(int i=0;i<size;i++)
this->a[i] = this->a[i+1] ;
size-- ;
return ;
}
}
};
int main()
{
Stack s ;
s.push(1);
s.push(2);
s.push(3);
s.push(4);
s.push(5);
s.push(6);
s.push(7);
s.push(8);
cout<<"Size of Stack :- "<<s.Size()<<endl;
cout<<s.top()<<endl;
s.pop();
cout<<s.top()<<endl;
s.pop();
cout<<s.top()<<endl;
s.pop();
cout<<s.top()<<endl;
cout<<s.top()<<endl;
s.pop();
cout<<s.top()<<endl;
s.pop();
cout<<s.top()<<endl;
s.pop();
cout<<s.top()<<endl;
s.pop();
cout<<s.top()<<endl;
return 0 ;
}