X-Git-Url: http://shamusworld.gotdns.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;ds=sidebyside;f=src%2Flist.h;h=cc430d055b5ad56d364c8994a6faf96ed42749ad;hb=1881acb17ed405cdb5aa2cb333a7af77f644a86d;hp=da0501f6e863cb1d75fe0914c0b0cf8067a0c1a3;hpb=20195737e60552ca35c3212db45ece6c4db6bb1d;p=ttedit diff --git a/src/list.h b/src/list.h old mode 100755 new mode 100644 index da0501f..cc430d0 --- a/src/list.h +++ b/src/list.h @@ -1,279 +1,279 @@ -// -// A generic list class using a circular singly linked list -// by James L. Hammons -// (C) 2004 Underground Software -// -// Based upon work I did for CS240 Project 5 at Cal Poly Pomona for Dr. Bruce Hillam. -// Hello Dr. Hillam! :-) -// -// JLH = James L. Hammons -// -// Who When What -// --- ---------- ------------------------------------------------------------- -// JLH 08/30/1999 Created this file -// JLH 05/11/2004 Cleaned up a few things in the implementation -// - - -// Some exception constants - -#define LIST_EMPTY 1 -#define OUT_OF_RANGE 2 - -template class List; // Forward declaration... -template class Node -{ - friend class List; - - public: - Node(void): next(NULL) {} - Node(const T d, Node * ptr = NULL): data(d), next(ptr) {} - - private: - T data; - Node * next; -}; - -template class List -{ - // This class implements the following exceptions: - // LIST_EMPTY is thrown if you attempt to remove an object from an empty list. - // OUT_OF_RANGE is thrown if you attempt to Add or Get from a position that - // does not exist for the list. - - private: - Node * last; - int length; - - public: - List(void); - ~List(); - bool operator==(List &l); - void AddAtFront(T data); - void AddAtRear(T data); - void AddAtPosition(int pos, T data); - T GetFront(void); - T GetRear(void); - T GetPosition(int pos); - T PeekFront(void); - T PeekRear(void); - T PeekPosition(int pos); - bool IsEmpty(void); - int Length(void); -}; - -// -// List class implementation -// -template List::List(void) -{ - last = NULL; - length = 0; -} - -template List::~List() -{ - while (length) // Destroy all nodes that were created... - { - Node * tmp = last; - last = last->next; - delete tmp; - length--; - } -} - -template bool List::operator==(List &l) -{ - if (Length() != l.Length()) - return false; - - Node * ptr1 = last->next, * ptr2 = l.last->next; - - do - { - if (ptr1->data != ptr2->data) - return false; - - ptr1 = ptr1->next, ptr2 = ptr2->next; - } - while (ptr1 != last); - - return true; -} - -template void List::AddAtFront(T data) -{ - Node * newNode = new Node(data); - - if (last == NULL) // i.e., the list is empty... - last = newNode->next = newNode; - else - { - newNode->next = last->next; - last->next = newNode; - } - - length++; -} - -template void List::AddAtRear(T data) -{ - Node * newNode = new Node(data); - - if (last == NULL) // i.e., the list is empty... - last = newNode->next = newNode; - else - { - newNode->next = last->next; - last->next = newNode; - last = newNode; - } - - length++; -} - -template void List::AddAtPosition(int pos, T data) -{ - if ((pos < 1) || (pos > length)) - throw OUT_OF_RANGE; - - if (pos == 1) - AddAtFront(data); // Why reinvent the wheel? - - if (pos == length) - AddAtRear(data); - - Node * newNode = new Node(data), * tmp = last; - - for(int i=1; i!=pos; i++) // Make tmp point to node ahead of add position - tmp = tmp->next; - - newNode->next = tmp->next; - tmp->next = newNode; - - length++; -} - -template T List::GetFront(void) -{ - if (IsEmpty()) - throw LIST_EMPTY; - - T retVal = last->next->data; - Node * ptr = last->next; - - if (length == 1) - last = NULL; - else - last->next = last->next->next; - - delete ptr; - length--; - - return retVal; -} - -template T List::GetRear(void) -{ - if (IsEmpty()) - throw LIST_EMPTY; - - T retVal = last->data; - Node * ptr = last; - - if (length == 1) - { - delete last; - last = NULL; - } - else - { - while (ptr->next != last) - ptr = ptr->next; - - ptr->next = ptr->next->next; - delete last; - last = ptr; - } - - length--; - - return retVal; -} - -template T List::GetPosition(int pos) -{ - if (IsEmpty()) - throw LIST_EMPTY; - - if ((pos < 1) || (pos > length)) - throw OUT_OF_RANGE; - - if (pos == 1) - return GetFront(); // Why reinvent the wheel? - - if (pos == length) - return GetRear(); - - Node * tmp = last; - - for(int i=1; i!=pos; i++) // Make tmp point to node ahead of del position - tmp = tmp->next; - - Node * nodeToDelete = tmp->next; - T retVal = nodeToDelete->data; - - tmp->next = tmp->next->next; - delete nodeToDelete; - - length--; - return retVal; -} - -template T List::PeekFront(void) -{ - if (IsEmpty()) - throw LIST_EMPTY; - - return last->next->data; -} - -template T List::PeekRear(void) -{ - if (IsEmpty()) - throw LIST_EMPTY; - - return last->data; -} - -template T List::PeekPosition(int pos) -{ - if (IsEmpty()) - throw LIST_EMPTY; - - if ((pos < 1) || (pos > length)) - throw OUT_OF_RANGE; - - if (pos == 1) - return PeekFront(); // Why reinvent the wheel? - - if (pos == length) - return PeekRear(); - - Node * tmp = last; - - for(int i=1; i!=pos; i++) - tmp = tmp->next; - - return tmp->next->data; -} - -template inline bool List::IsEmpty(void) -{ - return (bool)(length == 0); -} - -template inline int List::Length(void) -{ - return length; -} +// +// A generic list class using a circular singly linked list +// by James Hammons +// (C) 2016 Underground Software +// +// Based upon work I did for CS240 Project 5 at Cal Poly Pomona for Dr. Bruce +// Hillam. Hello Dr. Hillam! :-) +// +// JLH = James L. Hammons +// +// Who When What +// --- ---------- ----------------------------------------------------------- +// JLH 08/30/1999 Created this file +// JLH 05/11/2004 Cleaned up a few things in the implementation +// + + +// Some exception constants + +#define LIST_EMPTY 1 +#define OUT_OF_RANGE 2 + +template class List; // Forward declaration... +template class Node +{ + friend class List; + + public: + Node(void): next(NULL) {} + Node(const T d, Node * ptr = NULL): data(d), next(ptr) {} + + private: + T data; + Node * next; +}; + +template class List +{ + // This class implements the following exceptions: + // LIST_EMPTY is thrown if you attempt to remove an object from an empty list. + // OUT_OF_RANGE is thrown if you attempt to Add or Get from a position that + // does not exist for the list. + + private: + Node * last; + int length; + + public: + List(void); + ~List(); + bool operator==(List &l); + void AddAtFront(T data); + void AddAtRear(T data); + void AddAtPosition(int pos, T data); + T GetFront(void); + T GetRear(void); + T GetPosition(int pos); + T PeekFront(void); + T PeekRear(void); + T PeekPosition(int pos); + bool IsEmpty(void); + int Length(void); +}; + +// +// List class implementation +// +template List::List(void) +{ + last = NULL; + length = 0; +} + +template List::~List() +{ + while (length) // Destroy all nodes that were created... + { + Node * tmp = last; + last = last->next; + delete tmp; + length--; + } +} + +template bool List::operator==(List &l) +{ + if (Length() != l.Length()) + return false; + + Node * ptr1 = last->next, * ptr2 = l.last->next; + + do + { + if (ptr1->data != ptr2->data) + return false; + + ptr1 = ptr1->next, ptr2 = ptr2->next; + } + while (ptr1 != last); + + return true; +} + +template void List::AddAtFront(T data) +{ + Node * newNode = new Node(data); + + if (last == NULL) // i.e., the list is empty... + last = newNode->next = newNode; + else + { + newNode->next = last->next; + last->next = newNode; + } + + length++; +} + +template void List::AddAtRear(T data) +{ + Node * newNode = new Node(data); + + if (last == NULL) // i.e., the list is empty... + last = newNode->next = newNode; + else + { + newNode->next = last->next; + last->next = newNode; + last = newNode; + } + + length++; +} + +template void List::AddAtPosition(int pos, T data) +{ + if ((pos < 1) || (pos > length)) + throw OUT_OF_RANGE; + + if (pos == 1) + AddAtFront(data); // Why reinvent the wheel? + + if (pos == length) + AddAtRear(data); + + Node * newNode = new Node(data), * tmp = last; + + for(int i=1; i!=pos; i++) // Make tmp point to node ahead of add position + tmp = tmp->next; + + newNode->next = tmp->next; + tmp->next = newNode; + + length++; +} + +template T List::GetFront(void) +{ + if (IsEmpty()) + throw LIST_EMPTY; + + T retVal = last->next->data; + Node * ptr = last->next; + + if (length == 1) + last = NULL; + else + last->next = last->next->next; + + delete ptr; + length--; + + return retVal; +} + +template T List::GetRear(void) +{ + if (IsEmpty()) + throw LIST_EMPTY; + + T retVal = last->data; + Node * ptr = last; + + if (length == 1) + { + delete last; + last = NULL; + } + else + { + while (ptr->next != last) + ptr = ptr->next; + + ptr->next = ptr->next->next; + delete last; + last = ptr; + } + + length--; + + return retVal; +} + +template T List::GetPosition(int pos) +{ + if (IsEmpty()) + throw LIST_EMPTY; + + if ((pos < 1) || (pos > length)) + throw OUT_OF_RANGE; + + if (pos == 1) + return GetFront(); // Why reinvent the wheel? + + if (pos == length) + return GetRear(); + + Node * tmp = last; + + for(int i=1; i!=pos; i++) // Make tmp point to node ahead of del position + tmp = tmp->next; + + Node * nodeToDelete = tmp->next; + T retVal = nodeToDelete->data; + + tmp->next = tmp->next->next; + delete nodeToDelete; + + length--; + return retVal; +} + +template T List::PeekFront(void) +{ + if (IsEmpty()) + throw LIST_EMPTY; + + return last->next->data; +} + +template T List::PeekRear(void) +{ + if (IsEmpty()) + throw LIST_EMPTY; + + return last->data; +} + +template T List::PeekPosition(int pos) +{ + if (IsEmpty()) + throw LIST_EMPTY; + + if ((pos < 1) || (pos > length)) + throw OUT_OF_RANGE; + + if (pos == 1) + return PeekFront(); // Why reinvent the wheel? + + if (pos == length) + return PeekRear(); + + Node * tmp = last; + + for(int i=1; i!=pos; i++) + tmp = tmp->next; + + return tmp->next->data; +} + +template inline bool List::IsEmpty(void) +{ + return (bool)(length == 0); +} + +template inline int List::Length(void) +{ + return length; +}