#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXQUEUE 6 /*큐의 길이*/
typedef struct{
char *queue[MAXQUEUE];
int front;
int rear;
} CQueueType;
CQueueType *CreatQueue(); /*Queue 만들기*/
int IsEmp(CQueueType *CQueue); /*Empty 인지 체크*/
int IsFull(CQueueType *CQueue); /*Full 인지 체크*/
void InsertQueue(CQueueType *CQueue); /*내용삽입*/
void DelItem(CQueueType *CQueue); /*Front의 항목을 삭제*/
char *RetItem(CQueueType *CQueue); /*Front의 항목을 반환*/
void PrintQueue(CQueueType *CQueue); /*Queue 안의 내용을 출력*/
char *InputItem(FILE *fInStr); /*아이템을 삽입*/
void isStringNum(char *cInStr); /*문자,숫자 확인*/
int main()
{
CQueueType *CQ1 = CreatQueue();
int iSelect;
while (1)
{
iSelect = 0;
system("cls");
printf("데이터를 5개까지 입력 가능한 Circular Queue 입니다\n");
printf("1. 데이터 삽입\n");
printf("2. 데이터 반환\n");
printf("3. 데이터 삭제\n");
printf("4. 데이터 확인\n");
printf("5. 종료\n");
printf("해당 값을 선택하십시오 : ");
scanf_s("%d", &iSelect);
fflush(stdin);
switch (iSelect)
{
case 1:
InsertQueue(CQ1);
break;
case 2:
printf("반환된 데이터는 : %s 입니다\n", RetItem(CQ1));
DelItem(CQ1);
break;
case 3:
DelItem(CQ1);
printf("데이터를 삭제하였습니다 \n");
break;
case 4:
printf("큐 내부의 데이터 입니다.\n");
PrintQueue(CQ1);
break;
case 5:
free(CQ1);
return 0;
default:
printf("입력값이 올바르지 않습니다.\n");
break;
}
system("pause");
}
}
CQueueType *CreatQueue()
{
CQueueType *CQueue;
CQueue = (CQueueType*)malloc(sizeof(CQueueType));
CQueue->front = 0;
CQueue->rear = 0;
return CQueue;
}
int IsEmp(CQueueType *CQueue) /*빈큐 확인*/
{
if (CQueue->front == CQueue->rear)
{
printf("Circular Queue is empty\n");
return 1;
}
else
{
return 0;
}
}
int IsFull(CQueueType *CQueue)
{
if (((CQueue->rear + 1) % MAXQUEUE) == CQueue->front)
{
printf("Circular Queue is Full\n");
return 1;
}
return 0;
}
void InsertQueue(CQueueType *CQueue)
{
char *ch=NULL;
if (IsFull(CQueue))
{
exit(1);
}
else
{
printf("데이터를 입력하십시오 : ");
CQueue->rear = (CQueue->rear + 1) % MAXQUEUE;
CQueue->queue[CQueue->rear] = InputItem(stdin);
}
}
void DelItem(CQueueType *CQueue)
{
if (IsEmp(CQueue))
{
exit(1);
}
else
{
CQueue->front = (CQueue->front + 1) % MAXQUEUE;
free(CQueue->queue[CQueue->front]);
}
}
char *RetItem(CQueueType *CQueue)
{
if (IsEmp(CQueue))
{
exit(1);
}
else
{
return CQueue->queue[CQueue->front + 1 % MAXQUEUE];
}
}
void PrintQueue(CQueueType *CQueue)
{
int iCount;
int iFirst;
int iLast;
iFirst = (CQueue->front + 1) % MAXQUEUE;
iLast = (CQueue->rear + 1) % MAXQUEUE;
printf("Circular Queue : ");
iCount = iFirst;
while (iCount != iLast)
{
printf("[%s] ", CQueue->queue[iCount]);
iCount = (iCount + 1) % MAXQUEUE;
}
printf("\n");
}
char *InputItem(FILE *fInStr)
{
char *cSring;
int iBuf;
int iLen = 1;
cSring = malloc(iLen*sizeof(char));
while ( (iBuf = fgetc(fInStr)) != EOF && iBuf != '\n')
{
cSring[iLen - 1] = iBuf;
cSring = (char*)realloc(cSring, ++iLen*sizeof(char));
cSring[iLen-1] = '\0';
}
isStringNum(cSring);
fflush(stdin);
return cSring;
}
void isStringNum(char *cInStr)
{
int iCount;
int iLenStr = strlen(cInStr);
if (iLenStr == 0)
{
printf("입력값이 없습니다");
return;
}
else
{
for (iCount = 0; iCount < iLenStr; iCount++)
{
if (cInStr[iCount] == '+' ||
cInStr[iCount] == '-' ||
cInStr[iCount] == '.')
{
continue;
}
if (cInStr[iCount] < '0' || cInStr[iCount] >'9')
{
printf("문자열 데이터를 입력하셨습니다\n");
return;
}
}
printf("숫자열 데이터를 입력하셨습니다\n");
}
}
'IT 전공지식 > 자료구조(Data Structure)' 카테고리의 다른 글
linked list 정렬입력 (0) | 2014.11.14 |
---|---|
linked list (0) | 2014.11.14 |
배열 스택 (0) | 2014.10.24 |
서브스트링 연산 (0) | 2014.10.23 |
스트링 결합연산 (0) | 2014.10.23 |