Template de pile.

Description

Template de pile.

Pour l'utiliser c'est tout simple il suffit d'inclure BStack.h.

Source / Exemple :


#ifndef __BSTACK_H__
#define __BSTACK_H__

// Revision history
// -------------------
//
// 1.0.0 First release

#pragma once

#include <assert.h>

template <class T> class BStack  
{
protected:
	struct	sStackItem
	{
		T			item;
		sStackItem	*pNextItem;
	};

	sStackItem	*_pFirstItem;
	DWORD		_nCount;

	void		suph(const T &newItem);
	void		copyStack(const BStack &stack);

public:
				BStack();
				BStack(const BStack &stack);
	virtual		~BStack();

	// Pushes an item to the head of the stack.
	//
	// [in] const T &newItem : item to push in the stack.
	void		Push(const T &newItem);
	// Pops an item of the head of the stack.
	//
	// Returns the poped item.
	T			Pop();

	// Removes all items of the stack.
	void		RemoveAll();

	// Returns TRUE if the stack is empty, FALSE otherwise.
	BOOL		IsEmpty();
	// Returns the item count of the stack.
	DWORD		Count();

	BStack<T>	&operator = (const BStack &stack);
};

template <class T> void BStack<T>::suph(const T &newItem)
{
	sStackItem	*pNewItem=new sStackItem;
	sStackItem	*pCurs;

	pNewItem->item=newItem;
	pNewItem->pNextItem=NULL;
	if(_pFirstItem==NULL)
		_pFirstItem=pNewItem;
	else
	{
		for(pCurs=_pFirstItem ; pCurs->pNextItem!=NULL ; )
			pCurs=pCurs->pNextItem;
		pCurs->pNextItem=pNewItem;
	}
	_nCount++;
}

template <class T> void BStack<T>::copyStack(const BStack &stack)
{
	sStackItem	*pItem;

	RemoveAll();

	for(pItem=stack._pFirstItem ; pItem!=NULL ; pItem=pItem->pNextItem)
		suph(pItem->item);
}

template <class T> BStack<T>::BStack()
{
	_pFirstItem=NULL;
	_nCount=0;
}

template <class T> BStack<T>::BStack(const BStack &stack)
{
	_pFirstItem=NULL;
	_nCount=0;

	copyStack(stack);
}

template <class T> BStack<T>::~BStack()
{
	RemoveAll();
}

template <class T> void BStack<T>::Push(const T &newItem)
{
	sStackItem	*pNewItem=new sStackItem;

	pNewItem->item=newItem;
	pNewItem->pNextItem=_pFirstItem;

	_pFirstItem=pNewItem;

	_nCount++;
}

template <class T> T BStack<T>::Pop()
{
	assert(_nCount>0 && _pFirstItem!=NULL);

	sStackItem	*pPopedItem=_pFirstItem;
	T			item=pPopedItem->item;

	_pFirstItem=_pFirstItem->pNextItem;
	delete pPopedItem;

	_nCount--;

	return item;
}

template <class T> void BStack<T>::RemoveAll()
{
	while(_nCount)
		Pop();
}

template <class T> BOOL BStack<T>::IsEmpty()
{
	return !_nCount;
}

template <class T> DWORD BStack<T>::Count()
{
	return _nCount;
}

template <class T> BStack<T> &BStack<T>::operator = (const BStack &stack)
{
	copyStack(stack);

	return *this;
}

#endif

Conclusion :


Pour les mises à jour consultez mon site http://perso.club-internet.fr/sfeldis. Pour une explication visitez http://perso.club-internet.fr/sfeldis/langages/c++/bstack/mainFrame.html.

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.