-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathStack.cpp
103 lines (68 loc) · 2.42 KB
/
Stack.cpp
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
// Push and pop operation in C++ without using poinetrs.
#include<iostream>
#include<stdlib.h>
#define SIZE 6
using namespace std;
class stack
{
private:
int item[SIZE];
int top;
public:
stack() // Creating a constructor for initializing the top variable.
{
top = -1;
}
void push(int value) // For pushing the values into stack
{
if(top == SIZE -1) // If the stack will be full, it will not able to take more values
{
cout<<"Stack overflow"<<endl;
return;
}
top ++;
item[top] = value; // Putting the value into top variable
}
int pop() // For deleting the values from stack
{
if(top == -1) // If the stack does not contain any element
{
cout<<"Stack underflow"<<endl;
return -9999; // Either if there is not any value in the stack, still it have to return an integer
// as the return type of the function pop is int, so we will return -9999.
}
int value;
value = item[top]; // Putting the top element in the variable value
top--; // And then decrementing the top by one to get the value of the below element
return value; // Returning the value
}
};
void push(int);
int pop();
int main()
{
stack s1; // Creating an object s1 of type stack
int choice, value;
cout<<"1. push"<<endl<<"2. pop"<<endl<<"3. exit"<<endl; // Displaying message for user to select option
while(1)
{
cout<<"Enter your choice :"<<endl;
cin>>choice;
switch(choice)
{
case 1: cout<<"Enter value :"<<endl;
cin>>value;
s1.push(value);
break;
case 2: value = s1.pop();
if(value != -9999)
{
cout<<"The deleted element will be "<<value<<endl;
}
break;
case 3:exit(0); // To exit the console
default: cout<<"Invalid choice"<<endl; // For invalid entries
}
}
return 0;
}