数据结构自己实现——queue
SeqQueue.h
#define QueueSize 100
typedef char DataType; class SeqQueue
{
public:
DataType data[QueueSize];
int front;
int rear; void Initial();
bool IsEmpty();
bool IsFull();
void EnQueue(DataType temp);
DataType DeQueue();
DataType Front();
};
#include "SeqQueue.h"
#include <iostream>
using namespace std; void SeqQueue::Initial()
{
rear = front = 0;
} bool SeqQueue::IsEmpty()
{
return rear == front;
} bool SeqQueue::IsFull()
{
return rear - front == QueueSize - 1;
} void SeqQueue::EnQueue(DataType temp)
{
if(IsFull())
cout<< "队??列?D已??满??" <<endl;
else
{
data[rear] = temp;
rear++;
}
}
DataType SeqQueue::DeQueue()
{
if(IsEmpty())
{
cout<< "队??列?D为a空?" <<endl;
}
else
{
DataType temp;
temp = data[front];
front++;
return temp;
}
}
DataType SeqQueue::Front()
{
if(IsEmpty())
{
cout<< "队??列?D为a空?" <<endl;
}
else
{
DataType temp;
temp = data[front];
return temp;
}
}
typedef char DataType;
typedef struct node
{
DataType data;
node* next;
}Node;
class LinkQueue
{
public:
Node* front;
Node* rear; void Initial();
bool IsEmpty();
void EnQueue(DataType temp);
DataType DeQueue();
DataType Front();
};
#include "LinkQueue.h"
#include <iostream>
using namespace std; void LinkQueue::Initial()
{
front = rear = NULL;
}
bool LinkQueue::IsEmpty()
{
return front == NULL;
} void LinkQueue::EnQueue(DataType temp)
{
Node* tempnode = (Node*)malloc( sizeof(Node));
tempnode->data = temp;
tempnode->next = NULL; if(IsEmpty())
{
front = rear = tempnode;
}
else
{
rear->next = tempnode;
rear = tempnode;
}
} DataType LinkQueue::DeQueue()
{
if(IsEmpty())
{
cout<< "链???队??列?D为a空?" ;
}
else
{
Node * tempnode = (Node*)malloc( sizeof(Node));
tempnode = front;
DataType temp;
temp = tempnode->data;
front = front->next;
if(rear==tempnode)
rear= NULL;
free(tempnode);
return temp;
}
}
DataType LinkQueue::Front()
{
if(IsEmpty())
{
cout<< "链???队??列?D为a空?" ;
}
else
return front->data;
}
#include <iostream>
#include "SeqQueue.h"
#include "LinkQueue.h"
using namespace std; int main()
{
SeqQueue mySeqQueue;
mySeqQueue.Initial();
mySeqQueue.EnQueue( 'a');
cout<<mySeqQueue.Front();
mySeqQueue.EnQueue( 'b');
mySeqQueue.EnQueue( 'c');
cout<<mySeqQueue.DeQueue();
cout<<mySeqQueue.DeQueue();
cout<<mySeqQueue.DeQueue();
mySeqQueue.DeQueue(); LinkQueue myLinkQueue;
myLinkQueue.Initial();
myLinkQueue.EnQueue( 'a');
myLinkQueue.EnQueue( 'b');
myLinkQueue.EnQueue( 'c');
cout<<myLinkQueue.DeQueue();
cout<<myLinkQueue.DeQueue();
cout<<myLinkQueue.DeQueue();
myLinkQueue.DeQueue();
return 0;
}
数据结构自己实现——queue的更多相关文章
- Python与数据结构[2] -> 队列/Queue[0] -> 数组队列的 Python 实现
队列 / Queue 数组队列 数组队列是队列基于数组的一种实现,其实现类似于数组栈,是一种FIFO的线性数据结构. Queue: <--| 1 | 2 | 3 | 4 | 5 |<-- ...
- 第二十四篇 玩转数据结构——队列(Queue)
1.. 队列基础 队列也是一种线性结构: 相比数组,队列所对应的操作数是队列的子集: 队列只允许从一端(队尾)添加元素,从另一端(队首)取出元素: 队列的形象化描述如下图: 队列是一种先进 ...
- 数据结构-Stack和Queue
实现: #include "c2_list.h" template <typename object> class Stack{ public: bool isEmpt ...
- Python数据结构应用2——Queue
Reference: Problem Solving with Algorithms and Data Structures, Release 3.0 队列 Queue 建立 class Queue: ...
- 自己实现数据结构系列四---Queue
一.代码部分 1.定义接口: public interface Queue<E> { void enqueue(E e); E dequeue(); E getFront(); int g ...
- python 数据结构 队列(queue)
如需转发,请注明出处:小婷儿的python https://www.cnblogs.com/xxtalhr/p/10293817.html 欢迎关注小婷儿的博客: 有问题请在博客下留言或加作者微信:t ...
- 算法与数据结构基础 - 队列(Queue)
队列基础 队列具有“先进先出”的特点,用这个特点我们可以用它来处理时间序列相关或先后次序相关的问题,例如 LeetCode题目 933. Number of Recent Calls,时间复杂度O(1 ...
- 数据结构:队列queue 函数push() pop size empty front back
队列queue: push() pop() size() empty() front() back() push() 队列中由于是先进先出,push即在队尾插入一个元素,如:可以输出:Hello W ...
- 数据结构之队列(queue)
队列介绍 1.队列是一个有序列表,可以用数组或是链表来实现. 2.遵循先入先出的原则.即:先存入队列的数据,要先取出.后存入的要后取出. 应用场景 比如某某银行叫号系统: 数组模拟队列 队列本身是有序 ...
- 数据结构与算法-queue
队列和stack类似,stack是先进后出,而queue的先进先出,也是一种特殊的线性表 基本概念 概念 队列是一种特殊的线性表 队列仅在线性表的两端进行操作 队头(Front):取出数据元素的一端 ...
随机推荐
- 【离线 线段树分治】bzoj4025: 二分图
昨天mac的gdb挂了,今天怎么笔记本的gdb也挂了…… Description 神犇有一个n个节点的图.因为神犇是神犇,所以在T时间内一些边会出现后消失.神犇要求出每一时间段内这个图是否是二分图.这 ...
- PHP使用FTP上传文件到服务器(实战篇)
我们在做开发的过程中,上传文件肯定是避免不了的,平常我们的程序和上传的文件都在一个服务器上,我们也可以使用第三方sdk上传文件,但是文件在第三方服务器上.现在我们使用PHP的ftp功能把文件上传到我们 ...
- unbuntu14下Qt4.8 和MySQL连接问题 QSqlDatabase: QMYSQL driver not loaded QSqlDatabase: available drivers: QSQLITE
使用 QSqlDatabase::addDatabase创建数据库时 会报错: QSqlDatabase: QMYSQL driver not loaded QSqlDatabase: availab ...
- 03IO端口寻址和访问控制方式
1. I/O端口和寻址 CPU 为了访问 I/O 接口控制器或者控制卡上的数据和状态信息,需要首先指定他们的地址.这种地址就称为I/O端口地址或简称端口.通常一个 I/O 控制器包含访问数据的数据端口 ...
- destoon手机端mobileurl函数增加城市分类参数
mobileurl函数在include/global.func.php 858行 共四个参数,moduleid-模型id,catid-分类id,itemid -文章id,page-页码 functio ...
- ise与win8兼容解决方案
win8中ise无法加载code,显示impact4.exe停止运行. 解决方法如下: 找到程序安装路径 1.进入文件夹 D:\Xilinx\14.6\ISE_DS\ISE\lib\nt64 把li ...
- 用Python设置matplotlib.plot的坐标轴刻度间隔以及刻度范围
一.用默认设置绘制折线图 import matplotlib.pyplot as plt x_values=list(range(11)) #x轴的数字是0到10这11个整数 y_values=[x* ...
- python hashlib模块学习
目录 hashlib 模块 破解密码 hmac 模块 hashlib 模块 1.干嘛用的: 对字符进行加密,其实就是一个自定义的字符编码表,我们原来接触的是计算机语言0和1然后转化成字符,而hashl ...
- Educational Codeforces Round 53 (Rated for Div. 2) C Vasya and Robot 二分
题目:题目链接 思路:对于x方向距离与y方向距离之和大于n的情况是肯定不能到达的,另外,如果n比abs(x) + abs(y)大,那么我们总可以用UD或者LR来抵消多余的大小,所以只要abs(x) + ...
- 搜索引擎elasticsearch常用指令演示
目录 交互方式 常用操作示例 添加文档 删除文档 修改文档 查询 简单查询 高级多条件查询 交互方式 操作ES有3种方式: kibana控制台(Dev Tools) Http + json api接口 ...