forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack_using_Array.cpp
145 lines (133 loc) · 3.2 KB
/
Stack_using_Array.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
Implement the basic functionality of stack using Array Data structure
@author: Rishikeshrajrxl
@created: 17/08/2020
*/
#include <iostream>
using namespace std;
void Push(); //Function declaration
void Peek();
void Pop();
void Is_Empty();
void Is_Full();
void Display();
void count();
int stack_size,top=0,stack[100];
int main() //Main() starts
{
int ch;
cout<<"ENTER THE STACK SIZE : "; //taking stack size initally from the user.
cin>>stack_size;
cout<< "\n\n\t Implement the basic functionality of stack"<<endl; //Displaying the Features in a tabular manner
cout<<"\t **************************************************"<<endl;
do
{
cout<<"\t 1) Push\t \t 2) Pop "<<endl;
cout<<"\t 3) Peek\t \t 4) Is_Empty"<<endl;
cout<<"\t 5) Is_Full\t \t 6) Display"<<endl;
cout<<"\t 7) count\t \t 8) Exit"<<endl;
cout<<"\t ***************************************************\nENTER YOUR CHOICE : ";
cin >>ch;
switch(ch)
{
case 1:
Push();
break;
case 2:
Pop();
break;
case 3:
Peek();
break;
case 4:
Is_Empty();
break;
case 5:
Is_Full();
break;
case 6:
Display();
break;
case 7:
count();
break;
case 8:
exit(0);
break;
default:
cout<<"Invalid Input. Please enter the Number between 1 to 8 \n"<<endl;
}
}while(ch!=0);
return 0;
} // Main() Ends
//--------------------------------------------------------------------------
void Push() //Insertion at the top
{
int ele;
if(top==stack_size){
cout<<"Stack is Full."<<endl;
}
else{
cout<<"ENTER THE ELEMENT :";
cin>>ele;
top+=1;
stack[top]=ele;
cout<<"Pushed Successfully"<<endl;
}
}
//---------------------------------------------------------------------------
void Pop() //deletion from the top
{
if(top==0){
cout<<"Stack is Empty"<<endl;
}
else{
top=top-1;
cout<<"Poped Successfully"<<endl;
}
}
//---------------------------------------------------------------------------
void Peek() //returns the value of the top
{
if(top==0){
cout<<"Stack is Empty"<<endl;
}
else{
cout<<"Peek value is : "<<stack[top]<<endl;
}
}
//-----------------------------------------------------------------------------
void Is_Full() //returns Yes if Full, else No
{
if(top==stack_size){
cout<<"Yes"<<endl;
}
else{
cout<<"No"<<endl;
}
}
//-----------------------------------------------------------------------------
void Is_Empty() //returns Yes if Empty, else No
{
if(top==0){
cout<<"Yes"<<endl;
}
else{
cout<<"No"<<endl;
}
}
//------------------------------------------------------------------------------
void Display() //returns all the value inside the Stack
{
cout<<"ELEMENT IN THE STACK : ";
for(int i=top;i>0;i--)
{
cout<<stack[i]<<" ";
}
cout<<"\n";
}
//-------------------------------------------------------------------------------
void count() //returns the length of stack
{
cout<<"Length of Stack : "<<top<<endl;
}