begin process at 2010 02 10 03:57:26
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Maths & Algorithmes

 > TEMPLATE DE PILE.

TEMPLATE DE PILE.


 Information sur la source

Note :
9 / 10 - par 2 personnes
9,00 / 10

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10
Catégorie :Maths & Algorithmes Niveau :Initié Date de création :01/12/2002 Date de mise à jour :01/12/2002 22:17:57 Vu / téléchargé :2 015 / 78

Auteur : phenix13

Ecrire un message privé
Site perso
Commentaire sur cette source (4)
Ajouter un commentaire et/ou une note

 Description

Template de pile.

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

Source

  • #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
#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.

 Fichier Zip

Les Membres Club peuvent télécharger directement un fichier contenu dans le zip sans télécharger le zip en entier !

Télécharger le zip


 Sources du même auteur

Source avec Zip TEMPLATE DE SKIP-LIST.
Source avec Zip TEMPLATE DE QUEUE OU FILE D?ATTENTE
Source avec Zip LISTE TEMPLATE DOUBLEMENT CHAINÉE

 Sources de la même categorie

Source avec Zip OPERATION SUR LES MATRICES CARREES AVEC CLASSE GENERIQUE par chouhad
Source avec une capture OPÉRATIONS SUR MATRICES C++ par Minilogus
[DEV-C++] CALCUL DE LA RACINE CARRÉE D'UN RÉEL par Jhep
PROGRAMME QUI CALCUL LE PPCM ET LE PGCD par AnoSantino
EVALUER UNE EXPRESSION MATHÉMATIQUE par begueradj

Commentaires et avis

Commentaire de pepet le 04/12/2002 10:42:00

pourquoi DWORD et BOOL? ce sont des types win.

Commentaire de trinitacs le 04/12/2002 16:00:37

pepet &gt;&gt; kaid a fait la même remarque dasn la source suivante ou précédente.

Tes header commençant par B ça vient de BeOS?

Commentaire de phenix13 le 05/12/2002 12:03:51

J'utilise DWORD et BOOL car je programme principalement avec Visual C++ sous Windows et que j'utilise les MFC. J'utilise ces types pour ne pas mixer différentes conventions d'écriture qui rendrait la relecture un peu difficile. Quand a ceux qui ne connaissent pas les types en question DWORD est un unsigned long et BOOL un int qui peut être aisément remplacé par le type bool.

Commentaire de phenix13 le 05/12/2002 12:06:16

Oui si je met des B devant mes classes c'est simplement parceque je suis un grand fan du génialissime BeOS.

 Ajouter un commentaire




Nos sponsors


Sondage...

Comparez les prix

CalendriCode

Février 2010
LMMJVSD
1234567
891011121314
15161718192021
22232425262728

Consulter la suite du CalendriCode

 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 0,484 sec (4)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales