]> jfr.im git - irc/gameservirc.git/blobdiff - gameserv/list.h
* Added some admin features.
[irc/gameservirc.git] / gameserv / list.h
index c1dcda96395359ddb530ffdc2e32a5bb3f7b1313..384e9ada364929a5a3dbed97e67ac8ed553d0e2a 100644 (file)
@@ -6,6 +6,7 @@
 #include "listnode.h"
 #include "aClient.h"
 
+
 template <class T>
 class List {
     public:
@@ -18,6 +19,7 @@ class List {
        bool remove( T * );
        bool isEmpty() const;
        void print() const;
+       ListNode < T > *Find( T * );
         ListNode < T > *First() { return firstPtr; };
        ListNode < T > *Last() { return lastPtr; };
     private:
@@ -39,11 +41,13 @@ List<T>::~List()
 
        ListNode<T> *currentPtr = firstPtr, *tempPtr;
 
-       while (currentPtr != 0
+       while (currentPtr) 
        {
            tempPtr = currentPtr;
-           currentPtr = currentPtr->next;
+           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;
@@ -129,7 +133,7 @@ bool List<T>::removeFromBack( T &value )
 
 template <class T>
 bool List<T>::isEmpty() const
-   { return firstPtr == 0; }
+   { return firstPtr == 0 && lastPtr == 0; }
 
 template <class T>
 ListNode<T> *List<T>::getNewNode( const T &value)
@@ -140,6 +144,21 @@ ListNode<T> *List<T>::getNewNode( const T &value)
 
     return ptr;
 }
+template<class T>
+ListNode<T> *List<T>::Find( T *value )
+{
+    if (isEmpty()) {return NULL;}
+
+    ListNode<T> *currentPtr;
+    currentPtr = firstPtr;
+    while (currentPtr)
+    {
+       if (currentPtr->getData() == value)
+           return currentPtr;
+       currentPtr = currentPtr->Next();
+    }
+    return NULL;
+}
 
 template <class T>
 void List<T>::print() const
@@ -154,15 +173,15 @@ void List<T>::print() const
     currentPtr = firstPtr;
     while (currentPtr)
     {
-       cout << "aClient: " << *currentPtr->getData();
+       cout << "aClient: " << *currentPtr->getData() << flush;
 
         if (currentPtr->getData()->stats)
-           cout << "  Player Name: " << currentPtr->getData()->stats->name;
+           cout << "  Player Name: " << currentPtr->getData()->stats->name 
+                << "   Password: " << currentPtr->getData()->stats->password << flush;
        cout << endl;
        currentPtr = currentPtr->next;
     }
 
-    cout << endl;
 }
 
 template <class T>
@@ -171,6 +190,9 @@ bool List<T>::remove(T *remPtr)
     ListNode<T> *newPtr = firstPtr;
     T *testPtr;
 
+    if (isEmpty())
+       return false;
+
     while (newPtr)
     {
        testPtr = newPtr->getData();
@@ -178,12 +200,15 @@ bool List<T>::remove(T *remPtr)
        {
            if (firstPtr == lastPtr)
            {
-               firstPtr = lastPtr = 0; 
+               cout << "One Element. Deleting it" << endl << flush;
+               firstPtr = lastPtr = NULL;
                delete newPtr;
                return true;
            }
            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;
                newPtr->prev->next = newPtr->next;
                newPtr->next->prev = newPtr->prev;
                delete newPtr;
@@ -191,6 +216,8 @@ bool List<T>::remove(T *remPtr)
            }
            else if (newPtr == lastPtr)
            {
+               cout << "This was the last element. Deleting it, and pointing the tail to "
+                    << "its previous element." << endl << flush;
                lastPtr = newPtr->prev;
                lastPtr->next = 0;
                delete newPtr;
@@ -198,6 +225,8 @@ bool List<T>::remove(T *remPtr)
            }
            else if (newPtr == firstPtr)
            {
+               cout << "This was the first element. Deleting it, and pointing the head to "
+                    << "its next element." << endl << flush;
                firstPtr = newPtr->next;
                firstPtr->prev = 0;
                delete newPtr;