본문 바로가기

과제모음

[C++]Queue 구현

반응형

>Queue.h<
#ifndef QUEUE_H
#define QUEUE_H

class queue{
public:
    queue();
    bool IsEmpty();
    bool IsFull();
    void Push(const int x);
    int Pop();
    void DisplayQueue();

private:
    int front, rear;
    int data[6];
};

#endif

> main.cpp <

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include "queue.h"

int main()
{
    queue FIFO;
    int sel, input;    
    while(1){
        cout << endl << endl;
        cout << "1.PUSH QUEUE" << endl << "2.POP QUEUE" << endl <<
             "3.Display QUEUE" << endl << "4.EXIT QUEUE" << endl;
        cout << "Select Number [ ]\b\b";
        cin >> sel;

        switch(sel){
        case 1:
            cout << "Input QUEUE data : ";
            cin >> input;
            FIFO.Push(input);
            break;
        case 2:
            FIFO.Pop();
            break;    
        case 3:
            FIFO.DisplayQueue();
            break;
        case 4:
            cout << "Exit Queue Now....."<<endl;
            return 0;
        default:
            cout << "You select wrong number" << endl;
            return 0;
        }
    }
}
        
> queue.cpp<

#include <iostream>
using std::cin;
using std::cout;
using std::endl;

#include "queue.h"

queue::queue()
{
    rear = 0;
    front = 0;
    data[6] = 0;
}
bool queue::IsEmpty()
{
    if(front >= rear)
        return true;
    else
        return false;
}

bool queue::IsFull()
{
    if(rear>5)
        return true;
    else
        return false;
}

void queue::Push(const int x)
{
    if(!IsFull()){
        data[rear] = x;
        rear++;
    }
    
    else
        cout << "Queue is full" << endl;
}

int queue::Pop()
{
    int pop=0;
    if(!IsEmpty()){
        pop = data[front];
        data[front] = 0;
        front++;
    }

    else
        cout << "Queue is empty" << endl;

        rear =0;

        front = 0;

    return pop;
}

void queue::DisplayQueue()
{
    for(int i =front; i<rear; i++){
        cout << "Queue [ " << i << " ] : " << data[i] << endl;
    }
}

반응형