Windows CE 3.0Windows CE 2.11Pocket PC 2002Windows MobileVisual C++ 7.1Visual C++ 7.0Windows 2003Windows 2000Visual C++ 6.0Windows XP移动应用MFC中级开发Visual StudioWindowsC++
Win32 的轻量级堆栈实现






2.11/5 (6投票s)
2004年3月19日

53580

322
一个简单且非常小的堆栈实现,适用于任何类型。
引言
这是一个小的堆栈实现,依赖于 MFC CArray
模板类。它实现了三个最常用的堆栈函数 Push
、Pop
和 Peek
。该类直接继承自 CArray
,因此也提供了所有继承的函数。
使用示例
CStack<int> m_stack; m_stack.Push( 1973 ); m_stack.Push( 2004 ); m_stack.Push( 30 ); int size = m_stack.GetSize();// an inherited CArray function. int value = m_stack.Peek(); // only returns the last inserted value int value = m_stack.Pop(); // returns the last inserted value // and removes it from stack.
实现
// Copyright (C) 2003 by Daniel Junges // http://junges.gmxhome.de/ // Written by Daniel Junges daniel-junges@gmx.net // All rights reserved // // This is free software. // This code may be used in compiled form in any way you desire. :-) // // Release Date and Version: // Version: 1.0 Mars 2003 ////////////////////////////////////////////////////////////////////// // #ifndef _H_TEMPLATE_H_ #define _H_TEMPLATE_H_ template <class T> class CStack : public CArray<T,T> { public: // Add element to top of stack void Push( T newView ){ Add( newView ); } // Peek at top element of stack T Peek(int index=-1){ return ( index >= GetSize() || GetSize()==0) ? NULL : ElementAt( ( index==-1?GetSize()-1:index) ); } // Pop top element off stack T Pop(){ T item = Peek(); if(item) RemoveAt(GetSize()-1); return item; } }; #endif