티스토리 뷰

자료구조

원형 큐 구현 연습

심재철 2018. 8. 10. 21:30
#include <iostream>
using namespace std;

class queue{
private:
int size;
int start;
int end;
int* arr;
public:
bool isempty();
bool isfull();
void push(int num);
void pop();
void printque();
int front(){
return arr[start];
}
int get(int index){
return arr[index];
}

queue(int size){
this->size = size;
arr = new int[size];
start = end = 0;
}
void printarr(){
printf("start %d end %d\n",start+1,end);
for(int i=0; i<size; i++)
cout<<arr[i]<<" ";
cout<<endl;
}
};

void queue::printque(){
for(int i=(start+1)%size; i != end ; i = (i+1)%size)
cout<<arr[i]<<" ";
cout<<arr[end]<<" ";
cout<<endl;
}

bool queue::isempty(){
return start == end;
}

bool queue::isfull(){
return (end+1)%size == start%size;
}

void queue::push(int num){
if(isfull()){
cout<<"큐가 꽉찼습니다"<<endl;
return;
}
end = (end+1)%size;
arr[end] = num;
}

void queue::pop(){
if(isempty()){
cout<<"큐가 비었습니다"<<endl;
return;
}
start = (start+1)%size;
}


int main(){
queue* que = new queue(5);
que->push(5);
que->printque();
que->push(3);que->push(2);
que->printque();
que->push(1);
que->printque();
que->pop();
que->pop();
que->printque();
que->push(100);
que->pop();
que->printque();

return 0;
}


댓글
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
글 보관함