数据结构课后题目源码
2016-05-20 17:32
423 查看
习题描述如下:
假设以数组Q[m]存放循环队列中的元素,同时设置一个标志tag,以tag==0和tag==1来区别在队头指针(front)和队尾指针(rear)相等时,队列状态为“空”还是为”满“。试编写与此结构相应的插入(EnQueue)和删除(DeQueue)算法。
以下是博主自己码的代码,不喜勿喷!
谢谢阅读!
假设以数组Q[m]存放循环队列中的元素,同时设置一个标志tag,以tag==0和tag==1来区别在队头指针(front)和队尾指针(rear)相等时,队列状态为“空”还是为”满“。试编写与此结构相应的插入(EnQueue)和删除(DeQueue)算法。
以下是博主自己码的代码,不喜勿喷!
#ifndef QUEUE_H_ #define QUEUE_H_ #include <iostream> using namespace std; template<class T> class Queue{ private: int front,rear; T * elements; int maxSize; int tag; public: Queue(int sz = 10); ~Queue(); bool EnQueue(const T & x); bool DeQueue(T & x); }; template<class T> Queue<T>::Queue(int sz) { maxSize = sz; tag = 0; elements = new T[maxSize]; front = rear = 0; } template<class T> Queue<T>::~Queue() { delete[] elements; } template<class T> bool Queue<T>::EnQueue(const T & x) { if (front == rear&&tag == 1) { cout << "The queue is full!" << endl; return false; } elements[rear] = x; rear = (rear + 1) % maxSize; tag = 1; return true; } template<class T> bool Queue<T>::DeQueue(T & x) { if (front == rear&&tag == 0) { cout << "The queue is empty!" << endl; return false; } x = elements[front]; front = (front + 1) % maxSize; tag = 0; return true; } #endif
// ex3-23.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "Queue.h" int _tmain(int argc, _TCHAR* argv[]) { Queue<int> queue(5); int j; queue.DeQueue(j); int x = 5; for (int i = 0; i < 5; i++) { queue.EnQueue(x); } int y = 6; queue.EnQueue(y); system("pause"); return 0; }
谢谢阅读!
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 从源码安装Mysql/Percona 5.5
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- C#数据结构之顺序表(SeqList)实例详解
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(七):数据结构详解
- 浅析Ruby的源代码布局及其编程风格
- Lua教程(二):C++和Lua相互传递数据示例
- 解析从源码分析常见的基于Array的数据结构动态扩容机制的详解
- C#数据结构之队列(Quene)实例详解
- C#数据结构揭秘一
- C#数据结构之单链表(LinkList)实例详解
- C++联合体转换成C#结构的实现方法
- C++高级程序员成长之路
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析