顺序存储结构队列
#include<stdio.h>
#define MAXSIZE 100
#define SIZE sizeof(queue)
typedef struct{
int data[MAXSIZE];
int rear,front;
}queue;
queue *initQueue();
int inQueue(queue *head, int d);
int outQueue(queue *head, int *d);
int frontQueue(queue *head, int *d);
int emptyQueue(queue *head);
int main(){
queue *p = initQueue();
inQueue(p,1);
inQueue(p,2);
inQueue(p,3);
while (!emptyQueue(p)) {
int a,b;
frontQueue(p,&a);
outQueue(p,&b);
printf("front=%d, out=%d\n", a, b);
}
return 0;
}
//队列初始化
queue *initQueue() {
queue *p = (queue*)malloc(SIZE);
p->rear = -1;
p->front = -1;
return p;
}
//入队
int inQueue(queue *head, int d){
if(head->rear == MAXSIZE-1)
return 0;
head->data[++head->rear] = d;
if(emptyQueue(head))
head->front++;
return 1;
}
//出队
int outQueue(queue *head, int *d){
if(emptyQueue(head))
return 0;
//队列为空时初始化
if (head->front == head->rear) {
*d = head->data[head->front];
head->front = -1;
head->rear = -1;
}else{
*d = head->data[head->front++];
}
return 1;
}
//读队头
int frontQueue(queue *head, int *d){
if(emptyQueue(head))
return 0;
*d = head->data[head->front];
return 1;
}
//队判空
int emptyQueue(queue *head){
if (head->front == -1)
return 1;
return 0;
}
循环队列
#include<stdio.h>
#define MAXSIZE 100
#define SIZE sizeof(CSeQueue)
typedef struct {
int data[MAXSIZE];
int front;
int rear;
}CSeQueue;
CSeQueue *initSeQueue();
int inSeQueue(CSeQueue *q, int x);
int outSeQueue(CSeQueue *q, int *x);
int frontScQueue(CSeQueue *q, int *x);
int emptySeQueue(CSeQueue *q);
int main(){
}
//初始化
CSeQueue *initSeQueue(){
CSeQueue *q = (CSeQueue*)malloc(SIZE);
q->front = q->rear = MAXSIZE-1;
return q;
}
//入队
int inSeQueue(CSeQueue *q, int x){
if ((q->rear+1) % MAXSIZE == q->front)
return 0;
q->rear = (q->rear+1) % MAXSIZE;
q->data[q->rear] = x;
return 1;
}
//出队
int outSeQueue(CSeQueue *q, int *x){
if (emptySeQueue(q))
return 0;
q->front = (q->front+1) % MAXSIZE;
*x = q->data[q->front];
}
//读队头
int frontScQueue(CSeQueue *q, int *x){
if (emptySeQueue(q))
return 0;
int t = (q->front+1) % MAXSIZE;
*x = q->data[t];
return 1;
}
//判空
int emptySeQueue(CSeQueue *q){
if(q->front == q->rear)
return 1;
return 0;
}
链队列
#include<stdio.h>
typedef struct node {
int data;
struct node *next;
}QNode;
typedef struct {
QNode *front;
QNode *reat;
}LQueue;
LQueue *init_lQueue();
int inLQueue(LQueue *q, int x);
int outLQueue(LQueue* q, int *x);
int emptyLQueue(LQueue *q);
int frontLQueue(LQueue *q, int *x);
//链队列初始化
LQueue *init_lQueue(){
LQueue *q;
QNode *p;
q = (LQueue*)malloc(sizeof(LQueue));
p = (QNode*)malloc(sizeof(QNode));
p->next = NULL;
q->front = q->rear = p;
return q;
}
//链队列入队
int inLQueue(LQueue *q, int x){
QNode *p;
if(p = (QNode*)malloc(sizeof(QNode)) == NULL)
return 0;
p->data = x;
p->next = NULL;
q->reat->next = p;
q->rear = p;
return 1;
}
//链队列出队
int outLQueue(LQueue* q, int *x){
QNode *p;
if(emptyLQueue(q))
return 0;
p = q->front->next;
q->front->next = p->next;
*x = p->data;
free(p);
//出队后为空时,队列初始化
if(q->front->next == NULL)
q->rear = q->front;
return 1;
}
//读队头
int frontLQueue(LQueue *q, int *x){
if(emptyLQueue(q))
return 0;
*x = q->front->next->data;
return 1;
}
//判空
int emptyLQueue(LQueue *q){
if (q->front == q->rear)
return 1;
return 0;
}