-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack_Implementation_using_Array.cpp
More file actions
100 lines (97 loc) · 1.61 KB
/
Stack_Implementation_using_Array.cpp
File metadata and controls
100 lines (97 loc) · 1.61 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
/*
//** Auther : Abdullah Al Masum
*/
#include <stdio.h>
#include <stdlib.h>
#include <bits/stdc++.h>
#define MAXX 10
using namespace std;
class Stackk
{
public:
int top;
int arr[MAXX];
Stackk() { top = -1; }
bool isEmpty();
bool isFull();
void push(int val);
void pop();
void peek();
void traverse();
};
bool Stackk ::isEmpty()
{
if (top == -1)
return true;
}
return false;
}
bool Stackk ::isFull()
{
if (top >= MAXX /*sizeof(arr) / sizeof(arr[0]) */)
{
return true;
}
return false;
}
void Stackk ::push(int val)
{
if (isFull())
{
cout << val << " can not be pushed into stack" << endl;
return;
}
top++;
arr[top] = val;
cout << val << " is pushed" << endl;
return;
}
void Stackk ::pop()
{
if (isEmpty())
{
cout << "Can not be popped" << endl;
return;
}
cout << arr[top] << " is popped from stack" << endl;
top--;
return;
}
void Stackk ::traverse()
{
if (isEmpty())
{
cout << "Stack is Empty at this moment" << endl;
return;
}
cout << "Traversing... ";
int temp = 0;
while (temp != top + 1)
{
cout << arr[temp] << " ";
++temp;
}
return;
}
void Stackk ::peek()
{
if (isEmpty())
{
cout << "can not be peeked as Stack is Empty" << endl;
return;
}
cout << "At the top " << top << ", Top element is: " << arr[top] << endl;
return;
}
int main()
{
Stackk st;
st.peek();
st.push(5);
st.traverse();
st.peek();
st.pop();
st.push(9);
st.traverse();
return 0;
}