• 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 }
  • 相关阅读:
    ExtJs005继承
    ExtJs004define定义类
    ExtJS笔记
    解决vscode-pandoc插件生成pdf中文显示问题
    UDP学习笔记(.NET)
    WPF类库不能添加资源词典(xaml)的解决办法
    解决win10下获取操作系统版本为6.2.900(win8)的问题
    [转] Unit Test 访问Internal类型和方法
    VS2017使用小技巧(持续更新。。。)
    [转]Github遇到Permanently added the RSA host key for IP address '192.30.252.128' to the list of known host
  • 原文地址:https://www.cnblogs.com/24zyt/p/6980324.html
Copyright © 2020-2023  润新知