Click here to Skip to main content
15,881,248 members
Articles / Mobile Apps
Article

A light stack implementation for Win32

Rate me:
Please Sign up or sign in to vote.
1.19/5 (14 votes)
18 Mar 2004 52.3K   322   18   8
A simple and very small stack implementation for any type.

Introduction

This is a small stack implementation that is relied on MFC CArray template class. It has implemented the three mostly needed stack functions Push, Pop and Peek. The class is directly derived from CArray and consequently it supplies all inherited functions too.

Usage sample

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.

The implementation

// Copyright (C) 2003 by Daniel Junges 
// <A href="http://junges.gmxhome.de/">http://junges.gmxhome.de/</A>
// Written by Daniel Junges <A href="mailto:daniel-junges@gmx.net">daniel-junges@gmx.net</A>
// 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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Architect
Brazil Brazil
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralAnd what if Pin
Jörgen Sigvardsson19-Mar-04 6:45
Jörgen Sigvardsson19-Mar-04 6:45 
GeneralRe: And what if Pin
Anonymous20-Mar-04 4:39
Anonymous20-Mar-04 4:39 
GeneralRe: And what if Pin
John M. Drescher20-Mar-04 5:39
John M. Drescher20-Mar-04 5:39 
GeneralRe: And what if Pin
WREY20-Mar-04 8:30
WREY20-Mar-04 8:30 
GeneralRe: And what if Pin
Jörgen Sigvardsson20-Mar-04 22:38
Jörgen Sigvardsson20-Mar-04 22:38 
GeneralRe: And what if Pin
WREY21-Mar-04 2:53
WREY21-Mar-04 2:53 
GeneralRe: And what if Pin
unitrunker21-Mar-04 4:32
unitrunker21-Mar-04 4:32 
GeneralRe: And what if Pin
WREY21-Mar-04 11:14
WREY21-Mar-04 11:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.