线性表之队列

  • 队列是限制在两端进行插入操作和删除操作的线性表
  • 允许进行存入操作的一端称为"队尾"允许进行删除操作的一端称为"队头"
  • 当线性表中没有元素时,称为"空队"
  • 特点 :先进先出(FIFO)或后进后出
  • 普通队列的缺点:
    • 出队后前面的空间无法重用,会造成"假溢出"
    • 当 sq->front > 0 且 sq->rear == N 时,虽然数组前面有空位,但队列已满
  • 在实际应用中,循环队列是更高效的选择,因为它避免了元素的移动,空间利用率更高。
  • 普通队列的主要缺点是空间浪费或需要移动元素的开销
  • 功能实现
c 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

sequeue *queue_create() {
    sequeue *sq;
    
    if ((sq = (sequeue *)malloc(sizeof(sequeue))) == NULL) {
        printf("malloc failed\n");
        return NULL;
    }
    
    memset(sq->data, 0, sizeof(sq->data));
    sq->front = sq->rear = 0;
    return sq;
}

int enqueue(sequeue *sq, datatype x) {
    if (sq == NULL) {
        printf("sq is NULL\n");
        return -1;
    }
    
    if (sq->rear == N) {
        printf("sequeue is full\n");
        return -1;
    }
    
    sq->data[sq->rear] = x;
    sq->rear++;
    
    return 0;
}

datatype dequeue(sequeue *sq) {
    datatype ret;
    
    if (sq == NULL || sq->front == sq->rear) {
        printf("queue is empty or NULL\n");
        return (datatype)-1;  
    }
    
    ret = sq->data[sq->front];
    
    sq->front++;
    
    // 可选:当队列为空时,重置指针以重用空间
    if (sq->front == sq->rear) {
        sq->front = sq->rear = 0;
    }
    
    return ret;
}

int queue_empty(sequeue *sq) {
    if (sq == NULL) {
        printf("sq is NULL\n");
        return -1;
    }
    
    return (sq->front == sq->rear ? 1 : 0);
}

int queue_full(sequeue *sq) {
    if (sq == NULL) {
        printf("sq is NULL\n");
        return -1;
    }
    
    return (sq->rear == N ? 1 : 0);
}

int queue_clear(sequeue *sq) {
    if (sq == NULL) {
        printf("sq is NULL\n");
        return -1;
    }
    
    sq->front = sq->rear = 0;
    return 0;
}

sequeue *queue_free(sequeue *sq) {
    if (sq == NULL) {
        printf("sq is NULL\n");
        return NULL;
    }
    
    free(sq);
    return NULL;
}

int queue_length(sequeue *sq) {
    if (sq == NULL) {
        return -1;
    }
    return sq->rear - sq->front;
}
  • 头文件
c 复制代码
#define N 100  // 队列最大容量
typedef int datatype;  // 数据类型

typedef struct {
    datatype data[N];  // 存储队列元素
    int front;         // 队头指针
    int rear;          // 队尾指针
} sequeue;

sequeue *queue_create();
int enqueue(sequeue *sq, datatype x);
datatype dequeue(sequeue *sq);
int queue_empty(sequeue *sq);
int queue_full(sequeue *sq); 
int queue_clear(sequeue *sq);
sequeue *queue_free(sequeue *sq);
int queue_length(sequeue *sq);
  • 测试文件
c 复制代码
#include <stdio.h>
#include "sequeue.h"

int main(int argc, const char *argv[]) {
	sequeue *sq;

	if ((sq = queue_create()) == NULL) {
		return -1;
	}
	
	enqueue(sq, 10);
	enqueue(sq, 100);
	enqueue(sq, 1000);

	while (!queue_empty(sq)) {
		printf("dequeue:%d\n", dequeue(sq));
	}

	queue_free(sq);

	return 0;
}
相关推荐
AI情绪识别开源15 小时前
检信 ALLEMOTION OS 加密打包可执行程序 — 全面测试报告版本: v1.3功能测试 / 性能测试 /
开发语言·数据结构·人工智能·功能测试
伟大的车尔尼21 小时前
贪心的概念
数据结构·算法·贪心
AI情绪识别开源1 天前
检信AI高一分班分科智选评估系统 v2.0测试报告
开发语言·数据结构·人工智能·科技
CoderYanger1 天前
A.每日一题:输入单词需要的最少按键次数 Ⅰ+Ⅱ
java·数据结构·算法·leetcode·面试
雪碧聊技术1 天前
KMP算法详解
数据结构
布莱克6051 天前
队列详解:定义、分类、应用场景及与栈的区别(C/C++ 代码讲解)
c语言·开发语言·c++·队列
桃蹊、1 天前
FreeRTOS(六)之队列:任务间通信的基石
freertos·队列
positive_zpc1 天前
进阶数据结构图——关键路径(四)
数据结构·图论·关键路径
孙克旭_1 天前
单链表进阶实操:5 道常考面试题详细解析【Java 实现】
java·开发语言·数据结构·单链表
Chester_19991 天前
CSP202206C.角色授权
开发语言·数据结构·c++·蓝桥杯