|
// File : List.cpp
// Author: Hans Hamm alias Sendel
// Date : 24.04.2003
// What : Pointer Linked List Class
//////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <string.h>
#include "List.h"
List::List()
{
m_Tail = NULL;
m_Head = NULL;
m_Current = NULL;
m_Count = 0;
m_pStack_Settings = NULL;
}
List::~List()
{
clear();
SAVEDELETE(m_pStack_Settings);
}
void List::push(void *pItemToPush)
{
if (pItemToPush != NULL)
{
// 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++;
}
}
void * List::pop() // von Tail
{
if (m_Tail != NULL)
{
node_t *pNode = m_Tail; // den letzen Knoten merken
void * 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 Liste!
return pItem; // Element zur�ckgeben
}
return NULL;
}
void * List::pop(void *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;
void * pItem = tmpNode->item;
pNode->next = tmpNode->next;
SAVEDELETE(tmpNode);
m_Count--;
return pItem;
}
pNode = pNode->next;
}
return NULL;
}
void * List::getFirst()
{
m_Current = m_Tail;
if (m_Current != NULL)
{
return m_Current->item;
}
return NULL;
}
void * List::getNext()
{
if (m_Current != NULL)
{
m_Current = m_Current->next;
if (m_Current != NULL)
{
return m_Current->item;
}
}
return NULL;
}
void List::pushSettings()
{
if(m_pStack_Settings == NULL){
m_pStack_Settings = new Stack();
}
m_pStack_Settings->push(m_Current);
}
void List::popSettings()
{
if(m_pStack_Settings == NULL) return;
void *tmp = m_pStack_Settings->pop();
if(tmp){
m_Current = (node_t*)tmp;
}
}
void List::clear()
{
while (getFirst() != NULL)
{
pop();
}
}
int List::getSize()
{
return m_Count;
}
void * List::operator [](int i)
{
void * tmp;
tmp = getFirst();
for (int t=0; t<i; t++)
{
tmp = getNext();
}
return tmp;
}
|
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.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.