편집 기록

편집 기록
  • 프로필 유동욱님의 편집
    날짜2018.09.26

    클래스 배열선언 사이즈


    저가 배열을 이용한 큐를 선언했는데 제 생각에는 element의 사이즈가 5니까 총 6개의 엘리멘트가 들어 갈 수 있다고 생각하는데 배열에 6개 넘게 사이즈가 들어가더라고요 왜 그렇죠?

    #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();
            }
        }
    }
    
  • 프로필 알 수 없는 사용자님의 편집
    날짜2018.09.26

    클래스 배열선언 사이즈


    저가 배열을 이용한 큐를 선언했는데 제 생각에는 element의 사이즈가 5니까 총 6개의 엘리멘트가 들어 갈 수 있다고 생각하는데 배열에 6개 넘게 사이즈가 들어가더라고요 왜 그렇죠? ```사용하는_프로그래밍언어 C++

    include

    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(); } } }