-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack_using_class.cpp
More file actions
102 lines (87 loc) · 1.7 KB
/
Copy pathStack_using_class.cpp
File metadata and controls
102 lines (87 loc) · 1.7 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
#include <bits/stdc++.h>
using namespace std;
class Stack
{
private:
int top;
int size;
int *s;
public:
Stack(int size)
{
this->size = size;
this->top = -1;
this->s = new int[size];
}
bool isEmpty()
{
if (top == -1)
return true;
return false;
}
bool isFull()
{
if (top == size - 1)
return true;
return false;
}
void push(int data)
{
if (isFull())
cout << "OverFlow Condition!" << endl;
else if (top == -1)
{
top = 0;
s[top] = data;
cout << data << " is pushed into the stack." << endl;
}
else
{
top++;
s[top] = data;
cout << data << " is pushed into the stack." << endl;
}
}
void pop()
{
if (isEmpty())
cout << "UnderFlow Condition!";
cout << s[top--] << " is deleted or popped!" << endl;
}
void peek()
{
if (isEmpty())
cout << "UnderFlow Condition!";
cout << "\nElement at the top is : " << s[top] << endl;
}
void display()
{
if (isEmpty())
cout << "UnderFlow Condition!" << endl;
cout << "\nStack is : ";
for (int i = 0; i <= top; i++)
cout << s[i] << " ";
cout << "\n\n";
}
};
int main()
{
int size;
cout << "Enter the size of stack : ";
cin >> size;
Stack s(size);
s.push(10);
s.push(20);
s.push(30);
s.push(40);
s.push(50);
s.peek();
s.display();
s.pop();
s.pop();
s.peek();
s.display();
s.pop();
s.display();
return 0;
}