c语言实现循环队列

  • 接口参考严蔚敏老师的数据结构
  • 难点
    • 开始队列为空时front = rear = 0,队列满时如果也是front = rear,则很难判断空满两个状态,因此队列空出一个空间专门用来放尾指针。
    • 循环队列如何达到循环状态,接口函数实现的过程最好画图来形象的展示,实现后一个个调试,因为一不小心就可能出错误。
  • 实现环境:linux

下面是实现过程

数据结构:
typedef struct {
	Item *base;
	int front;
	int rear;
}Queue;

queue.h

#ifndef QUEUE_H_
#define QUEUE_H_
#include <stdbool.h>

#define MAXQSIZE 6
#define OK 1
#define ERROR 0
#define OVERFLOW -1

typedef int Item;

typedef struct {
	Item *base;
	int front;
	int rear;
}Queue;

/*initialize the queue*/
void InitQueue(Queue *q);

/*return the length of the queue*/
unsigned int QueueLength(Queue q);

/*Destroy the queue*/
void DestroyQueue(Queue *q);

/*determine if the queue is empty*/
bool IsEmpty(Queue q);

/*determine if the queue is full*/
bool IsFull(Queue q);

/*return the head elem of the queue*/
Item Top(Queue q);

/*return the back elem of the queue*/
Item Back(Queue q);

/*enqueue, insert the rear*/
bool EnQueue(Queue *q, Item e);

/*dequeue, pop the front*/
bool DeQueue(Queue *q);

/*print the queue*/
void PrintQueue(Queue q);

#endif



queue.c

#include "queue.h"
#include <stdio.h>
#include <stdlib.h>

void InitQueue(Queue *q) {
	q->base = (Item*)malloc(MAXQSIZE * sizeof(Item));
	if (q->base == NULL)
		exit(OVERFLOW);
	q->front = 0;
	q->rear = 0;
}

/*return the length of the queue*/
unsigned int QueueLength(Queue q) {
	return (q.rear - q.front + MAXQSIZE) % MAXQSIZE;
}
/*Destroy the queue*/
void DestroyQueue(Queue *q) {
	q->base = NULL;
	q->rear = 0;
	q->front = 0;
	free(q->base);
}

/*determine if the queue is empty*/
bool IsEmpty(Queue q) {
	return q.rear == q.front;
}

bool IsFull(Queue q) {
	return (q.rear + 1) % MAXQSIZE == q.front;
}

/*return the head elem of the queue*/
Item Top(Queue q) {
	return q.base[q.front];
}

/*return the back elem of the queue*/
Item Back(Queue q) {
	return q.base[(q.rear - 1 + MAXQSIZE) % MAXQSIZE];
}

/*enqueue, insert the rear*/
bool EnQueue(Queue *q, Item e) {
	if (IsFull(*q))
		return ERROR;
	q->base[q->rear] = e;
	q->rear = (q->rear + 1) % MAXQSIZE;
	
	return OK;
}
/*dequeue, pop the front*/
bool DeQueue(Queue *q) {
	if(IsEmpty(*q))
		return ERROR;
	q->front = (q->front + 1) % MAXQSIZE;
	return OK;
}

/*print the queue*/
void PrintQueue(Queue q) {
	int i, j;
	for (i = 0, j = q.front; i < QueueLength(q); i++, j = (j + 1) % MAXQSIZE) {
		printf("%d\n",q.base[j]);
	}
}

main.c

#include "queue.h"
#include <stdio.h>
int main () {
	Queue q;
	InitQueue(&q);
	EnQueue(&q, 1);
	EnQueue(&q, 2);
	EnQueue(&q, 3);
        EnQueue(&q, 4);
	EnQueue(&q, 5);
	if (IsFull(q))
		printf("hihi\n");
	DeQueue(&q);
	printf("%d\n%d\n", q.front, q.rear);
        EnQueue(&q, 6);
	printf("%d\n", Top(q));
	//printf("%d\n", q.base[0]);
	printf("%d\n", Back(q));
	PrintQueue(q);
	DestroyQueue(&q);	
}

Makefile

object = main.o queue.o

test : $(object)
	gcc -g -Wall -o test $(object)

main.o : queue.h
queue.o : queue.h

.PHONY : clean
clean :
	rm -rf *.o

  • 4
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
循环队列是一种特殊的队列,它的队尾可以指向队列的开头,形成一个环状结构。下面是C语言实现循环队列的基本操作: 1. 定义循环队列结构体 ```c typedef struct { int* arr; // 存放数据 int front; // 队首 int rear; // 队尾 int count; // 队内有效元素 int N; // 循环队列的长度 } MyCircularQueue; ``` 2. 创建循环队列 ```c MyCircularQueue* myCircularQueueCreate(int k) { MyCircularQueue* q = (MyCircularQueue*)malloc(sizeof(MyCircularQueue)); // 申请空间 if (q == NULL) { return NULL; } q->arr = (int*)malloc(sizeof(int) * k); // 申请空间 if (q->arr == NULL) { return NULL; } q->front = 0; // 初始化 q->rear = 0; q->count = 0; q->N = k; return q; } ``` 3. 判断循环队列是否为空 ```c bool myCircularQueueIsEmpty(MyCircularQueue* obj) { return obj->count == 0; } ``` 4. 判断循环队列是否已满 ```c bool myCircularQueueIsFull(MyCircularQueue* obj) { return obj->count == obj->N; } ``` 5. 入队操作 ```c bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) { if (myCircularQueueIsFull(obj)) { return false; } obj->arr[obj->rear] = value; obj->rear = (obj->rear + 1) % obj->N; obj->count++; return true; } ``` 6. 队操作 ```c bool myCircularQueueDeQueue(MyCircularQueue* obj) { if (myCircularQueueIsEmpty(obj)) { return false; } obj->front = (obj->front + 1) % obj->N; obj->count--; return true; } ``` 7. 获取队首元素 ```c int myCircularQueueFront(MyCircularQueue* obj) { if (myCircularQueueIsEmpty(obj)) { return -1; } return obj->arr[obj->front]; } ``` 8. 获取队尾元素 ```c int myCircularQueueRear(MyCircularQueue* obj) { if (myCircularQueueIsEmpty(obj)) { return -1; } return obj->arr[(obj->rear - 1 + obj->N) % obj->N]; } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值