队列:
先进先出 头删尾插
循环队列:
判断顺序队列 空还是满 --用空一个位置来判断
一开始就空出一个位置,尾+1=头即满
优先级队列:
出数据时,有两个权重–》优先级、顺序 (用堆来实现 最大堆将最大的元素出出去)
双端队列:
头删头插 尾删尾插都可以 --从两头去操作
受限的双端队列:
从两头出 但是只能从一头插 或者从两头插 一头出
队列实例:
queue.h
#pragma once
typedef int ElemType;
typedef struct _Que
{
ElemType data[10];
int head;
int tail;
}sQue, *pQue;
bool QueueInit(pQue que);
bool Push(pQue que, ElemType val);
bool Pop(pQue que);
bool GetHead(pQue que, ElemType *val);
void ClearQueue(pQue que);
// 循环队列
bool CQueueInit(pQue que);
bool CPush(pQue que, ElemType val);
bool CPop(pQue que);
bool CGetHead(pQue que, ElemType *val);
void CClearQueue(pQue que);
// 链式队列
typedef struct _Node
{
ElemType data;
struct _Node *next;
}LNode, *LinkList;
typedef struct _LQue
{
LinkList head;
LinkList tail;
}sLQue, *pLQue;
bool InitLQue(pLQue que);
bool LPush(pLQue que, ElemType val);
bool LPop(pLQue que);
queue.cpp
#include "queue.h"
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <malloc.h>
/
static bool IsEmpty(pQue que)
{
return que->tail == 0;
}
static bool IsFull(pQue que)
{
return que->tail == 10;
}
/
bool QueueInit(pQue que)
{
assert(que != NULL);
if (que == NULL) return false;
que->head = que->tail = 0;
}
bool Push(pQue que, ElemType val)
{
if (IsFull(que)) return false;
que->data[que->tail++] = val;
return true;
}
bool GetHead(pQue que, ElemType *val)
{
if (IsEmpty(que)) return false;
*val = que->data[que->head];
return true;
}
bool Pop(pQue que)
{
if (IsEmpty(que)) return false;
for (int i = 0; i < que->tail - 1; ++i)
{
que->data[i] = que->data[i + 1];
}
que->tail -= 1;
return true;
}
void ClearQueue(pQue que)
{
que->tail = 0;
}
///
static bool CIsEmpty(pQue que)
{
return que->head == que->tail;
}
static bool CIsFull(pQue que)
{
if ((que->tail + 1) % 10 == que->head)
{
return true;
}
return false;
}
///
bool CQueueInit(pQue que)
{
assert(que != NULL);
if (que == NULL) return false;
que->head = que->tail = 0;
}
bool CPush(pQue que, ElemType val)
{
if (CIsFull(que)) return false;
que->data[que->tail] = val;
que->tail = (que->tail + 1) % 10;
return true;
}
bool CPop(pQue que)
{
if (CIsEmpty(que)) return false;
que->head = (que->head + 1) % 10;
return true;
}
bool CGetHead(pQue que, ElemType *val)
{
if (CIsEmpty(que)) return false;
*val = que->data[que->head];
return true;
}
void CClearQueue(pQue que)
{
que->tail = que->head = 0;
}
bool InitLQue(pLQue que)
{
assert(que != NULL);
if (que == NULL) return false;
que->head = que->tail = NULL;
}
bool LPush(pLQue que, ElemType val)
{
LinkList s = (LinkList)malloc(sizeof(LNode));
assert(s != NULL);
if (s == NULL) return false;
s->data = val;
s->next = NULL;
if (que->tail == NULL)
{
que->tail = que->head = s;
}
else
{
que->tail->next = s;
que->tail = s;
}
}
bool LPop(pLQue que)
{
if (que->head == NULL) return false;
LinkList p = que->head;
que->head = que->head->next;
if (que->head == NULL)
{
que->tail = NULL;
}
free(p);
}