mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
[fix/docs]: Memory leakage caused by using new
operator & refactoring/documentation (#1428)
* fix: Fixed memory leak bug by using 'std::vector' instead of 'new' * Small refactoring * Small refactoring * Fix Code Formatter test failed * Code refactored based on PR review * Added return 0 based on PR review * docs: Added documentation for linear queue using array implementation * docs: Updated based on PR review * docs: Second update based on PR review * docs: Updated based on PR review * Change max_size data type * Use std::array instead of std::vector
This commit is contained in:
parent
5bd438066c
commit
b4b182a61d
@ -1,77 +1,120 @@
|
||||
/*
|
||||
Write a program to implement Linear Queue using array.
|
||||
/**
|
||||
* @file
|
||||
* @brief Implementation of Linear [Queue using array]
|
||||
* (https://www.geeksforgeeks.org/array-implementation-of-queue-simple/).
|
||||
* @details
|
||||
* The Linear Queue is a data structure used for holding a sequence of
|
||||
* values, which can be added to the end line (enqueue), removed from
|
||||
* head of line (dequeue) and displayed.
|
||||
* ### Algorithm
|
||||
* Values can be added by increasing the `rear` variable by 1 (which points to
|
||||
* the end of the array), then assigning new value to `rear`'s element of the array.
|
||||
*
|
||||
* Values can be removed by increasing the `front` variable by 1 (which points to
|
||||
* the first of the array), so it cannot reached any more.
|
||||
*
|
||||
* @author [Pooja](https://github.com/pooja-git11)
|
||||
* @author [Farbod Ahmadian](https://github.com/farbodahm)
|
||||
*/
|
||||
#include <iostream> /// for io operations
|
||||
#include <array> /// for std::array
|
||||
|
||||
Functions to implement
|
||||
Enqueue (Insertion)
|
||||
Dequeue (Deletion)
|
||||
constexpr uint16_t max_size{10}; ///< Maximum size of the queue
|
||||
|
||||
*/
|
||||
#include <iostream>
|
||||
/**
|
||||
* @namespace data_structures
|
||||
* @brief Algorithms with data structures
|
||||
*/
|
||||
namespace data_structures {
|
||||
|
||||
#define MAXSIZE 10
|
||||
/**
|
||||
* @namespace queue_using_array
|
||||
* @brief Functions for [Queue using Array]
|
||||
* (https://www.geeksforgeeks.org/array-implementation-of-queue-simple/) implementation.
|
||||
*/
|
||||
namespace queue_using_array {
|
||||
|
||||
/**
|
||||
* @brief Queue_Array class containing the main data and also index of head and tail of the array.
|
||||
*/
|
||||
class Queue_Array {
|
||||
int front;
|
||||
int rear;
|
||||
int size;
|
||||
|
||||
public:
|
||||
Queue_Array() {
|
||||
front = -1;
|
||||
rear = -1;
|
||||
size = MAXSIZE;
|
||||
}
|
||||
int *arr = new int[size];
|
||||
void enqueue(int);
|
||||
int dequeue();
|
||||
void display();
|
||||
public:
|
||||
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:
|
||||
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_size> arr; ///< All stored data
|
||||
};
|
||||
|
||||
void Queue_Array::enqueue(int ele) {
|
||||
if (rear == size - 1) {
|
||||
/**
|
||||
* @brief Adds new element to the end of the queue
|
||||
* @param ele to be added to the end of the queue
|
||||
*/
|
||||
void Queue_Array::enqueue(const int16_t& ele ) {
|
||||
if (rear == arr.size() - 1) {
|
||||
std::cout << "\nStack is full";
|
||||
} else if (front == -1 && rear == -1) {
|
||||
front = rear = 0;
|
||||
front = 0;
|
||||
rear = 0;
|
||||
arr[rear] = ele;
|
||||
} else if (rear < size) {
|
||||
rear++;
|
||||
} else if (rear < arr.size()) {
|
||||
++rear;
|
||||
arr[rear] = ele;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Remove element that is located at the first of the queue
|
||||
* @returns data that is deleted if queue is not empty
|
||||
*/
|
||||
int Queue_Array::dequeue() {
|
||||
int d;
|
||||
int8_t d{0};
|
||||
if (front == -1) {
|
||||
std::cout << "\nstack is empty ";
|
||||
return 0;
|
||||
} else if (front == rear) {
|
||||
d = arr[front];
|
||||
d = arr.at(front);
|
||||
front = rear = -1;
|
||||
} else {
|
||||
d = arr[front++];
|
||||
d = arr.at(front++);
|
||||
}
|
||||
|
||||
return d;
|
||||
}
|
||||
|
||||
void Queue_Array::display() {
|
||||
/**
|
||||
* @brief Utility function to show all elements in the queue
|
||||
*/
|
||||
void Queue_Array::display() const {
|
||||
if (front == -1) {
|
||||
std::cout << "\nStack is empty";
|
||||
} else {
|
||||
for (int i = front; i <= rear; i++) std::cout << arr[i] << " ";
|
||||
for (int16_t i{front}; i <= rear; ++i) std::cout << arr.at(i) << " ";
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
int op, data;
|
||||
} // namespace queue_using_array
|
||||
} // namespace data_structures
|
||||
|
||||
Queue_Array ob;
|
||||
|
||||
/**
|
||||
* @brief Main function
|
||||
* @details
|
||||
* Allows the user to add and delete values from the queue.
|
||||
* Also allows user to display values in the queue.
|
||||
* @returns 0 on exit
|
||||
*/
|
||||
int main() {
|
||||
int op{0}, data{0};
|
||||
data_structures::queue_using_array::Queue_Array ob;
|
||||
|
||||
std::cout << "\n1. enqueue(Insertion) ";
|
||||
std::cout << "\n2. dequeue(Deletion)";
|
||||
std::cout << "\n3. Display";
|
||||
std::cout << "\n4. Exit";
|
||||
while (1) {
|
||||
while (true) {
|
||||
std::cout << "\nEnter your choice ";
|
||||
std::cin >> op;
|
||||
if (op == 1) {
|
||||
@ -89,4 +132,6 @@ int main() {
|
||||
std::cout << "\nWrong choice ";
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user