#include <iostream>

 

using namespace std;

 

class Node{ // Node Class

public:

        Node(int n);

        Node* nextNode;

        Node* prevNode;

        int data;

};

 

Node::Node(int n)

{

        data = n;

        nextNode = NULL;

        prevNode = NULL;

}

 

class Queue{ // Queue

public:

        Queue();

        void enQueue(int n);

        void deQueue();

        void printQueue();

        int QueueSize();

private:

        int size;

        Node* front;

        Node* rear;

};

Queue::Queue()

{

        size = 0;

        front = NULL;

        rear = NULL;

}

void Queue::enQueue(int n)

{

        Node* NewNode = new Node(n);

        if(size==0) // r == f

        {

               front = NewNode;

               rear = NewNode;

        }

        else // r != f

        {

               Node* temp = rear;

               rear = NewNode;

               rear->nextNode = temp;

               temp->prevNode = rear;

        }

        size++;

}

void Queue::deQueue()

{

        Node* temp = front;

        front = temp->prevNode;

        delete temp;

        size--;

}

 

int Queue::QueueSize() { return size; }

 

void Queue::printQueue()

{

        Node* temp = front;

        cout << "Queue List" << " (Queue Size : " << QueueSize() << " )" << endl;

        while(1)

        {

               cout << temp->data << endl;

               if(temp->prevNode == NULL)

                       break;

               temp = temp->prevNode;

        }

}

int main()

{

        Queue q;

        q.enQueue(3);

        q.enQueue(4);

        q.enQueue(5);

        q.deQueue();

        q.printQueue(); // 4 5

        q.enQueue(7);

        q.enQueue(2);

        q.deQueue();

        q.enQueue(1);

        q.printQueue(); // 5 7 2 1

        return 0;

}

 


신고

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

List로 구현한 Queue  (0) 2013.01.31
Binary Trees  (0) 2012.11.07
CircularArray(환형 배열, queue)  (0) 2012.10.24
간단한 리스트 구현(단방향)  (0) 2012.09.08
Linked List(링크드 리스트)  (0) 2012.03.24
큐(Queue)  (0) 2012.03.02

+ Recent posts