- 存储结构
typedef struct node
{
ElementType data;
struct node* next;
}QNode;
typedef struct
{
QNode* front;
QNode* rear;
}Queue;
- 创建一个带头结点的队列
Queue* CreateQueue()
{
QNode* head=(QNode*)malloc(sizeof(QNode));
Queue* q=(Queue*)malloc(sizeof(Queue));
q->front=q->rear=head;
head->next=NULL;
return q;
}
- 判空
bool IsEmpty(Queue* q)
{
if(q->front==q->rear) return true;
return false;
}
- 入队
void InQueue(Queue* q,ElementType value)
{
QNode* New=(QNode*)malloc(sizeof(QNode));
New->data=value;
New->next=NULL;
q->rear->next=New;
q->rear=New;
}
- 出队
ElementType OutQueue(Queue* q)
{
if(IsEmpty(q)==true){
printf("队空\n");
return 0;
}
else{
QNode* p=q->front->next;
q->front->next=p->next;
int value=p->data;
free(p);
if(q->front->next==NULL) q->rear=q->front;
return value;
}
}