队列的创建,入队,出队,遍历,判断是否为空
//链式队列的代码演示
#include<bits/stdc++.h>
typedef struct Node{
int data;
struct Node *pNext;
}Node,*pNode;
typedef struct Queue{
pNode front;
pNode rear;
}Queue,*pQueue;
void init(pQueue pQ){
pQ->front=(pNode)malloc(sizeof(Node));
pQ->rear=pQ->front;
pQ->rear->pNext=NULL;
return;
}
void Enqueue(pQueue pQ,int val){
pNode pNew=(pNode)malloc(sizeof(Node));
pNew->data=val;
pNew->pNext=NULL;
pQ->rear->pNext=pNew;
pQ->rear=pNew;
return;
}
bool empty(pQueue pQ){
if(pQ->front==pQ->rear)
return true;
else
return false;
}
bool Dequeue(pQueue pQ,int *pVal){
if(empty(pQ))
return false;
pNode p=pQ->front->pNext;
*pVal=p->data;
pQ->front->pNext=p->pNext;
free(p);
p=NULL;
return true;
}
void traverse(pQueue pQ){
if(empty(pQ))
return;
pNode p=pQ->front->pNext;
while(p){
printf("%d ",p->data);
p=p->pNext;
}
printf("\n");
return;
}
int main()
{
Queue Q;
init(&Q);
Enqueue(&Q,1);
Enqueue(&Q,2);
Enqueue(&Q,3);
Enqueue(&Q,4);
int val;
if(Dequeue(&Q,&val))
printf("出队成功,出队的元素是%d\n",val);
else
printf("出对失败\n");
traverse(&Q);
}