typedef struct node{
char data;
struct node *next;
}Node;
typedef struct{
Node front;
Node rear;
}Queue;
//初始化
void Init(Queue *Q)
{
Q->front =NULL;
Q->rear =NULL;
}
//判断队列空
int IsEmpty(Queue *Q)
{
return Q->front==NULL;
}
//入队·
int EnQueue(Queue Q ,char x)
{
Node p=(Node*)malloc(sizeof(Node));
p->data=x;
p->next=NULL;
//printf("right\n");
if(IsEmpty(Q))
{
Q->front=p;
Q->rear=p;
}
else{
Q->rear->next=p;
Q->rear=p;
}
return 1;
}
char OutQueue(Queue *Q)
{
Node *p;
char ch;
if(IsEmpty(Q))
{
printf("IsEmpty==%d\n",IsEmpty(Q));
printf("The Queue is NULL,you can not out the Queue!\n");
}
else if(Q->rear==Q->front)
{
p=Q->front;
ch =p->data;
Q->front=NULL;
Q->rear=NULL;
free(p);
}
else{
p=Q->front;
Q->front=p->next;
ch =p->data;
free(p);
}
Q->rear->next=p;
Q->rear=p;
return 1;
}
void PrintQueue(Queue *Q)
{
printf("right\n");
while(Q->front)
{
printf("%c\n",Q->front->data);
Q->front=Q->front->next;
}
}
int main()
{
Queue Q;
printf("Now,we will execute the EnQueue!\n");
Init(&Q);
EnQueue(&Q,'a');
EnQueue(&Q,'b');
EnQueue(&Q,'c');
EnQueue(&Q,'d');
EnQueue(&Q,'e');
PrintQueue(&Q);
printf("Now,we will execute the OutQueue!\n");
OutQueue(&Q );
PrintQueue(&Q);
//for(;;);
return 0;
}