顺序循环队列
#include<stdio.h>
#include<stdlib.h>
#define MAX 100
typedef struct{
int elem[MAX];
int front,rear;
}CSequene;
CSequene* InitSequene (){
CSequene *q;
q = (CSequene *)malloc(sizeof(CSequene));
q->front = q->rear = MAX - 1;
return q;
}
int InSequene(CSequene *q,int x){
if ((q->rear+1)%MAX == q->front){
printf("队满");
return FALSE;
}else{
q->rear = (q->rear + 1) % MAX;
q->elem[q->rear] = x;
return 1;
}
}
int Outquene(CSequene* q,int x){
if (q->rear == q->front){
printf("队空");
return 0;
}else {
q->front = (q->front + 1 % MAX);
*x = q->elem[q->front];
return 1;
}
}
int EmptySequene(CSequene *q){
if (q->front == q->rear){
return 1;
}else{
return 0;
}
}
链循环队列
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int data;
struct node *next;
}QNode;
typedef struct {
QNode *front;
QNode *rear;
}LQuene;
LQuene *Inin_LQuene(){
LQuene *q;
QNode *p;
q = (LQuene *)malloc(sizeof(LQuene));
p = (QNode *)malloc(sizeof(QNode));
p->next = NULL;
q->front = q->rear = p;
return q;
}
void InLQuene (LQuene *q,int data){
QNode *p;
p = (QNode *)malloc(sizeof(QNode));
p->data = data;
p->next = NULL;
q->rear->next = p;
q->rear = p;
}
int Empty_LQuene(LQuene *q){
if (q->front == q->rear){
return 0;
}else{
return 1;
}
}
int Out_LQuene(LQuene *q,int *data){
QNode *p;
if (Empty_LQuene(q)){
printf("队空");
}else{
p = q->front->next;
q->front->next = p->next;
*data = p->data;
free(p);
if (q->front->next == NULL){
q->rear = q->front;
return 1;
}
}
}