queue.c

1.###

#include <stdio.h>

int queue[100];
int front; // 머리
int rear;  // 꼬리

void push(int data) // queue 배열에 data를 저장
{
queue[rear] = data; // 꼬리에 data를 넣고
++rear; // 꼬리의 위치 1 증가
}

int pop(void) // queue 배열에서 data를 뽑아냄
{
int tmp = queue[front];
++front;

return tmp;
}

int main(void)
{
push(100);
push(200);
push(300);

printf("1st pop() : %d\n", pop());
printf("2rd pop() : %d\n", pop());
printf("3rd pop() : %d\n", pop());

return 0;
}

###

2.###
// Queue 구조체 자료형 만듬

@main.c

#include <stdio.h>
#include "queue.h"

int main(void)
{
Queue q1, q2;

initQueue(&q1); // Queue 구조체 자료형 q1 초기화
initQueue(&q2);

push(100, &q1);
push(200, &q1);
push(300, &q1);

printf("q1 1st pop() : %d\n", pop(&q1));
printf("q1 2rd pop() : %d\n", pop(&q1));
printf("q1 3rd pop() : %d\n", pop(&q1));

push(400, &q2);
push(500, &q2);
push(600, &q2);

printf("q2 1st pop() : %d\n", pop(&q2));
printf("q2 2rd pop() : %d\n", pop(&q2));
printf("q2 3rd pop() : %d\n", pop(&q2));

return 0;
}

---

@queue.h

#ifndef QUEUE_H
#define QUEUE_H
#define QUEUESIZE 100

typedef struct {

int array[QUEUESIZE];
int front;
int rear;

} Queue;

void initQueue(Queue* pq);
void push(int data, Queue* pq);
int pop(Queue* pq);

#endif

---

@queue.c

#include "queue.h"

void initQueue(Queue* pq)
{
pq->front = 0;
pq->rear = 0;
}

void push(int data, Queue* pq)
{
pq->array[pq->rear] = data;
++pq->rear;
}

int pop(Queue* pq)
{
int tmp = pq->array[pq->front];
++pq->front;

return tmp;
}

###

3.###

@main.c

#include <stdio.h>
#include "queue.h"

int main(void)
{
Queue q1, q2;

initQueue(&q1, 1);
initQueue(&q2, 20);

push(&q1, 100);
push(&q1, 200);
push(&q1, 300);

printf("q1 1st pop() : %d\n", pop(&q1));
printf("q1 2rd pop() : %d\n", pop(&q1));
printf("q1 3rd pop() : %d\n", pop(&q1));

push(&q2, 400);
push(&q2, 500);
push(&q2, 600);

printf("q2 1st pop() : %d\n", pop(&q2));
printf("q2 2rd pop() : %d\n", pop(&q2));
printf("q2 3rd pop() : %d\n", pop(&q2));
printf("q2 4rd pop() : %d\n", pop(&q2));

cleanupQueue(&q1);
cleanupQueue(&q2);
return 0;
}

---

@queue.h

#ifndef QUEUE_H
#define QUEUE_H

typedef struct {

int* pArr;
int size;
int front;
int rear;

} Queue;

void initQueue(Queue* pq, int size);
void cleanupQueue(Queue* pq);
void push(Queue* pq, int data);
int pop(Queue* pq);

#endif

---

@queue.c

#include <stdio.h>
#include <stdlib.h>
#include "queue.h"

void initQueue(Queue* pq, int size)
{
pq->pArr = (int*)malloc(sizeof(int) * size);
pq->size = size;
pq->front = 0;
pq->rear = 0;
}

void cleanupQueue(Queue* pq)
{
free(pq->pArr);
}

void push(Queue* pq, int data)
{
if(pq->rear >= pq->size)
{
printf("Queue is full\n");
return;
}
pq->pArr[pq->rear] = data;
++pq->rear;
}

int pop(Queue* pq)
{
if(pq->front == pq->rear)
{
printf("Queue is empty\n");
return -2;
}
int tmp = pq->pArr[pq->front];
++pq->front;

return tmp;
}

###

4.###
// 메모리 상의 모든 data는 시작주소와 size만 알면 접근할 수 있다.

@main.c

#include <stdio.h>
#include "queue.h"

int main(void)
{
Queue q1, q2;

initQueue(&q1, 3, sizeof(int));
initQueue(&q2, 20, sizeof(double));

int i = 100; push(&q1, &i);
i = 200; push(&q1, &i);
i = 300; push(&q1, &i);

pop(&q1, &i); printf("q1 1st pop() : %d\n", i);
pop(&q1, &i); printf("q1 2rd pop() : %d\n", i);
pop(&q1, &i); printf("q1 3rd pop() : %d\n", i);

double d = 40.1; push(&q2, &d);
d = 50.2; push(&q2, &d);
d = 60.3; push(&q2, &d);

pop(&q2, &d); printf("q2 1st pop() : %f\n", d);
pop(&q2, &d); printf("q2 2rd pop() : %f\n", d);
pop(&q2, &d); printf("q2 3rd pop() : %f\n", d);
pop(&q2, &d); printf("q2 4rd pop() : %f\n", d);

cleanupQueue(&q1);
cleanupQueue(&q2);
return 0;
}

---

@queue.h

#ifndef QUEUE_H
#define QUEUE_H

typedef struct {

void* pArr; // 어떤 type의 data가 들어올지 모르기 때문에 void*를 쓴다.
int eleSize; // type의 eleSize를 써주기위해 구조체 멤버로 선언해준다.
int size;
int front;
int rear;

} Queue;

void initQueue(Queue* pq, int size, int eleSize);
void cleanupQueue(Queue* pq);
void push(Queue* pq, const void* pData);
void pop(Queue* pq, void* pData);

#endif

---

@queue.c

#include <stdio.h>
#include <stdlib.h>  // malloc();
#include <string.h> // memcpy();
#include "queue.h"

void initQueue(Queue* pq, int size, int eleSize)
{
pq->pArr = malloc(eleSize * size);
pq->eleSize = eleSize;
pq->size = size;
pq->front = 0;
pq->rear = 0;
}

void cleanupQueue(Queue* pq)
{
free(pq->pArr);
}

void push(Queue* pq, const void* pData)
{
if(pq->rear >= pq->size)
{
printf("Queue is full\n");
exit(-1);
}
//pq->pArr[pq->rear] = *pData;
memcpy((unsigned char*)pq->pArr + pq->eleSize * pq->rear, pData,pq->eleSize);
++pq->rear;
}

void pop(Queue* pq, void* pData)
{
if(pq->front == pq->rear)
{
printf("Queue is empty\n");
exit(-2);
}
memcpy(pData, (unsigned char*)pq->pArr + pq->eleSize * pq->front,pq->eleSize);
++pq->front;
}

###

Comments