码迷,mamicode.com
首页 > 其他好文 > 详细

011 模板栈和队列

时间:2019-10-07 23:43:49      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:queue   false   color   ++   top   ace   head   队列   const   

 

 

/*
目录:
一: 栈模板
    1 类内编写
    2 类外编写 
二: 队列
    1 类内编写
    2 类外编写
*/    

 

一: 栈模板
  1 类内编写

// Stack.h
#pragma once
#include <iostream>

template<typename DATA>
class CStack
{
public:
    CStack() :m_nTop(-1)
    {

    }
    bool isFull()
    {
        if (m_nTop + 1 >= COUNT)
        {
            return true;
        }
        return false;
    }

    bool isEmpty()
    {
        if (-1 == m_nTop)
        {
            return true;
        }
        return false;
    }

    bool push(const DATA &data)
    {
        if (true != isFull())
        {
            ++m_nTop;
            m_data[m_nTop] = data;
            return true;
        }

        return false;
    }

    bool pop(DATA &data)
    {
        if (true != isEmpty())
        {
            data = m_data[m_nTop];
            --m_nTop;
            return true;
        }

        return false;
    }

private:
    
    enum {COUNT = 5};
    DATA m_data[COUNT];
    int m_nTop;
};

 

#include "stdafx.h"
#include "Stack.h"

using namespace std;


int main(int argc, char *argv[], char **envp)
{
    CStack<int> st;
    
    int nLoop = 0;
    while (nLoop < 10)
    {
        st.push(nLoop + 1);
        ++nLoop;
    }

    
    nLoop = 0;
    while (nLoop < 10)
    {
        int data;
        if (true == st.pop(data))
        {
            cout << data << endl;
        }

        ++nLoop;
    }
    return 0;
}

 


  2 类外编写

// Stack.h
#pragma once
#include <iostream>


template<typename DATA>
class CStack
{
public:
    CStack(int nCount = 10);

    bool isFull();
    bool isEmpty();

    bool push(const DATA &data);
    bool pop(DATA &data);

private:
    DATA *m_pData;
    int m_nCount;
    int m_nTop;
};


template<typename DATA>
CStack<DATA>::CStack(int nCount): m_nTop(-1)
{
    m_pData = new DATA[nCount];
    m_nCount = nCount;
}

template<typename DATA>
bool CStack<DATA>::isFull()
{
    if (m_nTop + 1 >= m_nCount)
    {
        return true;
    }
    return false;
}

template<typename DATA>
bool CStack<DATA>::isEmpty()
{
    if (-1 == m_nTop)
    {
        return true;
    }
    return false;
}


template<typename DATA>
bool CStack<DATA>::push(const DATA &data)
{
    if (true != isFull())
    {
        ++m_nTop;
        m_pData[m_nTop] = data;
        return true;
    }

    return false;
}

template<typename DATA>
bool CStack<DATA>::pop(DATA &data)
{
    if (true != isEmpty())
    {
        data = m_pData[m_nTop];
        --m_nTop;
        return true;
    }

    return false;
}

 


二: 队列
  1 类内编写

#pragma once
typedef int DATA;

class CQueue
{
public:
    CQueue(int nCount = 6)
    {
        m_pData = new DATA[nCount];
        m_nHead = m_nTail = 0;
        m_nCount = nCount;
    }
    ~CQueue()
    {
        delete[] m_pData;
    }

    bool isFull()
    {
        if (m_nHead == (m_nTail + 1) % m_nCount)
        {
            return true;
        }
        return false;
    }

    bool isEmpty()
    {
        if (m_nTail == m_nHead)
        {
            return true;
        }
        return false;
    }

    bool push(const DATA &data)
    {
        if (true == isFull())
        {
            return false;
        }

        m_pData[m_nTail] = data;
        m_nTail = ++m_nTail % m_nCount;
        return true;
    }

    bool pop(DATA &data)
    {
        if (true == isEmpty())
        {
            return false;
        }

        data = m_pData[m_nHead];
        m_nHead = ++m_nHead % m_nCount;
        return true;
    }

private:
    DATA *m_pData;
    int m_nCount;
    int m_nHead, m_nTail;
};

 

#include "stdafx.h"
#include "Queue.h"
#include <iostream>

using namespace std;


int main(int argc, char *argv[], char **envp)
{
    CQueue q;

    int nLoop = 0;
    while (nLoop < 10)
    {
        q.push(nLoop);
        ++nLoop;
    }

    int d;
    q.pop(d);
    cout << d << endl;
    q.pop(d);
    cout << d << endl;

    nLoop = 0;
    while (nLoop < 5)
    {
        q.push(nLoop + 1);
        ++nLoop;
    }

    while (true == q.pop(d))
    {
        cout << d << endl;
    }

    return 0;
}

 

 

  2 类外编写

 

#pragma once

template<typename DATA>

class CQueue
{
public:
    CQueue(int nCount = 6);
    ~CQueue();

    bool isFull();
    bool isEmpty();

    bool push(const DATA &data);
    bool pop(DATA &data);

private:
    DATA *m_pData;
    int m_nCount;
    int m_nHead, m_nTail;
};

template<typename DATA>
CQueue<DATA>::CQueue(int nCount)
{
    m_pData = new DATA[nCount];
    m_nHead = m_nTail = 0;
    m_nCount = nCount;
}

template<typename DATA>
CQueue<DATA>::~CQueue()
{
    delete[] m_pData;
}

template<typename DATA>
bool CQueue<DATA>::isFull()
{
    if (m_nHead == (m_nTail + 1) % m_nCount)
    {
        return true;
    }
    return false;
}

template<typename DATA>
bool CQueue<DATA>::isEmpty()
{
    if (m_nTail == m_nHead)
    {
        return true;
    }
    return false;
}

template<typename DATA>
bool CQueue<DATA>::push(const DATA &data)
{
    if (true == isFull())
    {
        return false;
    }

    m_pData[m_nTail] = data;
    m_nTail = ++m_nTail % m_nCount;
    return true;
}

template<typename DATA>
bool CQueue<DATA>::pop(DATA &data)
{
    if (true == isEmpty())
    {
        return false;
    }

    data = m_pData[m_nHead];
    m_nHead = ++m_nHead % m_nCount;
    return true;
}

 

011 模板栈和队列

标签:queue   false   color   ++   top   ace   head   队列   const   

原文地址:https://www.cnblogs.com/huafan/p/11632759.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!