Click here to Skip to main content
15,888,579 members
Articles / Desktop Programming / MFC

Network Stream Class

Rate me:
Please Sign up or sign in to vote.
3.27/5 (10 votes)
6 May 20032 min read 56.4K   971   25  
A stream class to prepare data to be sent over the network
// File  : ListT.h
// Author: S�nke Freter + Hans Hamm alias Sendel
// Date  : 24.04.2003      
// What  : Linked List Template Class
//////////////////////////////////////////////////////////////////////

#ifndef _LISTT_H__
#define _LISTT_H__

#include <stdio.h>
#include "stack.h"

template <class type>
class ListT
{

protected:
	struct node_t
	{
		type		item;
		node_t *	next;
	};
	int m_Count;
	node_t *	m_Head;
	node_t *	m_Current;
	node_t *	m_Tail;

	Stack *		m_pStack_Settings;

	//void	push(type pItemToPush); || type	pop(); || type	pop(type pItemToPop);
	int help;

public:

	void	push(type pItemToPush);
	type	pop();
	type	pop(type pItemToPop);
	
	type 	getFirst();
	type 	getNext();
	void	pushSettings();
	void	popSettings();

	void	clear();
	int		getSize();
	type	operator[](int i);

	ListT*	getCopy();

	ListT();
	~ListT();

};


template <class type> 
ListT<type>::ListT()
{
	m_Tail		= NULL;
	m_Head		= NULL;
	m_Current	= NULL;
	m_Count		= 0;

	m_pStack_Settings = NULL;
}

template <class type> 
ListT<type>::~ListT()
{
	clear();
	SAVEDELETE(m_pStack_Settings);
}

template <class type> 
ListT<type>*	ListT<type>::getCopy(){
	ListT *tmp = new ListT();
	memmove(tmp, this, sizeof(ListT));
	
	node_t *	current = m_Tail;
	while( current )
	{
		tmp->push( current->item );
		current = current->next;
	}
	return tmp;
}

template <class type> 
void ListT<type>::push(type pItemToPush)
{
	// Neuen Knoten anlegen
	node_t *pNode = new node_t;
	pNode->item = pItemToPush;
		
	if (m_Head == NULL)  // nur 1 Element
	{
		m_Head		 = pNode;		
		m_Head->next = NULL;  // Neuen Zeiger auf Null zeigen lassen
		m_Tail = m_Head; // dann ist Head == Tail
	}
	else
	{
		m_Head->next = pNode; // alten Zeiger umbiegen
		m_Head		 = pNode;		
		m_Head->next = NULL;  // Neuen Zeiger auf Null zeigen lassen
	}
		m_Count++;
}

template <class type> 
type ListT<type>::pop() // von Tail 
{
	if (m_Tail != NULL)
	{
		node_t *pNode = m_Tail;			// den letzen Knoten merken
		type pItem = pNode->item;		// das Element merken
		if (m_Tail == m_Head)
		{
			m_Head = NULL;
		}
		m_Tail = m_Tail->next;			// einen weiter gehen
		
		SAVEDELETE(pNode);				// den Knoten l�schen
		m_Count--;						// einer Weniger in der ListTe!
		return pItem;					// Element zur�ckgeben
	}
	return NULL;
}

template <class type> 
type ListT<type>::pop(type pItemToPop)  // von Tail
{

	node_t * pNode = m_Tail;

	if (pNode == NULL)
	{
		return NULL;
	}

	if (pNode->next == NULL)
	{
		if (pNode->item == pItemToPop)
		{
			return pop();
		}
		else
		{
			return NULL;
		}
	}
	if (pNode->item == pItemToPop)
	{
		return pop();
	}

	while (pNode->next != NULL)
	{
		if (pNode->next->item == pItemToPop)
		{
			node_t *tmpNode =  pNode->next;
			type pItem = tmpNode->item;
			pNode->next = tmpNode->next;
			SAVEDELETE(tmpNode);
			m_Count--;
			return pItem;
		}
		pNode = pNode->next;
	}

	return NULL;	
}

template <class type> 
type  ListT<type>::getFirst()
{
	m_Current = m_Tail;
	if (m_Current != NULL)
	{
		return m_Current->item;
	}
	return NULL;
}

template <class type> 
type  ListT<type>::getNext()
{
	if (m_Current != NULL)
	{
		m_Current = m_Current->next;
		if (m_Current != NULL)
		{
			return m_Current->item;
		}
	}
	return NULL;
}

template <class type> 
void ListT<type>::pushSettings()
{
	if(m_pStack_Settings == NULL){
		m_pStack_Settings = new Stack();
	}
	m_pStack_Settings->push(m_Current);
}

template <class type> 
void ListT<type>::popSettings()
{
	if(m_pStack_Settings == NULL) return;

	typetmp = m_pStack_Settings->pop();
	if(tmp){
		m_Current = (node_t*)tmp;
	}
}

template <class type> 
void ListT<type>::clear()
{
	while (getFirst() != NULL)
	{
		pop();
	}
}

template <class type> 
int ListT<type>::getSize()
{
	return m_Count;
}

template <class type> 
type ListT<type>::operator [](int i)
{
	type tmp;
	tmp = getFirst();
	for (int t=0; t<i; t++)
	{
		tmp = getNext();
	}
	return tmp;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////



#endif 

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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
Software Developer (Senior) Deep Silver FISHLABS
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions