All pastes #92703 Raw Edit

Classe Liste chane

public cpp v1 · immutable
#92703 ·published 2006-07-19 20:54 UTC
rendered paste body
#include <iostream>using namespace std;struct data{	void *data;	struct data *prev;	struct data *next;	} ;template <class T> class LinkedList {	public :		LinkedList();		~LinkedList();				void add(T &elemt);		 		void remove(); 		bool remove(T &elemt);				T & get();		void start();		T & next();		T & prev();		bool isfirst();		bool islast();	private :		data *first;		data *pcurrent;} ;template <class T> LinkedList<T>::LinkedList(){	this->first = new data;	this->pcurrent = this->first;}template <class T> LinkedList<T>::~LinkedList(){	this->start();	while (this->pcurrent->next != NULL)	{		this->pcurrent = this->pcurrent->next;		delete this->pcurrent->prev;	}	delete this->pcurrent;}template <class T> void LinkedList<T>::add(T &elemt){	data * newdata = new data;	newdata->data = (void *) &elemt;	if (this->pcurrent->next != NULL)	{		newdata->next = this->pcurrent->next;		newdata->next->prev = newdata;	}	newdata->prev = this->pcurrent;	this->pcurrent->next = newdata;	if (this->pcurrent == this->first)	{		this->next();	}}template <class T> void LinkedList<T>::remove(){	data * removed;	removed = this->pcurrent;	if (removed->prev == NULL && removed->next == NULL)	{		return;	}// 	else if (removed->prev == NULL)// 	{// 		this->next();// 		this->remove();// 		this->prev();// 		return;//	}	else	{		removed->prev->next = removed->next;		if (removed->next != NULL)		{			removed->next->prev = removed->prev;		}		this->pcurrent = removed->prev;		delete removed;	}}template <class T> bool LinkedList<T>::remove(T &elemt){	data * old_pcurrent = this->pcurrent;	this->start();	while (! this->islast())	{		if (&elemt == &this->next())		{			this->remove();			this->pcurrent = old_pcurrent;			return true;		}	}	this->pcurrent = old_pcurrent;	return false;}template <class T> T & LinkedList<T>::get(){	return * (T *) this->pcurrent->data;}template <class T> void LinkedList<T>::start(){	this->pcurrent = this->first;}template <class T> T & LinkedList<T>::next(){	if (this->pcurrent->next != NULL)	{		this->pcurrent = this->pcurrent->next;	}	return this->get();}template <class T> T & LinkedList<T>::prev(){	if (this->pcurrent->prev != NULL || this->pcurrent->prev != this->debut)	{		this->pcurrent = this->pcurrent->prev;	}	return this->get();	}template <class T> bool LinkedList<T>::isfirst(){	if (this->pcurrent->prev == first)	{		return true;	}	else	{		return false;	}}template <class T> bool LinkedList<T>::islast(){	if (this->pcurrent->next == NULL)	{		return true;	}	else	{		return false;	}}int main(int argc, char **argv){	int test = 42;	int test1 = 43;	int test2 = 44;	LinkedList<int> liste;	liste.add(test);// 	liste.next();	liste.add(test1);/*	liste.start();*/	liste.add(test2);// 	liste.start();	liste.remove(test1);	liste.start();	cout << liste.next() << endl;	cout << liste.next() << endl;	if (! liste.islast())	{		cout << liste.next() << endl;	}	return 0;}