Array based QUEUE

What is it?

  • Array 기반의 Queue
  • 기본적으로 Enqueue (Insert)Dequeue (Delete) 연산이 가능한 FIFO (First-In, First-Out) 자료구조이다.
  • 너무 간단한 코드라 자세한 설명은 생략한다.
  • FIFO란? 먼저 들어온 자료가 먼저 나가는 것으로, 줄을 서다가 입장하는 것을 연상하면 된다.


CODE

/************************************************** * Array 기반의 Queue ***************************** * ************************************ q.c ******** * *************************************************/ #include <stdio.h> #define MAX_BUFF 10 /* Data for Queue */ typedef struct qData { int qkey; // 키 char qvalue; // 값 } qData; struct qData queue[MAX_BUFF]; /* Information for Queue */ int qTail; /* 삽입되는 부분에 대한 정보 */ int qNum; /* 입력받은 총 데이터 수 */ int countDe; /* 현재 데이터의 개수를 위한 디큐 횟수 */



/* * Dequeue시 Head의 데이터를 삭제하는 * 동시에 모든 데이터를 한칸씩 앞으로 * 옮긺으로써 Head를 따로 선언하지 않음. */

/* Queue Initiation */ void qInit() { qTail = 1; qNum = 0; countDe = 0; for(int i=0; i<MAX_BUFF; i++) { queue[i].qkey = '\0'; queue[i].qvalue = '\0'; } } /* Queue Print for Enqueue */ void qPrintEn() { int i; /* variable for 'for clause' */ printf("\n Current Queue's status is :\n\n"); printf(" KEY VALUE\n"); for( i = qTail-1 ; /*qTail-1*/ i >= 0; /*qHead-1*/ i--) { printf(" %d %s\n", queue[i].qkey, &queue[i].qvalue); } printf("\n"); printf("The number of data which you into : %d\n", qNum); printf("The number of data which current exist : %d\n\n", qNum-countDe); } /* Queue Print for Dequeue */ void qPrintDe() { int i; /* variable for 'for clause' */ printf("\n Current Queue's status is :\n\n"); printf(" KEY VALUE\n"); for( i = qTail-3 ; /* qTail-2 */ i>=0; /* qHead */ i--) { printf(" %d %s\n", queue[i].qkey, &queue[i].qvalue); } printf("\n"); printf("The number of data which you into : %d\n", qNum); printf("The number of data which current exist : %d\n\n", qNum-countDe); } /* Queue Insert the data 'val' */ void enQ(char val) { if ( qTail >= 11 ) { /* If the Queue is full */ printf("\nerror::queue overflow.\n"); } else { qNum++; queue[qTail-1].qkey = qNum; queue[qTail-1].qvalue = val; qPrintEn(); qTail++; } } /* Queue Delete the data from head */ void deQ() { if( qTail == 1 ) { /* If the Queue is empty */ printf("\nerror::queue is already empty.\n"); } else { countDe++; printf("Deleted data : %s\n", &queue[0].qvalue); queue[0].qkey = '\0'; queue[0].qvalue = '\0'; for(int i=1; i<qTail; i++) { queue[i-1].qkey = queue[i].qkey; queue[i-1].qvalue = queue[i].qvalue; } qPrintDe(); qTail--; } } /* MAIN FUNCTION */ int main() { qInit(); printf("\n\nIt's an array based Queue.\n\n"); int menu; char push; while(menu!=3) { printf("\nYou make a choice to menu.\n\n"); printf("Press '0' If you wanna Initiation.\n"); printf("Press '1' If you wanna Enqueue.\n"); printf("Press '2' Ifyou wanna Dequeue.\n"); printf("Press '3' If you wanna exit.\n\n"); printf(" PRESS : "); scanf("%d", &menu); switch(menu) { case 0: printf("You pressed '0' to Initiation.\n"); printf("So, the stack is cleared.\n"); qInit(); qPrintDe(); break; case 1: printf("You pressed '1' to Enqueue.\n"); printf("Press the character which you want to Enqueue. : "); scanf( "%s", &push ); enQ(push); break; case 2: printf("You pressed '2' to Dequeue.\n"); deQ(); break; case 3: printf("Exit......\n\n"); break; default: printf("It's wrong number. press again.\n\n"); break; } // switch } // while return 0; }


블로그 이미지

차트

소소한 일상 C코드 DB 항상 행복하게^-^★

,