Recent Posts
printf("ho_tari\n");
Queue 본문
<main.cpp>
//#include <stdio.h>
#include <iostream>
#include "queue.h"
int main()
{
Queue q1(100), q2(10);
//initQueue(&q1, 100);
//initQueue(&q2, 10);
q1.push(100);
q1.push(200);
q1.push(300);
//printf("q1 1st pop() : %d\n", pop(&q1));
//printf("q1 2nd pop() : %d\n", pop(&q1));
//printf("q1 3rd pop() : %d\n", pop(&q1));
std::cout << "q1 1st pop() : " << q1.pop() << std::endl;
std::cout << "q1 2nd pop() : " << q1.pop() << std::endl;
std::cout << "q1 3rd pop() : " << q1.pop() << std::endl;
q2.push(900);
q2.push(800);
q2.push(700);
//printf("q2 1st pop() : %d\n", pop(&q2));
//printf("q2 2nd pop() : %d\n", pop(&q2));
//printf("q2 3rd pop() : %d\n", pop(&q2));
std::cout << "q2 1st pop() : " << q2.pop() << std::endl;
std::cout << "q2 2nd pop() : " << q2.pop() << std::endl;
std::cout << "q2 3rd pop() : " << q2.pop() << std::endl;
//cleanupQueue(&q1);
//cleanupQueue(&q2);
return 0;
}
<queue.h>
#ifndef QUEUE_H
#define QUEUE_H
class Queue {
private:
int *pArr;
int size;
int front;
int rear;
public:
//void initQueue(Queue *pq, int size);
//void cleanupQueue(Queue *pq);
Queue(int size);
~Queue();
void push(int data);
int pop();
};
#endif
<queue.cpp>
//#include <stdlib.h>
#include <cassert>
#include "queue.h"
Queue::Queue(int size)
{
//this->pArr = malloc(sizeof(int) * size);
this->pArr = new int[size];
assert(this->pArr);
this->size = size;
this->front = 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;
++this->rear;
}
int Queue::pop()
{
assert(this->front != this->rear);
int index = this->front;
++this->front;
return this->pArr[index];
}
<compile 결과>
'C++' 카테고리의 다른 글
String2 (0) | 2023.09.01 |
---|---|
Rational3 (여러연산자) (0) | 2023.08.31 |
String (0) | 2023.08.31 |
Stack (0) | 2023.08.31 |
Complex3 (여러연산자) (0) | 2023.08.31 |