C++ 用类封装实现队列

  1 #include<stdlib.h>
  2 #include <iostream>
  3 using std::cout;
  4 using std::endl;
  5
  6 #define SUM 3 //此处设置队列的大小,因为队列节点是 malloc 的空间,一般不用设置队列上限。但是为了测试方便就加了上限
  7
  8 typedef struct QNode{
  9     int data;
 10     struct QNode *pNext;
 11 }QNode,*pQNode;
 12
 13 class Queue{
 14     private:
 15         pQNode m_front;
 16         pQNode m_rear;
 17         int m_sum;//队列中节点数目
 18     public:
 19         Queue();
 20         ~Queue();
 21         void push(int x);
 22         void pop();
 23         int front();
 24         int back();
 25         bool empty();
 26         bool full();
 27 };
 28 Queue::Queue(){
 29     m_front = m_rear =(pQNode)malloc(sizeof(QNode));
 30     m_front->pNext = NULL;
 31     m_sum = 0;
 32 }
 33 Queue::~Queue(){//清空队列
 34     pQNode p,q;
 35     m_rear = m_front;
 36     p = m_front->pNext;
 37     m_front->pNext = NULL;
 38     while(p){
 39         q = p;
 40         p = p->pNext;
 41         free(q);
 42     }
 43     free(m_front);
 44 }
 45 void Queue::push(int x){
 46     if(!full()){
 47         pQNode p =(pQNode)malloc(sizeof(QNode));
 48         p->data = x;
 49         p->pNext = NULL;
 50         cout<< "进队列元素是"<< x <<endl;
 51         m_rear->pNext = p;
 52         m_rear = p;
 53         m_sum++;
 54     }else{
 55         cout << "队列已满" <<endl;
 56     }
 57 }
 58 void Queue::pop(){
 59
 60     pQNode tmp;//指向被删除节点,方便释放空间&判断被删节点是否是对尾节点
 61     if(empty()){
 62         cout << "队列为空,出队失败" <<endl;
 63         return ;
 64     }
 65     cout <<"删除的队列头是:" << m_front->pNext->data <<endl;
 66     tmp = m_front->pNext;
 67     m_front->pNext = tmp->pNext;
 68     if(m_rear == tmp){
 69         m_rear = m_front;
 70     }
 71     free(tmp);
 72     m_sum--;
 73 }
 74 int Queue::front(){
 75     if(empty()){
 76         cout << "队列为空" <<endl;
 77         return -1;//队列为空
 78     }else{
 79         return m_front->pNext->data;
 80     }
 81 }
 82 int Queue::back(){
 83     if(empty()){
 84         cout <<"队列为空" <<endl;
 85         return -1;//队列为空
 86     }else{
 87         return m_rear->data;
 88     }
 89 }
 90 bool Queue::empty(){
 91     if(m_front == m_rear){
 92         return true;
 93     }else
 94         return false;
 95 }
 96 bool Queue::full(){
 97     if(m_sum < SUM){
 98         return false;
 99     }else
100         return true;
101 }
102
103 int main(){
104     Queue que;
105     int ret;
106
107     que.pop();
108     que.push(1);
109     que.push(2);
110
111     que.pop();
112     if((ret = que.front()) != -1){
113         cout << "队列头元素是:"<< ret <<endl;
114     }
115     if((ret = que.back()) != -1){
116         cout << "队列尾元素是:" << ret <<endl;
117     }
118     que.push(3);
119     que.push(4);
120     que.push(5);
121
122     if((ret = que.front()) != -1){
123         cout << "队列头元素是:"<< ret <<endl;
124     }
125     if((ret = que.back()) != -1){
126         cout << "队列尾元素是:" << ret <<endl;
127     }
128
129     return 0;
130
131 }
时间: 2024-08-09 15:12:15

C++ 用类封装实现队列的相关文章

QT 操作excel 类封装

