TheAlgorithms/C++
1.0.0
All the algorithms implemented in C++
Toggle main menu visibility
Loading...
Searching...
No Matches
queue_using_array.cpp
Go to the documentation of this file.
1
20
#include <array>
21
#include <cstdint>
22
#include <iostream>
23
24
constexpr
uint16_t
max_size
{10};
25
30
namespace
data_structures
{
31
38
namespace
queue_using_array
{
39
44
class
Queue_Array
{
45
public
:
46
void
enqueue
(
const
int16_t&);
47
int
dequeue
();
48
void
display
()
const
;
49
private
:
50
int8_t
front
{-1};
51
int8_t
rear
{-1};
52
std::array<int16_t, max_size>
arr
{};
53
};
54
59
void
Queue_Array::enqueue
(
const
int16_t& ele) {
60
if
(
rear
==
arr
.size() - 1) {
61
std::cout <<
"\nStack is full"
;
62
}
else
if
(
front
== -1 &&
rear
== -1) {
63
front
= 0;
64
rear
= 0;
65
arr
[
rear
] = ele;
66
}
else
if
(
rear
<
arr
.size()) {
67
++
rear
;
68
arr
[
rear
] = ele;
69
}
70
}
71
76
int
Queue_Array::dequeue
() {
77
int8_t d{0};
78
if
(
front
== -1) {
79
std::cout <<
"\nstack is empty "
;
80
return
0;
81
}
else
if
(
front
==
rear
) {
82
d =
arr
.at(
front
);
83
front
=
rear
= -1;
84
}
else
{
85
d =
arr
.at(
front
++);
86
}
87
88
return
d;
89
}
90
94
void
Queue_Array::display
()
const
{
95
if
(
front
== -1) {
96
std::cout <<
"\nStack is empty"
;
97
}
else
{
98
for
(int16_t i{
front
}; i <=
rear
; ++i) std::cout <<
arr
.at(i) <<
" "
;
99
}
100
}
101
102
}
// namespace queue_using_array
103
}
// namespace data_structures
104
112
int
main
() {
113
int
op{0},
data
{0};
114
data_structures::queue_using_array::Queue_Array
ob;
115
116
std::cout <<
"\n1. enqueue(Insertion) "
;
117
std::cout <<
"\n2. dequeue(Deletion)"
;
118
std::cout <<
"\n3. Display"
;
119
std::cout <<
"\n4. Exit"
;
120
while
(
true
) {
121
std::cout <<
"\nEnter your choice "
;
122
std::cin >> op;
123
if
(op == 1) {
124
std::cout <<
"Enter data "
;
125
std::cin >>
data
;
126
ob.
enqueue
(
data
);
127
}
else
if
(op == 2) {
128
data
= ob.
dequeue
();
129
std::cout <<
"\ndequeue element is:\t"
<<
data
;
130
}
else
if
(op == 3) {
131
ob.
display
();
132
}
else
if
(op == 4) {
133
exit(0);
134
}
else
{
135
std::cout <<
"\nWrong choice "
;
136
}
137
}
138
139
return
0;
140
}
data_structures::queue_using_array::Queue_Array
Queue_Array class containing the main data and also index of head and tail of the array.
Definition
queue_using_array.cpp:44
data_structures::queue_using_array::Queue_Array::dequeue
int dequeue()
Delete element from back of the queue.
Definition
queue_using_array.cpp:76
data_structures::queue_using_array::Queue_Array::front
int8_t front
Index of head of the array.
Definition
queue_using_array.cpp:50
data_structures::queue_using_array::Queue_Array::enqueue
void enqueue(const int16_t &)
Add element to the first of the queue.
Definition
queue_using_array.cpp:59
data_structures::queue_using_array::Queue_Array::display
void display() const
Show all saved data.
Definition
queue_using_array.cpp:94
data_structures::queue_using_array::Queue_Array::rear
int8_t rear
Index of tail of the array.
Definition
queue_using_array.cpp:51
data_structures::queue_using_array::Queue_Array::arr
std::array< int16_t, max_size > arr
All stored data.
Definition
queue_using_array.cpp:52
main
int main()
Main function.
Definition
generate_parentheses.cpp:110
data
int data[MAX]
test data
Definition
hash_search.cpp:24
data_structures
for IO operations
queue_using_array
Functions for [Queue using Array] (https://www.geeksforgeeks.org/array-implementation-of-queue-simple...
max_size
constexpr uint16_t max_size
for std::array
Definition
queue_using_array.cpp:24
data_structures
queue_using_array.cpp
Generated by
1.18.0