옵션 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | #include<stdio.h> #include<stdlib.h> #include<string.h> #include<conio.h> #define SIZE 20 typedef char element; typedef struct QueueNode{ element name[SIZE]; element sex; struct QueueNode *link; }QueueNode; typedef struct{ QueueNode *front, *rear; }QueueType; void init(QueueType *q){ q->front = q->rear = NULL; } int is_empty(QueueType *q) { return (q->front == NULL); } void enqueue(QueueType *q, element name[], element sex){ QueueNode *tmp = (QueueNode *)malloc(sizeof(QueueNode)); if (tmp == NULL){ fprintf(stderr, "memory allocate error"); exit(1); } else{ strcpy_s(tmp->name, 1024, name); tmp->sex = sex; tmp->link=NULL; if (is_empty(q)){ q->front = tmp; q->rear = tmp; } else{ q->rear->link = tmp; q->rear = tmp; } } } element dequeue(QueueType *q){ QueueNode *tmp = q->front; element item[SIZE]; if (is_empty(q)){ fprintf(stderr, "Queue is empty"); exit(2); } else{ strcpy_s(item, sizeof(item), tmp->name); q->front = q->front->link; if (q->front == NULL) q->rear = NULL; free(tmp);//에러가 나는 부분입니다! return item; } } element peek(QueueType *q){ element tmp[SIZE]; if (is_empty(q)) return 0; else{ strcpy_s(tmp, 1024, q->front->name); return tmp; } } void main(){ QueueType man; QueueType girl; int i = 0; init(&man); init(&girl); for (;;){ element name[SIZE]=""; element sex=""; printf("고객이름 : "); gets_s(name,sizeof(name)); fflush(stdin); printf("성별을 입력하세요(f or m) "); sex = getc(stdin); fflush(stdin); switch (sex){ case 'm': enqueue(&man, name, sex); break; case 'f': enqueue(&girl, name, sex); break; default: fprintf(stderr, "only accept m or f!"); exit(3); } if (i++ > 2) printf("%s \n", dequeue(&girl));//원래는 밑에를 실행해야하는데 dequeue가 안되서 이렇게 임시로 해놨스빈다. /*if (peek(&man) == 0 || peek(&girl) == 0){ printf("\n아직 대상자가 없습니다. 기다려주십시요.\n"); } else{ printf("커플이 탄생했습니다! %s과 %s\n", dequeue(&man), dequeue(&girl)); }*/ } } | cs |