1 # pro file 2 [plain] view plaincopy 3 CONFIG += qaxcontainer 4 5 QT += core 6 7 QT -= gui 8 9 TARGET = QExcel 10 CONFIG += console 11 CONFIG -= app_bundle 12 13 TEMPLATE = app 14 15 16 SOURCES += main.cpp \ 17 qexcel.cpp 18 19 HEADERS += \ 20 qexce

php---数据库类封装

为了节省以后的时间,今天封装了操作sql语句的一个类,在此保存起来,方面以后使用. 这个类的文件名:SqlTool.class.php 主要有dql和dml两个函数 看下面的源码" <?php class SqlTool{ private $conn; private $username="root"; private $password="1234"; private $host="127.0.0.1"; private $db

操作类封装

/*操作类封装 */ /*调用方法 如下: * var str= new IStrManipulation();//实例化字符串处理接口 * console.log(str.StrManipulation('StrManipulation',"111sss23123").getLength()); * var convert =new IConvert();//实例化类型转换接口 * console.log(convert.Convert('Convert',"1112312

MFC如和将类封装到DLL以及调用

MFC如和将类封装到DLL以及调用 分类: C++技术2012-06-27 17:40 1028人阅读 评论(0) 收藏 举报 dllfunmfcnullexe *1.先用mfc向导生成静态dll文件.*2.编辑增加类:*3.生成dll文件和lib文件:*4.将生成的dll和lib,和类的头文件复制到需要引用的文*件exe下:*///导出dll的头文件myClass.h#define DLLimport __declspec(dllimport)#define DLLexprot __decls

RUBY的类封装,继承,多态简单演示

class Person def initialize(name,age=18) @name=name @age=age @motherland="China" end def talk puts "my name is " [email protected]+",age is "+@age.to_s if @motherland == "China" puts "I am a China." else p

jQuery自定义类封装:

jQuery自定义类封装: (function ($) { $.DragField = function (arg) { var name = "你好";     //这个是私有变量,外部无法访问 this.testFun = function () {     //this.testFun方法,加上了this,就是公有方法了,外部可以访问. alert(arg.title + "," + name); }; }; })(jQuery); 使用方法: var a =

C++类封装成DLL动态链接库

最近在进行OSG开发,想将里面模型导入部分重复使用的代码封装成DLL,这样后续不需要重复编写这部分代码.而C++类封装成DLL步骤如下: 1. VS2015新建一个Win32项目,应用程序类型选择DLL 2.新建DLL项目后,项目解决方案文件结构如下: 3.配置OSG库 因为DLL使用到了osg库,所以项目首先要配置osg库,如下: 4.打开OSG.h头文件,进行代码编写修改 在头文件中加入OSG库的相关头文件,并且在类中进行函数和变量的声明,头文件如下(标红部分为添加代码): 5.在源文件中O

uni-app,vue,react,Trao之缓存类封装

uni-app,vue,react,Trao之缓存类封装 一,介绍与需求 1.1,介绍 缓存主要分为如下几个 1.LocalStorage LocalStorage是永久性的本地缓存,存储在客户端的浏览器上,除非主动删除,是不会过期的.LocalStorage采用的是键值对的方式进行存储,存储方式只能是字符串.存储内容可以用图片.json.样式.脚本等. API基本使用方法: 1.1.localStorage.setItem() 存储 1.2.localStorage.getItem() 获取数

教你写Android网络框架之Request、Response类与请求队列

转载请注明出处,本文来自[ Mr.Simple的博客 ]. 我正在参加博客之星,点击这里投我一票吧,谢谢~ 前言 在教你写Android网络框架之基本架构一文中我们已经介绍了SimpleNet网络框架的基本结构,今天我们就开始从代码的角度来开始切入该网络框架的实现,在剖析的同时我们会分析设计思路,以及为什么要这样做,这样做的好处是什么.这样我们不仅学到了如何实现网络框架,也会学到设计一个通用的框架应该有哪些考虑,这就扩展到框架设计的范畴,通过这个简单的实例希望能给新人一些帮助.当然这只是一家之言