/* 目录: 一: 栈模板 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; }