哈尔滨百度优化搜索关键词优化
队列的基本概念
只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出
入队列:进行插入操作的一端称为队尾
出队列:进行删除操作的一端称为队头
队列用链表实现
队列的实现
队列的定义
队列初始化
入队
出队
判断队列是否为空
销毁
队头数据
队尾数据
队列数据
运行调试
完整代码
Queue.h
#pragma once#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <stdbool.h>typedef int QDataType;typedef struct QueueNode
{struct QueueNode* next;QDataType data;
}QNode;typedef struct Queue
{QNode* head;QNode* tail;int size;
}Que;void QueueInit(Que* pst); //初始化队列void QueuePush(Que* pst,QDataType x); //入队void QueuePop(Que* pst); //出队bool QueueEmpty(Que* pst); //判断队列是否为空int QueueSize(Que* pst); //队列数据QDataType QueuBack(Que* pst); //队尾数据QDataType QueueFront(Que* pst); //队头数据void QueueDestroy(Que* pst); //销毁队列
Queue.c
#define _CRT_SECURE_NO_WARNINGS 1#include "Queue.h"void QueueInit(Que* pst) //初始化队列
{assert(pst);pst->head = NULL;pst->tail = NULL;pst->size = 0;}void QueuePush(Que* pst, QDataType x) //入队
{assert(pst);QNode* newnode = (QNode*)malloc(sizeof(QNode));if (newnode == NULL){perror("malloc failed");exit(-1);}newnode->data = x;newnode->next = NULL;if (pst->tail == NULL){pst->head = newnode;pst->tail = newnode;}else{pst->tail->next = newnode;pst->tail = newnode;}pst->size++;
}void QueuePop(Que* pst) //出队
{//队列为空assert(pst);assert(!QueueEmpty(pst));//队列只有一个元素if (pst->head->next == NULL){free(pst->head);pst->head = pst->tail = NULL;}//队列多个元素else{QNode* del = pst->head;pst->head = pst->head->next;free(del);}pst->size--;
}bool QueueEmpty(Que* pst) //判断队列是否为空
{assert(pst);return pst->head == NULL;
}void QueueDestroy(Que* pst) //销毁队列
{assert(pst);QNode* cur = pst->head;while (cur){QNode* next = cur->next;free(cur);cur = next;}pst->head = NULL;pst->tail = NULL;pst->size = 0;
}QDataType QueueFront(Que* pst) //队头数据
{assert(pst);assert(!QueueEmpty(pst));return pst->head->data;
}QDataType QueuBack(Que* pst) //队尾数据
{assert(pst);assert(!QueueEmpty(pst));return pst->tail->data;
}int QueueSize(Que* pst) //队列数据
{assert(pst);return pst->size;
}