Click here to Skip to main content
Licence 
First Posted 18 Mar 2004
Views 41,239
Bookmarked 18 times

A light stack implementation for Win32

By | 18 Mar 2004 | Article
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

About the Author

Daniel Junges

Architect

Brazil Brazil

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralAnd what if PinmemberJörgen Sigvardsson6:45 19 Mar '04  
GeneralRe: And what if PinsussAnonymous4:39 20 Mar '04  
GeneralRe: And what if PinmemberJohn M. Drescher5:39 20 Mar '04  
GeneralRe: And what if PinmemberWREY8:30 20 Mar '04  
GeneralRe: And what if PinmemberJörgen Sigvardsson22:38 20 Mar '04  
GeneralRe: And what if PinmemberWREY2:53 21 Mar '04  
GeneralRe: And what if Pinmemberrfmobile4:32 21 Mar '04  
GeneralRe: And what if PinmemberWREY11:14 21 Mar '04  

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 19 Mar 2004
Article Copyright 2004 by Daniel Junges
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid