배우고픈 공돌이

C++ Queue! 본문

C, C++ /C++

C++ Queue!

내 마음 아홉수 2017. 10. 16. 16:19

queue.h


#ifndef QUEUE_H

#define QUEUE_H


class Queue{

private:

int* pArr;

int  rear;

int  front;

int  size;

public:

Queue(int size);

~Queue();

void push(int data);

int pop();

};


#endif


queue.cpp


#include "queue.h"

#include <cassert>


Queue::Queue(int size)

{

//this-> pArr = (int*) malloc(sizeof(int)*size);

this->pArr = new int[size];

assert( this->pArr );


this-> size = size;

this-> front =0;

this-> rear = 0;


}


Queue::~Queue()

{

//free( this->pArr );

delete [] this->pArr;

}


void Queue::push(int data)

{

assert( this-> rear != this->size);

this->pArr[(this->rear)++] = data;

}


int Queue::pop()

{

assert( this->rear != this->front );

return this->pArr[(this->front)++];

}



'C, C++ > C++' 카테고리의 다른 글

c++ reference!  (0) 2017.10.16
C++ linked list!  (0) 2017.10.16
C++ stack!  (0) 2017.10.16
C를 C++로 변경하기. (2)  (0) 2017.10.12
C를 C++로 변경하기.  (0) 2017.10.12
Comments