TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
data_structures::queue_using_array::Queue_Array Class Reference

Queue_Array class containing the main data and also index of head and tail of the array. More...

Collaboration diagram for data_structures::queue_using_array::Queue_Array:
[legend]

Public Member Functions

void enqueue (const int16_t &)
 Add element to the first of the queue.
 
int dequeue ()
 Delete element from back of the queue.
 
void display () const
 Show all saved data.
 

Private Attributes

int8_t front {-1}
 Index of head of the array.
 
int8_t rear {-1}
 Index of tail of the array.
 
std::array< int16_t, max_sizearr {}
 All stored data.
 

Detailed Description

Queue_Array class containing the main data and also index of head and tail of the array.

Definition at line 44 of file queue_using_array.cpp.

Member Function Documentation

◆ dequeue()

int data_structures::queue_using_array::Queue_Array::dequeue ( )

Delete element from back of the queue.

Remove element that is located at the first of the queue.

Returns
data that is deleted if queue is not empty

Definition at line 76 of file queue_using_array.cpp.

76 {
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}
std::array< int16_t, max_size > arr
All stored data.

◆ display()

void data_structures::queue_using_array::Queue_Array::display ( ) const

Show all saved data.

Utility function to show all elements in the queue.

Definition at line 94 of file queue_using_array.cpp.

94 {
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}

◆ enqueue()

void data_structures::queue_using_array::Queue_Array::enqueue ( const int16_t & ele)

Add element to the first of the queue.

Adds new element to the end of the queue.

Parameters
eleto be added to the end of the queue

Definition at line 59 of file queue_using_array.cpp.

59 {
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}

Member Data Documentation

◆ arr

std::array<int16_t, max_size> data_structures::queue_using_array::Queue_Array::arr {}
private

All stored data.

Definition at line 52 of file queue_using_array.cpp.

52{};

◆ front

int8_t data_structures::queue_using_array::Queue_Array::front {-1}
private

Index of head of the array.

Definition at line 50 of file queue_using_array.cpp.

50{-1};

◆ rear

int8_t data_structures::queue_using_array::Queue_Array::rear {-1}
private

Index of tail of the array.

Definition at line 51 of file queue_using_array.cpp.

51{-1};

The documentation for this class was generated from the following file: