#include <stdio.h>
#include <malloc.h>
#include <string.h>

typedef struct QNode{
    char *name;//이름
    char *phone;//전화번호
    char *data;//서비스 요청 품목
    struct QNode *link;
}QNode;

typedef struct{
    QNode *front,*rear;
}LQueueType;

LQueueType *CreateLinkedQueue(){
     LQueueType *LQ;
     LQ = (LQueueType *)malloc(sizeof(LQueueType));
     LQ->front=NULL;
     LQ->rear=NULL;
     return LQ;
}

int isEmpty(LQueueType *LQ){
    if(LQ->front ==NULL){
        printf("\n Linked Queue is empty! \n");
        return 1;
    }
    else return 0;
}

void enQueue(LQueueType *LQ, char *_name,char *_phone,char *_data){
     QNode *newNode=(QNode *)malloc(sizeof(QNode));
     strcpy(newNode->name,_name);
     strcpy(newNode->phone,_phone);
     strcpy(newNode->data,_data);
     newNode->link=NULL;
     if(LQ->front==NULL){
         LQ->front =newNode;
         LQ->rear=newNode;
     }
     else {
		 LQ->rear->link=newNode;
		 LQ->rear=newNode;
     }
}

void deQueue(LQueueType *LQ){
    QNode *old=LQ->front;    
    if(isEmpty(LQ)) printf("목록이 비어있습니다 \n");
    else{  
		printf("%s님의 요청이 완료되었습니다.\n",LQ->front->name);    
        LQ->front=LQ->front->link;
		if(LQ->front==NULL)
           LQ->rear=NULL;
        free(old);
   }
}

int waitnumber(LQueueType *LQ,char *name)//대기번호
{
    int number=0;
    QNode *ptr =LQ->front;
    while(!strcmp(ptr->name,name)){
        ptr=ptr->link;
        number++;
    }
    return number;

}


int main(){
    int n;

    char name[30];
    char phone[30];
    char data[30];

	LQueueType *LQ1 = CreateLinkedQueue();

 
	 do{
		  printf("***** Service center ****** \n");
		  printf("1. 서비스 요청 등록\n2. 대기번호 출력\n3. 서비스 처리\n4. 종료\n");
		  printf("입력>>");
		  scanf("%d",&n); 
		  switch(n){
				case 1:printf("이름 전화번호 요청물품을 순서대로 입력하세요\n"); //입력
					printf("이름 : ");
					scanf("%s",name);
					printf("전화번호 : ");
					scanf("%s",phone);
					printf("요청물품 : ");
					scanf("%s",data);
					enQueue(LQ1,name,phone,data);					
					continue;
				case 2: printf("이름을 입력하세요 : ");//대기번호
					scanf("%s",&name);
					printf("대기번호 %d\n",waitnumber(LQ1,name));
					continue;
				case 3:deQueue(LQ1);//완료-삭제
					continue;
				
			}
		}while(n!=4);

}
 








