当前位置:首页 > 编程笔记 > 正文
已解决

队列相关代码

来自网友在路上 171871提问 提问时间:2023-11-05 23:06:35阅读次数: 71

最佳答案 问答题库718位专家为你答疑解惑

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;
}

查看全文

99%的人还看了

猜你感兴趣

版权申明

本文"队列相关代码":http://eshow365.cn/6-33091-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!