Queue.h
//
// Queue.hpp
// FirstP
//
// Created by 赫赫 on 2023/11/1.
// 这里主要记录一下顺序栈#ifndef Queue_hpp
#define Queue_hpp#define MaxSize 10#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
using namespace std;#endif /* Queue_hpp */typedef struct{int data[MaxSize];int front,rear;//队头指针和队尾指针
}SeqQueue;typedef struct LNode{int data;struct LNode *next;
}LNode,*LinkList;//初始化队列
void InitQueue(SeqQueue &Q);//入队操作
bool EnQueue(SeqQueue &Q,int elem);//出队操作
bool DeQueue(SeqQueue &Q,int &elem);//判断队列是否为空
bool isEmpty(SeqQueue &Q);//链式队列的定义
typedef struct{LNode *front,*rear;//队头指针和队尾指针
}LinkQueue;//初始化链式队列
void InitLinkQueue(LinkQueue &Q);
Queue.cpp
//
// Queue.cpp
// FirstP
//
// Created by 赫赫 on 2023/11/1.
//#include "Queue.hpp"//初始化队列
void InitQueue(SeqQueue &Q){Q.rear=0;Q.front=0;//Q.rear==Q.front时队列为空
}//入队操作
bool EnQueue(SeqQueue &Q,int elem){if((Q.rear+1)%MaxSize==Q.front){//判断队列满return false;}else{Q.data[Q.rear]=elem;Q.rear=(Q.rear+1)%MaxSize;return true;}
}//出队操作
bool DeQueue(SeqQueue &Q,int &elem){if(Q.rear==Q.front){//队列为空return false;}//只有队头指针的元素出队列elem=Q.data[Q.front];Q.front=(Q.front+1)%MaxSize;return true;
}//判断队列是否为空
bool isEmpty(SeqQueue &Q){if(Q.rear==Q.front){return true;}else{return false;}
}//初始化链式队列
void InitLinkQueue(LinkQueue &Q){LNode *p=(LNode *)malloc(sizeof(LNode));Q.front=p;Q.rear=p;
}