게시판 즐겨찾기
편집
드래그 앤 드롭으로
즐겨찾기 아이콘 위치 수정이 가능합니다.
심심해서 큐 만들어봄
게시물ID : programmer_5825짧은주소 복사하기
작성자 : 황금비
추천 : 2
조회수 : 407회
댓글수 : 0개
등록시간 : 2014/10/07 18:26:14
아래 어떤 분이 큐 문제 올려서 그냥 만들어 보았습니다.

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

typedef int              INT;
typedef unsigned int     UINT;
#define FALSE 0
#define TRUE  1

#define DATA_SIZE 36
#define MAX_H 25
#define MAX_QUE (DATA_SIZE * MAX_H)

#ifdef __cplusplus
struct TQUEUE
#else
typedef struct tagQueue
#endif
{
INT  front; // front index
INT  rear; // rear index
INT  count; // stored count
UINT que[DATA_SIZE * MAX_H]; // que buffer

#ifdef __cplusplus
TQUEUE(); // constructor

void Init(); // initialize the queue
INT  PushBack(UINT* buf, INT size); // return the copied size to que buffer
INT  PopFront(UINT* buf, INT size, INT bREMOVE= TRUE); // peek from the que buffer. return the count
INT  Copy(UINT* buf, INT size); // copy the que to buf. return the copied size
INT  Size() { return count; } // return the stored data
#endif

#ifdef __cplusplus
};
#else
} TQUEUE;
#endif

#ifdef __cplusplus
TQUEUE::TQUEUE()
{
front = 0;
rear  = 0;
count = 0;
memset(que, 0, sizeof(UINT)* MAX_QUE);
}
#endif

#ifdef __cplusplus
void TQUEUE::Init()
#else
void Queue_Init(_this)
TQUEUE* _this;
#endif
{
#ifdef __cplusplus
TQUEUE* _this  = this;
#endif

_this->front = 0;
_this->rear  = 0;
_this->count = 0;
memset(_this->que, 0, sizeof(UINT)* MAX_QUE);
}

#ifdef __cplusplus
int TQUEUE::PushBack(UINT* buf, INT size) // return the copied size to que buffer
#else
int Queue_PushBack(_this, buf, size) // return the copied size to que buffer

TQUEUE* _this;
UINT* buf;
INT size;
#endif
{
#ifdef __cplusplus
TQUEUE* _this  = this;
#endif

INT i=0;

for(i=0; i<size; ++i)
{
if(MAX_QUE == _this->count)
return i;

*(_this->que+_this->rear) = *(buf + i);
++_this->rear; // move the _this->rear index
_this->rear %= MAX_QUE; // reset the index

++_this->count; // increase count
}

return i;
}

#ifdef __cplusplus
INT TQUEUE::PopFront(UINT* buf, INT size, INT bREMOVE)
#else
INT Queue_PopFront(_this, buf, size, bREMOVE)
TQUEUE* _this;
UINT* buf;
INT size;
INT bREMOVE;
#endif
{
#ifdef __cplusplus
TQUEUE* _this  = this;
#endif

INT i=0;
INT t_front = 0;
INT t_count = 0;

if(0 == _this->count)
return 0;

if(bREMOVE)
{
for(i=0; i<size; ++i)
{
if(0 == _this->count)
return i;

*(buf + i) = *(_this->que + _this->front);
++_this->front; // move the rear index
_this->front %= MAX_QUE; // reset the index

--_this->count; // increase count
}

return i;
}


t_front = _this->front;
t_count = _this->count;
for(i=0; i<size; ++i)
{
if(0 == t_count)
return i;

*(buf + i) = *(_this->que + t_front);
++t_front; // move the rear index
t_front %= MAX_QUE; // reset the index

--t_count; // increase count
}

return i;
}

#ifdef __cplusplus
INT TQUEUE::Copy(UINT* buf, INT size)
#else
INT Queue_Copy(_this, buf, size)
TQUEUE* _this;
UINT* buf;
INT size;
#endif
{
#ifdef __cplusplus
TQUEUE* _this  = this;
#endif

INT i=0;
INT t_front = _this->front;
INT t_count = _this->count;

for(i=0; i<size; ++i)
{
if(0 == t_count)
return i;

*(buf + i) = *(_this->que + t_front);
++t_front; // move the rear index
t_front %= MAX_QUE; // reset the index

--t_count; // increase count
}

return i;
}

int main(int argc, char** argv)
{
INT number;
INT outnum;
INT random;

UINT arr_i[DATA_SIZE+4] ={0};
UINT arr_o[DATA_SIZE+4] ={0};
INT  rcv  =0;
INT  i =0;
INT  size = 0;

TQUEUE qh;
srand((unsigned)time(NULL));

#ifdef __cplusplus
qh.Init();
#else
Queue_Init(&qh);
#endif


for(outnum=0;outnum<10000;outnum++)
{
size = 4 + rand()% DATA_SIZE;

for(number=0;number<size;number++)
{
random = rand(); // 난수생성
arr_i[number]=random%100; // 임시배열에 난수 넣기
}

printf("add : %2d :: ", size);
for(i=0; i<size; ++i)
printf("%2d ", arr_i[i]);

printf("\n");


// test ...
#ifdef __cplusplus
qh.PushBack(arr_i, size); // temp를 큐에 넣기
rcv = qh.PopFront(arr_o, size, TRUE);
#else
Queue_PushBack(&qh, arr_i, size); // temp를 큐에 넣기
rcv = Queue_PopFront(&qh, arr_o, size, TRUE);
#endif


printf("peek: %2d :: ", size);
for(i=0; i<rcv; ++i)
printf("%2d ", arr_o[i]);

printf("\n");
}
return 0;
}

전체 추천리스트 보기
새로운 댓글이 없습니다.
새로운 댓글 확인하기
글쓰기
◀뒤로가기
PC버전
맨위로▲
공지 운영 자료창고 청소년보호