home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-09-26 | 7.0 KB | 251 lines | [TEXT/CWIE] |
- // Start of bit added by Ken
-
- #include "ooflist.hpp"
-
- // -------------------------------------------------------
- // O O F I L E _ L i s t E l e m e n t
- // -------------------------------------------------------
-
- OOF_ListElement::OOF_ListElement(unsigned long theItem)
- {
- mItem = theItem;
- mNext = 0; // assume we'll put it at the end of the list
- }
-
- // -------------------------------------------------------
- // O O F I L E _ L i s t
- // -------------------------------------------------------
-
- //----------------------------------------------------------------------
- // OOF_List::OOF_List
- // Initialize a list, empty to start with.
- // Elements can now be added to the list.
- //----------------------------------------------------------------------
-
- OOF_List::OOF_List()
- {
- mCount = 0;
- mFirst = mLast = 0;
- }
-
- //----------------------------------------------------------------------
- // OOF_List::~OOF_List
- // prepare a list for deallocation. If the list still contains any
- // ListElements, de-allocate them. However, note that we do *not*
- // de-allocate the "items" on the list -- this module allocates
- // and de-allocates the ListElements to keep track of each item,
- // but a given item may be on multiple lists, so we can't
- // de-allocate them here.
- //----------------------------------------------------------------------
-
- OOF_List::~OOF_List()
- {
- while (remove() != 0)
- ; // delete all the list elements
- }
-
- //----------------------------------------------------------------------
- // OOF_List::append
- // append an "item" to the end of the list.
- //
- // Allocate a ListElement to keep track of the item.
- // If the list is empty, then this will be the only element.
- // Otherwise, put it at the end.
- //
- // "item" is the thing to put on the list, it can be a pointer to
- // anything.
- //----------------------------------------------------------------------
-
- void
- OOF_List::append(unsigned long theItem)
- {
- OOF_ListElement *element = new OOF_ListElement(theItem);
-
- mCount++;
- if (isEmpty()) { // list is empty
- mFirst = element;
- mLast = element;
- } else { // else put it after last
- mLast->mNext = element;
- mLast = element;
- }
- }
-
- //----------------------------------------------------------------------
- // OOF_List::prepend
- // Put an "item" on the front of the list.
- //
- // Allocate a ListElement to keep track of the item.
- // If the list is empty, then this will be the only element.
- // Otherwise, put it at the beginning.
- //
- // "item" is the thing to put on the list, it can be a pointer to
- // anything.
- //----------------------------------------------------------------------
-
- void
- OOF_List::prepend(unsigned long theItem)
- {
- OOF_ListElement *element = new OOF_ListElement(theItem);
-
- mCount++;
- if (isEmpty()) { // list is empty
- mFirst = element;
- mLast = element;
- } else { // else put it before first
- element->mNext = mFirst;
- mFirst = element;
- }
- }
-
- //----------------------------------------------------------------------
- // OOF_List::remove
- // remove the first "item" from the front of the list.
- //
- // Returns:
- // Removed item, 0 if nothing on the list.
- //----------------------------------------------------------------------
-
- unsigned long
- OOF_List::remove()
- {
- OOF_ListElement *element = mFirst;
- unsigned long thing;
-
- if (isEmpty())
- return 0;
-
- thing = mFirst->mItem;
- if (mFirst == mLast) { // list had one item, now has none
- mFirst = 0;
- mLast = 0;
- } else {
- mFirst = element->mNext;
- }
- mCount--;
- delete element;
- return thing;
- }
-
- //----------------------------------------------------------------------
- // OOF_List::isEmpty
- // Returns true if the list is empty (has no items).
- //----------------------------------------------------------------------
-
- bool
- OOF_List::isEmpty() const
- {
- if (mFirst == 0)
- return true;
- else
- return false;
- }
-
- //----------------------------------------------------------------------
- // OOF_List::member
- // Returns true if the list contains theItem.
- //----------------------------------------------------------------------
-
- bool
- OOF_List::member(unsigned long theItem) const
- {
- if (mFirst != 0)
- {
- OOF_ListElement *ptr; // keep track
-
- for (ptr = mFirst; ptr->mNext != 0; ptr = ptr->mNext)
- {
- if(ptr->mItem == theItem)
- return(true);
- }
- }
- return false;
- }
-
- //----------------------------------------------------------------------
- // OOF_List::sortedInsert
- // Insert an "item" into a list, so that the list elements are
- // sorted in increasing order.
- //
- // Allocate a ListElement to keep track of the item.
- // If the list is empty, then this will be the only element.
- // Otherwise, walk through the list, one element at a time,
- // to find where the new item should be placed.
- //----------------------------------------------------------------------
-
- void
- OOF_List::sortedInsert(unsigned long theItem)
- {
- OOF_ListElement *element = new OOF_ListElement(theItem);
- OOF_ListElement *ptr; // keep track
-
- mCount++;
- if (isEmpty()) { // if list is empty, put
- mFirst = element;
- mLast = element;
- } else if (theItem < mFirst->mItem) {
- // item goes on front of list
- element->mNext = mFirst;
- mFirst = element;
- } else { // look for mFirst one in list bigger than item
- for (ptr = mFirst; ptr->mNext != 0; ptr = ptr->mNext) {
- if (theItem < ptr->mNext->mItem) {
- element->mNext = ptr->mNext;
- ptr->mNext = element;
- return;
- }
- }
- mLast->mNext = element; // item goes at end of list
- mLast = element;
- }
- }
-
- //----------------------------------------------------------------------
- // List::sortedInsertNoDups
- // Insert an "item" into a list, so that the list elements are
- // sorted in increasing order and there are no duplicates.
- //
- // Allocate a ListElement to keep track of the item.
- // If the list is empty, then this will be the only element.
- // Otherwise, walk through the list, one element at a time,
- // to find where the new item should be placed.
- //----------------------------------------------------------------------
-
- void
- OOF_List::sortedInsertNoDups(unsigned long theItem)
- {
- OOF_ListElement *element = new OOF_ListElement(theItem);
- OOF_ListElement *ptr; // keep track
-
- if (isEmpty()) { // if list is empty, put
- mFirst = element;
- mLast = element;
- } else if (theItem < mFirst->mItem) {
- // item goes on front of list
- element->mNext = mFirst;
- mFirst = element;
- } else { // look for first one in list bigger than item
- for (ptr = mFirst; ptr->mNext != 0; ptr = ptr->mNext) {
- if (theItem < ptr->mNext->mItem) {
- if (theItem == ptr->mItem) {
- delete element;
- return;
- } else {
- element->mNext = ptr->mNext;
- ptr->mNext = element;
- mCount++;
- return;
- }
- }
- }
- mLast->mNext = element; // item goes at end of list
- mLast = element;
- }
- }
-
- unsigned long OOF_List::count() const
- {
- return mCount;
- }
- // End of bit added by Ken
-