#include <stdio.h>
#include <conio.h>
#define MaxLength 10
typedef int ElementType;
typedef struct{
        ElementType Element[MaxLength];
        int Front, Rear;
        }Queue;

//Tao hang rong
void MakeNull_Queue(Queue *Q){
     Q->Front=-1;
     Q->Rear=-1;
     }

//Kiem tra hang rong
int Empty_Queue(Queue Q){
    return Q.Front==-1;
}

//Kiem tra hang day
int Full_Queue(Queue Q){
    return (Q.Rear-Q.Front+1)==MaxLength;
}

//Them phan tu vao hang
void EnQueue(ElementType X, Queue *Q){
     if (!Full_Queue(*Q)){
        if (Empty_Queue(*Q)) Q->Front=0;
        if (Q->Rear==MaxLength-1){
           for (int i=Q->Front;i<=Q->Rear;i++)
               Q->Element[i-Q->Front]=Q->Element[i];
               Q->Rear=MaxLength-Q->Front-1;
               Q->Front=0;
               }
        Q->Rear=Q->Rear+1;
        Q->Element[Q->Rear]=X;
        }
     else printf("\nLoi! Hang day!");
}

//Xoa phan tu ra khoi hang
void DeQueue(Queue *Q){
     if (!Empty_Queue(*Q)){
        Q->Front=Q->Front+1;
        if (Q->Front>Q->Rear) MakeNull_Queue(Q);
        }
     else printf("\nLoi! Hang rong!");
}

//Lay noi dung cua phan tu dau hang
ElementType Front(Queue Q){
            return Q.Element[Q.Front];
}

//Nhap du lieu cho hang
void ReadQueue(Queue *Q){
     MakeNull_Queue(Q);
     ElementType X;
     int n;
     printf("\n\nHang co bao nhieu phan tu?");
     scanf("%d",&n);
     for (int i=1;1<=n;i++){
         printf("\nPhan tu thu %d : ",i); scanf("%d",&X);
         EnQueue(X,Q);
         }
}

//Hien thi hang
void PrintQueue(Queue *Q){
     while (!Empty_Queue(*Q)){
           printf("%d ", Front(*Q));
           DeQueue(Q);
           }
     printf("\n");
}

//Dao chieu ngan xep
void ReverseStack(Stack *S){
     Queue Q;
     MakeNull_Queue(&Q);
     while (!Empty_Stack(*S)){
           EnQueue(Top(S)),&Q);
           Pop(S);
           }
     while (!Empty_Queue(Q)){
           Push(Front(Q),S);
           DeQueue(&Q);
     }
}