循环链队,这个考试考的多,因为循环链队充分结合了链表和队列,所以很多自命题很爱考
#include<iostream>
#include<stdio.h>
using namespace std;
初始化
//定义结构体
typedef struct qNode
{
int data;
struct qNode *next;
}qNode,*queue;
typedef struct
{
queue front;
queue rear;
}Queue;
//初始化队列
void initQueue(Queue &Q)
{
Q.front=Q.rear=(queue)malloc(sizeof(qNode));
Q.front->next=NULL;
Q.rear->next=NULL;
}
特判
//判断队列是否为空
bool emptyQueue(Queue &Q)
{
if(Q.front==Q.rear) return false;
else return true;
}
入队
//入队列
void inQueue(Queue &Q,int x)
{
queue p;
p=(queue)malloc(sizeof(qNode));
p->data=x;
p->next=Q.front;
Q.rear->next=p;
Q.rear=p;
}
出队
//出队列(或者直接函数返回x也行,不需要单独保存在x当中)
void outQueue(Queue &Q,int &x){
queue p;
p=(queue)malloc(sizeof(qNode));
p=Q.front->next;
Q.rear->next=p;
x=p->data;
Q.front->next=p->next;
if(Q.rear==p) Q.rear=Q.front;
delete(p);//释放p所指结点空间
}
发现有问题了,及时跟我反应
返回目录:
https://www.acwing.com/file_system/file/content/whole/index/content/5976074/