Class Array Declaration Size

Asked 2 years ago, Updated 2 years ago, 63 views

I declared a queue using a low-cost array, but I think the size of the element is 5, so I think a total of 6 elements can fit in it, but there are more than 6 sizes in the array. Why is that?

#include <iostream>
using namespace std;

class queue {
public:
    int front;
    int rear;
    int size;
    int element[5];

public:
    queue() :front(0), rear(0), size(0) { }
    void enqueue(int);
    void dequeue();
    bool empty();
    bool full();
    void print();
};

void queue::enqueue(int value) {
    if (!full()) {
        cout << "Front Rear" << front << " " << rear << endl;
        element[rear] = value;
        rear++;
        size++;
    }
}

void queue::dequeue() {
    if (!empty()) {
        element[front] = 0;
        front++;
        size--;
    }
    else {
        cout << "No Data" << endl;
    }
}

bool queue::empty() {
    if (front == rear) {
        return true;
    }
    else {
        return false;
    }
}

bool queue::full() {
    if (size == 5+1) {
        return true;
    }
    else {
        return false;
    }
}

void queue::print() {
    for (int i = front; i < rear; i++) {
        cout << element[i] << endl;
    }
}

int main() {
    queue q;
    int com, val;
    for (int i = 0; i < 15;i++) {
        cout << "-----------------------------------------" << endl;
        cout << "1. Enqueue // 2.Dequeue // 3. print" << endl;
        cout << "-----------------------------------------" << endl;
        cin >> com;
        if (com == 1) {
            cin >> val;
            q.enqueue(val);
        }
        if (com == 2) {
            q.dequeue();
        }
        if (com == 3) {
            q.print();
        }
    }
}

data-structure

2022-09-22 19:36

1 Answers

If the array size is 5, 5 elements are included and the index ranges from 0 to 4. If front and are are the same and the queue is empty, the queue can have up to four elements.

There will be a problem if 6 values go into the queue, but I don't think more than 7 will go in, so in what order did you put the values? Or what did you see to determine that there were more than seven?

And looking at the code, it seems like Linkyu, but the implementation is wrong.

As shown below, we need to add processing for cases where lear and front are out of range.

void queue::enqueue(int value) {
    if (!full()) {
        cout << "Front Rear" << front << " " << rear << endl;
        element[rear] = value;
        rear++;
        rear %= sizeof(element) / sizeof(element[0]);
        size++;
    }
}

void queue::dequeue() {
    if (!empty()) {
        element[front] = 0;
        front++;
        front %= sizeof(element) / sizeof(element[0]);
        size--;
    }
    else {
        cout << "No Data" << endl;
    }
}

bool queue::empty() {
    return front == rear;
}

bool queue::full() {
    int next = rear + 1;
    next %= sizeof(element) / sizeof(element[0]);
    return next == front;
}

void queue::print() {
    int i = front;
    while (i != rear) {
        cout << element[i] << endl;
        ++i;
        i %= sizeof(element) / sizeof(element[0]);
    }
}


2022-09-22 19:36

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.