TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
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
24constexpr uint16_t max_size{10};
25
30namespace data_structures {
31
38namespace queue_using_array {
39
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
59void 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
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
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
112int main() {
113 int op{0}, data{0};
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}
Queue_Array class containing the main data and also index of head and tail of the array.
int dequeue()
Delete element from back of the queue.
void enqueue(const int16_t &)
Add element to the first of the queue.
std::array< int16_t, max_size > arr
All stored data.
int data[MAX]
test data
for IO operations
Functions for [Queue using Array] (https://www.geeksforgeeks.org/array-implementation-of-queue-simple...
constexpr uint16_t max_size
for std::array
int main()
Main function.