Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adding implementation of queue by a binary file #2677

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
134 changes: 134 additions & 0 deletions data_structures/circular_queue_using_binaric_file.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/**
* @file
* @brief Implementation of a queue using a file.
* @details
* This program implements a queue using a file.
* It includes a template class `MyQueue` defining the queue interface,
* and a class `QueueFile` implementing the queue using a file as storage.
* * @algorithm
* The qoeoe include two pointers to the head and the tail of the qoueue
* And it also contain fstraem file there the data of the queue kept.
* When removing the value of the head indexOfFirst increase by one
* @author [chavi](https://github.com/chavi362)
*/

#include <fstream>
#include <iostream>
#include <string>
Comment on lines +15 to +17
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should document headers as well

#include <header> /// For <reason>



/**
* @interface MyQueue
* @brief A generic queue interface.
* @tparam T The type of elements stored in the queue.
*/
template <class T>
class MyQueue {
public:
/// Clears the queue.
virtual void clear() = 0;

/// Removes and returns the front element of the queue.
virtual T dequeue() = 0;

/// Adds an element to the rear of the queue.
virtual void enqueue(T value) = 0;

/// Returns the front element of the queue without removing it.
virtual T front() = 0;

/// Checks if the queue is empty.
virtual bool isEmpty() = 0;

/// Destructor.
virtual ~MyQueue() {}
};

/**
* @class QueueFile
* @brief A queue implementation using a file as storage.
* @tparam T The type of elements stored in the queue.
*/
template <class T>
class QueueFile : public MyQueue<T> {
int queueSize = 10;
int indexOfFirst = 0;
int indexOfLast = 0;
fstream data;

/// Checks if the queue is full.
bool isFull() { return indexOfFirst == indexOfLast + 1; }

public:
/**
* @brief Constructor for QueueFile.
* @param size The size of the queue.
*/
QueueFile(int size = 100) : queueSize(size) {
data.open("queue.dat", ios::out);
T stam;
for (int i = 0; i < size; i++) {
data.write((char*)&stam, sizeof(T));
}
data.close();
data.open("queue.dat", ios::out | ios::in);
if (!data)
throw "error in opening";
}

/// Clears the queue.
void clear() override {
indexOfFirst = 0;
indexOfLast = 0;
}

/// Checks if the queue is empty.
bool isEmpty() override { return indexOfFirst == indexOfLast; }

/// Returns the front element of the queue without removing it.
T front() override {
if (this->isEmpty())
throw "underflow";
chavi362 marked this conversation as resolved.
Show resolved Hide resolved
data.seekg(indexOfFirst * sizeof(T), ios::beg);
T s;
data.read((char*)&s, sizeof(T));
return s;
}

/// Removes and returns the front element of the queue.
T dequeue() override {
if (this->isEmpty())
throw "underflow";
data.seekg(indexOfFirst * sizeof(T), ios::beg);
T s;
data.read((char*)&s, sizeof(T));
if (indexOfFirst == queueSize)
indexOfFirst = 0;
else
indexOfFirst++;
return s;
}

/// Adds an element to the rear of the queue.
void enqueue(T value) override {
if (this->isFull())
throw "overflow";
data.seekp(indexOfLast * sizeof(T), ios::beg);
data.write((char*)&value, sizeof(value));
if (indexOfLast == queueSize)
indexOfLast = 0;
else
indexOfLast++;
}

/// Destructor
~QueueFile() override { data.close(); }
};

/**
* @brief Main function.
* @returns 0 on exit.
*/
int main() {
return 0;
}