한 걸음 두 걸음

C++ 원형 덱 본문

CSE/Data Structure

C++ 원형 덱

언제나 변함없이 2019. 11. 6. 17:23
반응형
#include <cstdio>
#include <cstdlib>
const int MAX_QUEUE_SIZE = 100;

class CircularQueue {
protected:
    int front;
    int rear;
    int data[MAX_QUEUE_SIZE];

public:
    CircularQueue() {
        front = rear = 0;
    }
    void enqueue(int element) {
        if (!isFull()) {
            rear = (rear + 1) % MAX_QUEUE_SIZE;
            data[rear] = element;
        }
    }

    int dequeue() {
        if (!isEmpty()) {
            front = (front + 1) % MAX_QUEUE_SIZE;
        }
        return data[front];
    }

    bool isFull() {
        return (rear + 1) % MAX_QUEUE_SIZE == front;
    }
    bool isEmpty() {
        return front == rear;
    }
    int peek() {
        if (!isEmpty()) {
            return data[(front + 1) % MAX_QUEUE_SIZE];
        }
    }
    void display() {
        int max = front < rear ? rear : rear + MAX_QUEUE_SIZE;
        for (int i = front + 1; i <= max; i++) {
            printf("%2d / ", data[i % MAX_QUEUE_SIZE]);
        }
    }

};

class CircularDeque :public CircularQueue{
public:
    CircularDeque() {}
    void addRear(int element) {
        enqueue(element);
    }
    int deleteFront() {
        return dequeue();
    }
    int getFront() {
        return peek();
    }

    //deque추가기능
    void addFront(int element) {
        if (!isFull()) {
            data[front] = element;
            front = (front - 1 + MAX_QUEUE_SIZE) % MAX_QUEUE_SIZE;
        }
    }

    int deleteRear() {
        if (!isEmpty()) {
            int deletedElement = data[rear];
            rear = (rear - 1 + MAX_QUEUE_SIZE) % MAX_QUEUE_SIZE;
            return deletedElement;

        }
    }

    int getRear() {
        if (!isEmpty()) {
            return data[rear];
        }
    }

    //@Override
    void display() {
        int max = front < rear ? rear : rear + MAX_QUEUE_SIZE;
        for (int i = front + 1; i <= max; i++) {
            printf(" %2d ", data[i % MAX_QUEUE_SIZE]);
        }
    }

};

void main() {
    CircularDeque deque;
}

반응형

'CSE > Data Structure' 카테고리의 다른 글

자료구조 / 인접리스트를 활용한 그래프 (C++)  (0) 2019.11.13
C++ 선형 큐  (0) 2019.11.06
C++ 환형 큐  (0) 2019.11.06
C++ 스택  (0) 2019.11.06
자료구조 맵 ] Map / Collection  (0) 2019.03.27