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 항상 행복하게^-^★

,

Array based STACK

What is it?

  • Array 기반의 Stack
  • 기본적으로 Push (Insert)Pop (Delete)연산이 가능한 FILO (First-In Last-Out) 자료구조이다.
  • 너무 간단한 코드라 자세한 설명은 생략한다.
  • FILO란? 먼저 들어온 자료가 마지막에 나가는 것으로, 쌓아논 책을 위에서부터 습득하는 것을 연상하면 된다.



CODE

/**************************************************** * Array 기반의 STACK ******************************* * *********************************** st.c ********** * **************************************************/ #include<stdio.h> /* 배열의 크기를 상수로 지정 */ #define MAX_BUFF 10 /* 배열에 들어가는 데이터 */ typedef struct sData { int skey; //키 char svalue; //값 } sData; /* 크기가 MAX_BUFF인 스택 선언 */ struct sData stack[MAX_BUFF]; /* Information for Top of Stack */ int sTop; /* Information for number of datas */ int sNum; // 데이터가 들어온 총 횟수 int countPop; // 삭제연산의 수 /* Stack Initiation */ void sInit() { /* Top을 1로, 나머지는 모두 초기화한다 */ sTop = 1; sNum = 0; countPop = 0; for(int i=0; i<MAX_BUFF; i++) { stack[i].skey = '\0'; stack[i].svalue = '\0'; } } /**************************************************************** * Push 후 출력과 Pop 후 출력을 구별하는 게 좀 더 깔끔해서 구별함 * * ***************************************************************/ /* Stack Print for Push */ void sPrintPush() { printf("\n Current stack's status is :\n\n"); printf(" KEY VALUE\n"); for(int i=sTop-1; i>=0; i--) { printf(" %d %s\n", stack[i].skey, &stack[i].svalue); } printf("\n"); printf("The number of data which you into : %d\n", sNum); printf("The number of data which current exist : %d\n\n", sNum-countPop); } /* Stack Print for Pop */ void sPrintPop() { printf("\n Current stack's status is :\n\n"); printf(" KEY VALUE\n"); for(int i=sTop-2; i>=0; i--) { printf(" %d %s\n", stack[i].skey, &stack[i].svalue); } printf("\n"); printf("The number of data which you into : %d\n", sNum); printf("The number of data which current exist : %d\n\n", sNum-countPop); } /* Stack Insert the data 'val' */ void sPush(char val) { if(sTop-1>=MAX_BUFF) { /* If the STACK is full */ printf("\nerror::stack overflow.\n"); } else { sNum++; stack[sTop-1].skey = sNum; stack[sTop-1].svalue = val; sPrintPush(); sTop++; } } /* Stack Delete the data of top */ void sPop() { if(sTop-1==0) { /* If the STACK is empty */ printf("\nerror::stack is already empty.\n"); } else { sTop--; printf("Poped data : %s\n", &stack[sTop].svalue); stack[sTop-1].skey = '\0'; stack[sTop-1].svalue = '\0'; countPop++; sPrintPop(); } } /* MAIN FUNCTION */ int main() { sInit(); printf("\n\nIt's an array based STACK.\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 push.\n"); printf("Press '2' Ifyou wanna pop.\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"); sInit(); sPrintPop(); break; case 1: printf("You pressed '1' to Push.\n"); printf("Press the character which you want to push. : "); scanf("%s", &push); sPush(push); break; case 2: printf("You pressed '2' to Pop.\n"); sPop(); 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 항상 행복하게^-^★

,