-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstack_class_implementation.cpp
More file actions
117 lines (95 loc) · 1.65 KB
/
stack_class_implementation.cpp
File metadata and controls
117 lines (95 loc) · 1.65 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
115
116
#include<iostream>
#include<cstdlib>
#define SIZE 100 // or const int SIZE=100;
using namespace std;
class stack
{
/*
objective : Create a class for implementing Stack using Array
input parameters: none
output value: none
description: class definition
approach: class defines data member and member function of the stack class
*/
int *arr; // for dynamic array
int top;
int capacity;
public:
stack(int size=SIZE) //constructor
{ arr= new int[SIZE];
top=-1;
}
void push(int &value)
{
top+=1;
arr[top]=value;
}
int pop()
{
int temp = arr[top];
top-=1;
return temp;
}
int peek()
{
return arr[top];
}
int size()
{
return top+1;
}
bool isEmpty()
{
if(top==-1)
return true;
else
return false;
}
bool isFull()
{
if(SIZE==top+1)
return true;
else
return false;
}
~stack() //destructor
{
delete arr;
}
};
int main()
{
stack sobj;
int n,value;
cout<<"\n Press \n 1. Push\n 2. Pop\n 3. Peek \n 4. Size\n 5. Is Empty?\n 6. Is Full?\t";
cin>>n;
switch(n)
{
case 1:
cout<<"\nEnter the value to be inserted:\t";
cin>>value;
sobj.push(value);
break;
case 2:
cout<<"\nThe value that is popped is: "<<sobj.pop();
break;
case 3:
cout<<"\nThe Value at the top of the stack is: "<<sobj.peek();
break;
case 4:
cout<<"\nThe size of the stack is: "<<sobj.size();
break;
case 5:
if(sobj.isEmpty()==true)
cout<<"\nThe stack is empty. ";
else
cout<<"\nThe stack if not empty. ";
break;
case 6:
if(sobj.isFull()==true)
cout<<"\nThe stack is full. ";
else
cout<<"\nThe stack if not full. ";
break;
}
}