Let me explain the data structure a bit to help make things more understandable; The render system is going to consist of an array of this 'list' type, with each index representing a different layer (background towards forground so that I can use blending effects, etc). This 'list' structure does the following:
When you add a node (a node consists of m_key, which represents the sprite being used for the entity, and a m_id which represents the entities unique id.) it cycles through to see if any entities of that m_key exist. If they do then it is attatched as a 'junction' to that node. If not it is added as a child to the last child node added.
I can currently add nodes for entities without any issues, however I am having trouble traversing this structure in order to delete a specific node that has a specific m_id. You can see my attempt bellow:
Code: Select all
/*
* List.h
* Core
*
* Created by Samuel Zeid on 24/01/10.
* Copyright 2010 Sam Zeid. All rights reserved.
*
*/
#define NULL 0
#ifndef _LIST_H
#define _LIST_H
struct Node
{
int m_key, m_id;
Node *m_child, *m_junction;
};
class List
{
public:
List()
{
m_root=NULL;
}
void addNode(int p_key, int m_id)
{
_insert(m_root, p_key, m_id);
}
void deleteKey(int p_key)
{
if(m_root!=NULL)
{
if(m_root->m_key==p_key)
{
Node *temp=m_root;
m_root=m_root->m_child;
_clear(temp->m_junction);
delete temp;
}
else
_deleteKey(m_root, p_key);
}
}
void deleteNode(int p_id)
{
_deleteNode(m_root,p_id);
}
void empty()
{
_clear(m_root);
}
void release()
{
_clear(m_root);
delete this;
}
private:
Node *m_root;
void _insert(Node *&n, int p_key, int p_id)
{
if(n==NULL)
{
n=new Node;
n->m_key=p_key;
n->m_id=p_id;
n->m_child=NULL;
n->m_junction=NULL;
}
else
{
if(n->m_key==p_key||n->m_key==0)
{
_insert(n->m_junction,p_key,p_id);
return;
}
_insert(n->m_child,p_key,p_id);
}
}
void _deleteKey(Node *&n, int p_key)
{
if(n->m_child==NULL)
return;
if (n->m_child->m_key==p_key)
{
Node *temp=n;
n->m_child=n;
_clear(temp->m_junction);
delete temp;
}
else
{
_deleteKey(n->m_child, p_key);
}
}
void _deleteNode(Node *&n, int p_id)
{
if(n==NULL)
return;
if(n->m_id==p_id)
{
if(n->m_junction!=NULL)
{
Node *temp=n->m_junction;
n->m_id=n->m_junction->m_id;
n->m_junction=n->m_junction->m_junction;
delete temp;
return;
}
if(n->m_child!=NULL)
{
Node *temp=n;
n=n->m_child;
delete temp;
return;
}
}
_deleteNode(n->m_junction,p_id);
_deleteNode(n->m_child,p_id);
}
void _clear(Node *&n)
{
if(n!=NULL)
{
_clear(n->m_child);
_clear(n->m_junction);
delete n;
}
}
};
#endif
Code: Select all
void deleteNode(int p_id)
{
_deleteNode(m_root,p_id);
}
void _deleteNode(Node *&n, int p_id)
{
if(n==NULL)
return;
if(n->m_id==p_id)
{
if(n->m_junction!=NULL)
{
Node *temp=n->m_junction;
n->m_id=n->m_junction->m_id;
n->m_junction=n->m_junction->m_junction;
delete temp;
return;
}
if(n->m_child!=NULL)
{
Node *temp=n;
n=n->m_child;
delete temp;
return;
}
}
_deleteNode(n->m_junction,p_id);
_deleteNode(n->m_child,p_id);
}