#ifndef LIST_H
#define LIST_H
-#include <iostream.h>
+#include <iostream>
#include <cassert>
#include "listnode.h"
#include "aClient.h"
+#include "options.h"
+
+using std::cout;
+using std::endl;
+using std::flush;
+
+void log(const char *fmt, ...);
template <class T>
class List {
public:
List(); //constructor
~List(); //deconstructor
- void insertAtFront( const T & );
- void insertAtBack( T *&);
+ T *insertAtFront( const T & );
+ T *insertAtBack( T *&);
+ T *random(); // Returns a random element in the list
+ ListNode<T> *insertAtBack_RLN( T *&);
bool removeFromFront( T & );
bool removeFromBack( T & );
- bool remove( T * );
+ bool del( T *);
+ ListNode < T > *remove( T * );
bool isEmpty() const;
+ #ifdef DEBUGMODE
void print() const;
+ #endif
+ void deleteNodes();
+
ListNode < T > *Find( T * );
ListNode < T > *First() { return firstPtr; };
ListNode < T > *Last() { return lastPtr; };
+ int elements; // Number of elements in the list
private:
ListNode < T > *firstPtr;
ListNode < T > *lastPtr;
ListNode < T > *getNewNode ( const T & );
+ ListNode < T > *getNewNode ( T & );
};
template <class T>
-List<T>::List() : firstPtr (0), lastPtr (0) {}
+List<T>::List() : firstPtr (0), lastPtr (0) {elements = 0;}
template <class T>
-List<T>::~List()
+void List<T>::deleteNodes()
{
- if (!isEmpty())
+ if (isEmpty())
+ return;
+ ListNode<T> *it = firstPtr, *tempPtr;
+ while (it)
{
- cout << "Destroying Nodes" << endl;
+ tempPtr = it;
+ it = it->Next();
+ delete tempPtr;
+ }
+ firstPtr = NULL;
+ lastPtr = NULL;
+ elements = 0;
+}
- ListNode<T> *currentPtr = firstPtr, *tempPtr;
+template <class T>
+List<T>::~List()
+{
+ deleteNodes();
+}
- while (currentPtr)
- {
- tempPtr = currentPtr;
- currentPtr = currentPtr->Next();
-// if (!tempPtr->getData()->stats || tempPtr->getData()->stats->started == 0)
- cout << "Deleting Memory address: " << tempPtr->getData() << endl << flush;
-
- delete tempPtr;
-// else
-// tempPtr->getData()->stats->started = 0;
- }
- cout << "All Nodes destroyed" << endl;
+template<class T>
+T *List<T>::random()
+{
+ if (isEmpty())
+ return NULL;
+
+ int num = rand() % elements;
+ ListNode<T> *it = firstPtr;
+ for (int x = 0; x < num; x++)
+ {
+ it = it->Next();
}
+ return it->getData();
}
template<class T>
-void List<T>::insertAtFront( const T &value )
+T *List<T>::insertAtFront( const T &value )
{
ListNode<T> *newPtr = getNewNode ( value );
firstPtr->prev = newPtr;
firstPtr = newPtr;
}
+ elements++;
+ return firstPtr->getData();
+}
+
+template<class T>
+T *List<T>::insertAtBack(T *&value )
+{
+ ListNode<T> *newPtr = getNewNode(*value);
+
+ if (isEmpty())
+ {
+ firstPtr = lastPtr = newPtr;
+ }
+ else
+ {
+ newPtr->prev = lastPtr;
+ lastPtr->next = newPtr;
+ lastPtr = newPtr;
+ }
+ elements++;
+ return lastPtr->getData();
}
template<class T>
-void List<T>::insertAtBack(T *&value )
+ListNode<T> *List<T>::insertAtBack_RLN(T *&value )
{
ListNode<T> *newPtr = getNewNode(*value);
lastPtr->next = newPtr;
lastPtr = newPtr;
}
+ elements++;
+ return lastPtr;
}
value = tempPtr->getData();
delete tempPtr;
+ elements--;
return true;
}
}
value = tempPtr->getData();
delete tempPtr;
+ elements--;
return true;
-
}
}
return ptr;
}
+
+template <class T>
+ListNode<T> *List<T>::getNewNode( T &value)
+{
+ ListNode<T> *ptr = new ListNode<T>(value);
+
+ assert( ptr != 0);
+
+ return ptr;
+}
+
template<class T>
ListNode<T> *List<T>::Find( T *value )
{
currentPtr = firstPtr;
while (currentPtr)
{
+ cout << currentPtr->getData() << endl << value << endl;
if (currentPtr->getData() == value)
return currentPtr;
currentPtr = currentPtr->Next();
return NULL;
}
+#ifdef DEBUGMODE
template <class T>
void List<T>::print() const
{
currentPtr = firstPtr;
while (currentPtr)
{
+ /*
cout << "aClient: " << *currentPtr->getData() << flush;
if (currentPtr->getData()->stats)
cout << " Player Name: " << currentPtr->getData()->stats->name
<< " Password: " << currentPtr->getData()->stats->password << flush;
cout << endl;
+ */
+ /*
+ cout << currentPtr->getData()->getString() << endl;
+ currentPtr = currentPtr->next;
+ */
+ T *dat;
+ dat = currentPtr->getData();
+ log("name: %s", dat->name.getString());
+ log("weapon: %s", dat->weapon.getString());
+ log("death: %s", dat->death.getString());
+ log("strength: %d", dat->strength);
+ log("exp: %ld", dat->exp);
+ log("gold: %ld", dat->gold);
+ log("maxhp: %d", dat->maxhp);
+ log("hp: %d", dat->hp);
currentPtr = currentPtr->next;
}
+}
+#endif
+template <class T>
+bool List<T>::del(T *remPtr)
+{
+ ListNode<T> *removed = remove( remPtr );
+ if (removed != NULL)
+ {
+ delete removed;
+ return true;
+ }
+ return false;
}
template <class T>
-bool List<T>::remove(T *remPtr)
+ListNode<T> *List<T>::remove(T *remPtr)
{
ListNode<T> *newPtr = firstPtr;
T *testPtr;
{
if (firstPtr == lastPtr)
{
- cout << "One Element. Deleting it" << endl << flush;
+ #ifdef DEBUGMODE
+ log("One Element. Deleting it");
+ #endif
firstPtr = lastPtr = NULL;
- delete newPtr;
- return true;
+ return newPtr;
}
else if (newPtr != lastPtr && newPtr != firstPtr)
{
- cout << "Many elements, this one is in the middle. Deleting it"
- << ", linking front to back, and back to front." << endl << flush;
+ #ifdef DEBUGMODE
+ log("Many elements, this one is in the middle. Deleting it, linking front to back, and back to front.");
+ #endif
newPtr->prev->next = newPtr->next;
newPtr->next->prev = newPtr->prev;
- delete newPtr;
- return true;
+ return newPtr;
}
else if (newPtr == lastPtr)
{
- cout << "This was the last element. Deleting it, and pointing the tail to "
- << "its previous element." << endl << flush;
+ #ifdef DEBUGMODE
+ log("This was the last element. Deleting it, and pointing the tail to its previous element.");
+ #endif
lastPtr = newPtr->prev;
lastPtr->next = 0;
- delete newPtr;
- return true;
+ return newPtr;
}
else if (newPtr == firstPtr)
{
- cout << "This was the first element. Deleting it, and pointing the head to "
- << "its next element." << endl << flush;
+ #ifdef DEBUGMODE
+ log("This was the first element. Deleting it, and pointing the head to its next element.");
+ #endif
firstPtr = newPtr->next;
firstPtr->prev = 0;
- delete newPtr;
- return true;
+ return newPtr;
}
+ elements--;
}
newPtr = newPtr->next;
}
- return false;
+ return NULL;
}
#endif