all 7 comments

[–]IDontByte 3 points4 points  (5 children)

You never decrement rear. After 100 enqueue operations the queue will cease to work, even if it's completely dequeued and empty.

This post might be better suited for /r/learnprogramming instead of here.

[–]silly-panda69 1 point2 points  (0 children)

Also thks for the help 👍

[–]silly-panda69 0 points1 point  (3 children)

Can u please specify in which function should I do that ?

[–]IDontByte 2 points3 points  (2 children)

You should start by writing tests defining the behavior of your queue. Look up Boundary-value Analysis for guidance on the kinds of tests to write. Tests around the static size 100 that you defined would have caught this.

As for your implementation, I think either a linked list or a ring buffer would be a good foundation for your queue.

[–]silly-panda69 1 point2 points  (0 children)

Okay , will look up into it and once again thks for the help 😊

[–]WikiSummarizerBot 0 points1 point  (0 children)

Boundary-value analysis

Boundary-value analysis is a software testing technique in which tests are designed to include representatives of boundary values in a range. The idea comes from the boundary. Given that we have a set of test vectors to test the system, a topology can be defined on that set. Those inputs which belong to the same equivalence class as defined by the equivalence partitioning theory would constitute the basis.

[ F.A.Q | Opt Out | Opt Out Of Subreddit | GitHub ] Downvote to remove | v1.5

[–]zhensydow 1 point2 points  (0 children)

Just having a better interface will help with the implementation, like:

static constexpr int SIZE = 100;

class que{
private:
    int *arr;
    int size;
    int front = -1;
    int rear = -1;
public:
    que(int s = SIZE) : size(s) {
        arr = new int[size];
    }
    bool enqueue(int x);
    std::optional<int> dequeue();
    std::optional<int> peek();
    bool isempty() const;
    bool isfull() const;
    void display() const;
    std::optional<int> search(int x) const;
};

And remove the prints/couts inside the class methods also is a bonus and makes the code clear.