链队列的基本操作

这里写图片描述

Queue.h

#pragma once

#include <stdio.h>
#include <malloc.h>
#include <assert.h>
#define TRUE 1
#define FALSE 0


typedef int DataType;
/*用链表表示的队列简称为链队列,为了操作方便,采用带头结点的链表结构,
并设置一个队头指针和一个对尾指针*/
typedef struct QueueNode    /*结点结构*/
{
    DataType data;          /*数据域*/
    struct QueueNode* next; /*指针域*/
}QueueNode;

typedef struct Queue
{
    QueueNode* front;       /*队头指针*/
    QueueNode* back;        /*对尾指针*/
}Queue;

void QueueInit(Queue* pq);
void QueueDestory(Queue* pq);
QueueNode* BuyQueueNode(DataType x);
void QueuePush(Queue* pq, DataType x);
void QueuePop(Queue* pq);
DataType QueueFront(Queue* pq);
int QueueEmpty(Queue* pq);
int QueueSize(Queue* pq);
DataType QueueBack(Queue* pq);

void TestQueue();

Queue.c

#include "Queue.h"

void QueueInit(Queue * Q)//初始化为空的链队列
{
    Q->front = (QueueNode*)malloc(sizeof(QueueNode));
    Q->back = Q->front;/*头指针和尾指针都指向头结点*/
    Q->front->next = NULL;
}

void QueueDestory(Queue * Q)//销毁队列
{
    assert(Q);
    QueueNode *cur = Q->front;
    while (cur)
    {
        Q->front = Q->front->next;
        free(cur);
        cur = Q->front;
    }
    Q->front = NULL;
    Q->back = NULL;
}

QueueNode * BuyQueueNode(DataType x)
{
    QueueNode *NewQueueNode;
    NewQueueNode = (QueueNode*)malloc(sizeof(QueueNode));
    if (NewQueueNode == NULL)
    {
        printf("创建队列结点失败\n");
    }
    NewQueueNode->data = x;
    NewQueueNode->next = NULL;
}

void QueuePush(Queue * Q, DataType x)
{
    assert(Q);
    QueueNode *NewQueueNode;
    NewQueueNode = (QueueNode*)malloc(sizeof(QueueNode));
    if (NewQueueNode != NULL)
    {
        NewQueueNode->data = x;
        NewQueueNode->next = NULL;
        Q->back->next = NewQueueNode;/*在队尾插入结点*/
        Q->back = NewQueueNode;/*修改队尾指针*/
    }
}

void QueuePop(Queue * Q)//出队列
{
    assert(Q);
    QueueNode *tmp;
    if (Q->front == Q->back)
    {
        primtf("空队列,不需出队\n");
    }
    tmp = Q->front->next;/*tmp指向队头元素*/
    Q->front->next = tmp->next;/*队头元素tmp出队*/
    if (Q->back == tmp)/*如果队中只有一个元素tmp,则tmp出队后成为空队*/
    {
        Q->back = Q->front;
        Q->front->next = NULL;
        free(tmp);
    }
}

DataType QueueFront(Queue * Q)//获取队头元素
{
    assert(Q);
    if (Q->front == Q->back)/*空队返回0*/
    {
        return FALSE;
    }
    return Q->front->next->data;
}

int QueueEmpty(Queue * Q)/*判断队列是否为空,如队列为空返回TRUE,否则返回FAUSE*/
{
    assert(Q);
    if (Q->front == Q->back)
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}

int QueueSize(Queue * Q)//获取队列大小
{
    assert(Q);
    int count = 0;
    while (Q->front->next)
    {
        count++;
    }
    return count;
}

DataType QueueBack(Queue * Q)//获取队尾元素
{
    assert(Q);
    if (Q->front == Q->back)/*空队返回0*/
    {
        return FALSE;
    }
    return Q->back->data;
}
### 头歌教育平台中的队列基本操作 #### 什么是队列队列是一种基于表实现的队列结构,其特点是遵循“先进先出”的原则。与数组实现的循环队列不同,队列不需要固定大小的空间,因此可以动态扩展[^1]。 #### 队列的基本组成 队列由两个主要部分构成:节点和指针。每个节点包含两部分内容——数据域(用于存储实际的数据)以及指向下一个节点的指针域。队列通常维护两个指针变量 `front` 和 `rear`,分别指向队头和队尾节点[^2]。 #### 队列基本操作 以下是队列常见的基本操作及其具体实现: ##### 1. 初始化队列 初始化队列时,需将 `front` 和 `rear` 指向 NULL,表示当前队列为空。 ```c typedef struct Node { int data; struct Node* next; } Node; void initQueue(Node** front, Node** rear) { *front = NULL; *rear = NULL; } ``` ##### 2. 判断队列是否为空 当 `front` 和 `rear` 同时为 NULL 时,说明队列为一个空队列。 ```c int isEmpty(Node* front) { return (front == NULL); } ``` ##### 3. 入队操作 入队是指在队列的尾部插入一个新的节点。新节点成为新的队尾,并更新 `rear` 指针。 ```c void enqueue(Node** front, Node** rear, int value) { Node* newNode = (Node*)malloc(sizeof(Node)); if (!newNode) { printf("内存分配失败\n"); return; } newNode->data = value; newNode->next = NULL; if (*rear == NULL) { // 如果队列为空,则同时设置 front 和 rear *front = *rear = newNode; } else { // 否则仅更新 rear 的 next 指针 (*rear)->next = newNode; *rear = newNode; } } ``` ##### 4. 出队操作 出队是从队列头部移除一个节点的操作。如果队列不为空,则释放队首节点并调整 `front` 指针;如果此时队列变为空,则还需重置 `rear` 指针。 ```c int dequeue(Node** front, Node** rear) { if (isEmpty(*front)) { printf("队列已空,无法执行出队操作。\n"); return -1; // 返回错误标志 } Node* temp = *front; int removedValue = temp->data; *front = (*front)->next; free(temp); if (*front == NULL) { // 若队列变为空,则重置 rear *rear = NULL; } return removedValue; } ``` ##### 5. 获取队头元素 获取队头元素并不改变队列的状态,只需返回 `front` 所指向节点的数据即可。 ```c int getFront(Node* front) { if (isEmpty(front)) { printf("队列为空,无队头元素。\n"); return -1; // 返回错误标志 } return front->data; } ``` #### 总结 以上介绍了队列的基本概念及其实现方法,包括初始化、判空、入队、出队和获取队头元素等核心功能。这些操作均可以通过 C 语言中的指针来完成,体现了队列灵活性高的特点。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值