typedef struct _product {
int price;
int count;
char name[20];
}Product, *pProduct;
typedef struct _node {
pProduct item;
struct _node *next;
}Node, *pNode;
typedef struct _queue {
int count; // 노드의 갯수를 저장한다.
pNode front; // 첫번째 노드를 가리킨다.
pNode rear; // 첫번째 노드를 가리킨다.
}Queue, *pQueue;
pQueue makeQueue() {
pQueue tmp = (pQueue)malloc(sizeof(Queue));
tmp->front = NULL;
tmp->rear = NULL;
tmp->count = 0;
return tmp;
}
pNode makeNode(char *name, int price, int count) {
pNode tmp = (pNode)malloc(sizeof(Node));
tmp->item = (pProduct)malloc(sizeof(Product));
tmp->item->count = count;
tmp->item->price = price;
strcpy(tmp->item->name, name);
tmp->next = NULL;
return tmp;
}
void enqueue(pQueue Q, pNode N) {
N->next = Q->front;
Q->front = N;
if(Q->count==0)
Q->rear = N;
Q->count++;
}
pNode dequeue(pQueue Q) {
pNode tmp = Q->front;
pNode temp;
if (Q->count == 0){
return NULL;
}else if (Q->count == 1) {
Q->front = NULL;
Q->rear = NULL;
Q->count--;
return tmp;
}
else {
while (1) {
if (tmp->next == Q->rear)
break;
tmp = tmp->next;
}
Q->count--;
temp = tmp->next;
Q->rear = tmp;
tmp->next = NULL;
return temp;
}
}
void dequeueAll(pQueue Q) {
int count = Q->count;
pNode tmp;
for (size_t i = 0; i < count; i++)
{
tmp = dequeue(Q);
if(tmp!=NULL)
printf("계산하실 금액은 : %s %d개, %d원입니다. \n", tmp->item->name, tmp->item->count, tmp->item->price);
}
printf("\n");
}