zoukankan      html  css  js  c++  java
  • MyStack

    #pragma once
    
    
    class MyQueue
    {
    public:
        MyQueue();
        ~MyQueue();
        void Insert(int aValue);
        int Top();
        void Pop();
        void PrintQueue();
        void PrintHead();
    
    private:
        int GetIncreIndex(const int& aIndex);
    
    private:
        int* m_pData;
        int m_Length;
        int m_Count;
        int m_Head;
        int m_Tail;
    };
    View Code
    #include "MyQueue.h"
    #include <stdio.h>
    #include <string.h>
    #include <malloc.h>
    #include <stdlib.h> 
    
    MyQueue::MyQueue( void )
    :m_pData(NULL)
    , m_Length(8)
    , m_Head(0)
    , m_Tail(0)
    , m_Count(0)
    {
        m_pData = (int*)malloc(sizeof(int)*m_Length);
        memset(m_pData,0,m_Length);
    }
    
    void MyQueue::Insert( int aValue )
    {
        m_pData[m_Tail] = aValue;
        m_Tail = (m_Tail+1)%m_Length;
        ++m_Count;
    
        if (m_Count == m_Length-1)
        {
            m_Length = m_Length << 2;
            m_pData = (int*)realloc(m_pData,sizeof(int)*m_Length);
        }
    }
    
    int MyQueue::Top()
    {
        return m_pData[m_Head];
    }
    
    void MyQueue::Pop()
    {
        if (m_Count == 0)
        {
            printf("NoData
    ");
            return;
        }
        m_Head = (m_Head+1)%m_Length;
        --m_Count;
    }
    
    MyQueue::~MyQueue()
    {
        delete []m_pData;
    }
    
    void MyQueue::PrintQueue()
    {
        if (m_Count > 0)
        {
            int lTempIndex = m_Head;
            do 
            {
                printf("%d ", m_pData[lTempIndex]);
                lTempIndex = GetIncreIndex(lTempIndex);
            } while (lTempIndex != m_Tail);
        }
    
        printf("
    ");
    }
    
    int MyQueue::GetIncreIndex( const int& aIndex )
    {
        return (aIndex+1)%m_Length;
    }
    
    void MyQueue::PrintHead()
    {
        if (m_Count==0)
        {
            printf("NoData
    ");
            return;
        }
        printf("Head: %d
    ",m_pData[m_Head]);
    }
  • 相关阅读:
    echarts 折线图(移动端)X轴显示不全
    文字超出省略号类型
    逻辑运算为true
    13年省赛总结
    PyCharm专业版破解教程
    django之定义统一返回数据格式与GET/POST装饰器
    Xmind8破解教程
    django之mysqlclient安装
    django之“static”全局设置
    django之集成第三方支付平台PaysAPI与百度云视频点播服务接入
  • 原文地址:https://www.cnblogs.com/xiangshancuizhu/p/3303247.html
Copyright © 2011-2022 走看看