๐ 1. ๋ฌธ์ ์ค๋ช
-Version #1

๐ 2. ์์ค์ฝ๋ + ์ฃผ์
๐ Visual Studio ๋ฒ์
#include<malloc.h>
#include<stdlib.h>
#define TRUE 1;
#define FALSE 0 // ์ฐธ/๊ฑฐ์ง์ ๊ตฌ๋ถํ๊ธฐ์ํ ์ ์
typedef struct _node Node; //๊ตฌ์กฐ์ฒด Node, Queue์์ฑ
struct _node
{
int data;
Node *next;
};
typedef struct _queue {
Node *head; //Queue์ ์ญํ ์ ๋
ธ๋์ ๋จธ๋ฆฌ์ ๊ผฌ๋ฆฌ๋ฅผ ์ฐ๊ฒฐ
Node *tail;
}Queue;
typedef int element;
element createQueue(Queue * q) //๋์ ๋ฉ๋ชจ๋ฆฌ ํ ๋น malloc ์ฌ์ฉ
{
if (q == NULL) { //q์ ์๋ฌด๊ฒ๋ ์์๋ 0์ผ๋ก ๋ฆฌํด
return FALSE ;
}
q->head = (Node*)calloc(1, sizeof(Node));// Node์ ์ฌ์ด์ฆ ๋งํผ 1๊ฐ์ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ ํ ๋น
if (q->head == NULL) {
return FALSE;
}
q->tail = (Node*)calloc(1, sizeof(Node)); // ์์ ๋ด์ฉ๊ณผ ๋์ผ
if (q->tail == NULL) { //q์ ๊ผฌ๋ฆฌ๊ฐ NULL์ด๋ฉด q->head๋ฅผ freeํ 0์ผ๋ก ๋ฆฌํด
free(q->head);
return FALSE;
}
else {
q->head->next = q->tail; //ํ ๋น์ด ๋์ด์๋ค๋ฉด q์ ํค๋์ ๋ฅ์คํธ๋ฅผ q์ ๊ผฌ๋ฆฌ์ ์ฐ๊ฒฐํ์ฌ ํํ ๊ตฌํ
q->tail->next = q->tail; //๋ํ q์ ๊ผฌ๋ฆฌ์ ๋ฅ์คํธ์ q์ ๊ผฌ๋ฆฌ๋ฅผ ์ฐ๊ฒฐํ ๋ค 1๋ก ๋ฆฌํด
return TRUE;
}
}
element isQueueEmpty(const Queue *q)//๊ตฌ์ด์ฒด ์์ด ์๋ฌด๊ฒ๋ ์๋ ์ํ์ธ์ง ํ์ธ
{
if (q == NULL) {
return FALSE;
}
if (q->head->next == q->tail) {
return TRUE;
}
else {
return FALSE;
}
}
element enqueue(Queue * q, int enqueData) //๋ฐ์ดํฐ ๋ฃ๊ธฐ
{
Node*cur;
if (q == NULL) {
return FALSE;
}
Node* np = (Node*)(calloc(1, sizeof (Node)));
if (np == NULL) {
return FALSE;
}
cur = q->head;
while (cur->next != q->tail)
cur = cur->next;
cur->next = np;
np->next = q->tail;
np->data = enqueData;
return TRUE;
}
element dequeue(Queue * q, int * dequeData) //๋ฐ์ดํฐ ์ถ๋ ฅ ํ ์ญ์
{
Node*cur;
if (q == NULL) { //๊ตฌ์ด์ฒด๊ฐ NULL์ด๋ฉด ๋น์ด์์์ ๋ํ๋ธ๋ค
printf("EMPTY\n");
return FALSE;
}
if (isQueueEmpty(q)) {
return FALSE;
}
else {
*dequeData = q->head->next->data;
cur = q->head->next;
q->head->next = q->head->next->next;
free(cur);
return TRUE;
}
}
void printQueue(const Queue * q)
{
Node*curp;
int res;
if (q == NULL) {
return;
}
printf("ํ :");
curp = q->head->next;
while (curp != q->tail) {
printf(" %d - ", curp->data);
curp = curp->next;
}
res=isQueueEmpty(q);//๋น์ด์์์ ํ์ธํ ํ ๋ฆฌํด๊ฐ์ ๋ฐ๋ผ NULL์ถ๋ ฅ
if(res==1){
printf("NULL");
}
printf("\n\n");
}
void destroyQueue(Queue * q)// ๋จ์์๋ ๋ฐ์ดํฐ free
{
Node*curp;
if (q == NULL) {
return;
}
curp = q->head->next;
while (curp != q->tail) {
q->head->next = q->head->next->next;
free(curp);
curp = q->head->next;
}
free(q->head);
free(q->tail);
q->head = q->tail = NULL;
printf("BYE\n");
}
void input(Queue *qp)// enqueue ๋ฐ์ดํฐ ์
๋ ฅ
{
int enqueData;
if (scanf("%d", &enqueData) != 1) {
}
if (!enqueue(qp, enqueData))
printf("enqueue ์คํจ!!\n");
printQueue(qp);
}
void myDelete(Queue *qp)//dequeue ๋ฐ์ดํฐ ์
๋ ฅ
{
int i;
int cnt;
int dequeData;
int res;
if(isQueueEmpty(qp)){// ๊ตฌ์กฐ์ฒด ์์ด NULL์ผ๋ EMPTY์ถ๋ ฅ
printf("EMPTY\n");
printQueue(qp);//NULL ์ถ๋ ฅ
return;//๋ฉ์ธ์ผ๋ก ๋์๊ฐ
}
scanf("%d", &dequeData);
res = dequeue(qp, &dequeData);
if (res == 1) //NULL์ด ์ใ
ฃ๋ฉด ์ถ๋ ฅ์ผ๋ก ์ด๋/๋ฐ์ดํฐ ์ญ์
{
}
/*else
printf("Empty\n");*/
printQueue(qp);
}
int menu(const char **menuList, int menuCnt) // ๋ฌด์จ ์ต์
์ด ์๋์ง ๋ณด์ฌ์ฃผ๋ ์ํ
{
int i;
int menuNum = 0;
int res;
for (i = 0; i < menuCnt; i++)
{
printf("%d. %s\n", i + 1, menuList[i]);
}
while (menuNum<1 || menuNum>menuCnt)
{
printf("์
๋ ฅ:");
res = scanf("%d", &menuNum);
if (res != 1)
{
continue;
}
}
return menuNum;
}
int main()
{
Queue q;
const char *menuList[] = { "Enqueue", "Dequeue", "์ข
๋ฃ" };
int menuCnt;
int menuNum;
createQueue(&q);
menuCnt = sizeof(menuList) / sizeof(menuList[0]);
while (1)
{
menuNum = menu(menuList, menuCnt);
if (menuNum == menuCnt)
{
break;
}
switch (menuNum)
{
case 1:
{
input(&q);
break;
}
case 2:
{
myDelete(&q);
break;
}
}
}
destroyQueue(&q);
return 0;
}
๐ก -Linux ๋ฒ์






๐ 3.์คํ ํ๋ฉด ์บก์ณ


โจ 4. ๋๋์
์ด๋ฒ ์๋ฃ๊ตฌ์กฐ list๋ฅผ ํ์ฉํ Queue๊ตฌํ ํด์ฆ๋ฅผ ํ์ด๋ณด๋ฉด์ ๊ธฐ์กด์ ์์
์๊ฐ์ ๋ฐฐ์ ๋ ๋จ์ผ์ฐ๊ฒฐ ๋ฆฌ์คํธ์ ์๋ฆฌ์ FIFO ๊ตฌ์กฐ๋ฅผ ์ง๋ Queue์ ์๋ฆฌ๋ฅผ ํตํด ํจ์๋ค์ ๊ธฐ๋ฅ์ ๊ตฌํํ์๊ณ , ์ฃผ์ด์ง ์ถ๋ ฅ๊ฐ์ ๋ณด๊ธฐ์ ์
๋ ฅ์ ๋์ผํ๊ฒ ํ๊ธฐ์ํด ๋ฉ์ธ ํจ์๋ฅผ ์๋กญ๊ฒ ๋ง๋ค์๋ค. ์ด๋ฒ ํด์ฆ๋ ์ด์ ํด์ฆ์ ๋นํด ์ฃผ์ด์ง ์ถ๋ ฅ๊ณผ ๊ฐ๊ฒ ๋ฉ์ธํจ์๋ฅผ ๋ง๋๋๊ฒ๋ ๋์ด๋๊ฐ ์์๊ณ , ํด์ฆ๋ฅผ ํ๋ฉด์ ๋ง์ง๋ง Dequeueํ ๋์ ๋ฌธ์ ์ ๋ ๋ง์ด ์๊ฒผ๋ค. ํ์ง๋ง ๋๊น์ง ํฌ๊ธฐํ์ง ์๊ณ Queue๋ฅผ ๊ตฌ์ฑํ๋ ๋ช๊ฐ์ง ํจ์๋ค์ ์์ ํ๊ณ , ๋ค์ํ ๋ฐฉ๋ฒ์ผ๋ก ์ ๊ทผํ์ฌ ๋๋ด ์ด ๋ฌธ์ ๋ฅผ ํ์ด๋๋ค. ๋๋ ์ด๋ฒ ํด์ฆ๋ฅผ ํ์ด๋ณด๊ณ ์ด๋ฒ ํ๊ธฐ์ ๋ด์ฉ์ ์ ๋ฆฌํ๋ ๊ฐ์ฅ ์ค์ํ ํด์ฆ๋ผ๊ณ ์๊ฐํ์๊ณ , ์ด๋ ๊ฒ ํด์ฆ๋ฅผ ํ์ด๋ด์ผ๋ก์จ ์ด๋ฐ ํ๊ธฐ๋์ ์ค๋ ฅ์ ๋นํด ๋ง์ ๋ฐ์ ์ด ์์๋ค๊ณ ์๊ฐํ์์ผ๋ฉฐ, ์์ผ๋ก ์ด์ธ์ ๋ค์ํ ๋ฌธ์ ๋ฅผ ํ์ด๋ด์ผ๋ก์จ ์ค๋ ฅ์ ๋ ํค์์ผ๊ฒ ๋ค๋ ์๊ฐ์ ํ๋ ๊ณ๊ธฐ๊ฐ ๋์๋ค.