# include <stdio.h>
# include <malloc.h>
# include <stdlib.h> //包含了exit函数
typedef struct LinkList{
int data;
struct LinkList * next;
}NODE, * PNODE;
typedef struct Queue{
PNODE front;
PNODE rear;
}QUEUE,* PQUEUE ;
//初始化
bool init(PQUEUE pq);
//入队
bool inQueue(PQUEUE pq,int val);
//出队
int outQueue(PQUEUE pq);
//判空
bool isEmpty(PQUEUE pq);
//输出
void showElements(PQUEUE pq);
int main(int argc, char *argv[])
{
QUEUE qu;
init(&qu);
showElements(&qu);
outQueue(&qu);
inQueue(&qu,1);
inQueue(&qu,2);
inQueue(&qu,3);
inQueue(&qu,4);
inQueue(&qu,5);
showElements(&qu);
outQueue(&qu);
showElements(&qu);
outQueue(&qu);
showElements(&qu);
return 0;
}
//初始化
bool init(PQUEUE pq){
//创建新的节点
PNODE pnew=(PNODE)malloc(sizeof(NODE));
if(NULL==pnew){
printf("动态内存分配失败,元素入队失败!");
return false;
}
pnew->next=NULL;
pq->front=pnew;
pq->rear=pq->front;
}
//入队
bool inQueue(PQUEUE pq,int val){
//创建新的节点
PNODE pnew=(PNODE)malloc(sizeof(NODE));
if(NULL==pnew){
printf("动态内存分配失败,元素入队失败!");
return false;
}
pnew->next=NULL;
pq->rear->data=val;
pq->rear->next=pnew;
pq->rear=pnew;
}
//出队
int outQueue(PQUEUE pq){
if(isEmpty(pq)){
printf("队列已空,再无元素可供出队,程序将返回0!\n");
return 0;
}
PNODE p=pq->front;
int result=p->data;
pq->front=pq->front->next;
free(p);
}
//判空
bool isEmpty(PQUEUE pq){
if(pq->front->next==NULL){
return true;
}
return false;
}
//输出
void showElements(PQUEUE pq){
if(isEmpty(pq)){
printf("栈为空,无元素可供输出!\n");
return;
}
PNODE p=pq->front;
while(p->next!=NULL){
printf("%d ",p->data);
p=p->next;
}
printf("\n");
}