-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackintro.cpp
More file actions
114 lines (106 loc) · 1.93 KB
/
Stackintro.cpp
File metadata and controls
114 lines (106 loc) · 1.93 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
102
103
104
105
106
107
108
109
110
111
112
113
114
#include<bits/stdc++.h>
#include<stack>
using namespace std;
//implementation of stack using array
class Stack
{ public:
//properties of class
int size;
int *arr;
int top;
//behaviours
Stack(int size)
{
//intialize size of array
this->size=size;
//ye func wala size=declared size var
arr=new int[size]; //initiliaze array with size
top=-1;
}
void push(int data)
{
if(size-top>1)
{
//if top reaches last index of array then ele cannot be inserted
top++;
arr[top]=data;
}
else
{
cout<<"Stack overflow"<<endl;
}
}
void pop()
{
if(top>0)
{
//only if ele are present in array
top--;
}
else
{
cout<<"Stack underflow";
}
}
int peek()
{
if(top>=0)
{
//ele presents
return arr[top];
}
else
{
cout<<"stack is empty"<<endl;
return -1;
}
}
bool isempty()
{
if(top==-1)
{
//no ele as top is same as intialized
return true;
}
else
{
return false;
}
}
};
int main()
{
//implementation of stack using stl
// stack<int>s;
// s.push(23); //inserting elements
// s.push(34);
// s.push(5);
// cout<<s.top()<<endl; //for top ele
// s.pop(); //removing ele
// if(s.empty())
// {
// //to check if stack is empty of not
// cout<<"empty";
// }
// else
// {
// cout<<"not empty";
// }
Stack st(5); //size given of 5 ele
st.push(2);
st.push(4);
st.push(33);
st.push(45);
cout<<st.peek()<<endl;
st.pop();
cout<<st.peek()<<endl;
if(st.isempty())
{
cout<<"yes empty"<<endl;
}
else
{
cout<<"not empty";
}
return 0;
}