#ifndef __list_h__ #define __list_h__ #include #pragma warning(disable:4715) typedef unsigned int LIST_ARRAY_INDEX_TYPE; template class listNode { public: listNode(); ItemType data; listNode* next; }; template class list { public: list(); ~list(); list(const list&); bool IsEmpty() const; bool IsFull() const; void Insert(ItemType newItem); void Remove(ItemType target); void Remove(); ItemType Retrieve() const; bool PrevPosition(); bool NextPosition(); bool IsFirstPosition() const; bool IsLastPosition() const; void FirstPosition(); void LastPosition(); void Clear(); bool Contains(ItemType) const; unsigned int Length(); void operator=(const list&); ItemType operator[](LIST_ARRAY_INDEX_TYPE) const; ItemType& operator[](LIST_ARRAY_INDEX_TYPE); private: listNode* head; listNode* tail; listNode* currPos; }; template listNode::listNode() { next = NULL; } template list::list() { head = NULL; tail = NULL; currPos = NULL; } template list::~list() { currPos = NULL; while(head != NULL) { tail = head; head = head->next; delete tail; tail = NULL; } } template list::list(const list& rtOp) { head = NULL; tail = NULL; currPos = NULL; operator=(rtOp); } template void list::Clear() { while(head) Remove(head->data); } template void list::operator=(const list& rtOp) { Clear(); if(!rtOp.IsEmpty()) { listNode* temp = rtOp.head->next; listNode* temp2 = NULL; if(temp != NULL) { head = new listNode; head->data = rtOp.head->data; tail = head; temp2 = head; } while (temp != NULL) { temp2->next = new listNode; temp2 = temp2->next; temp2->data = temp->data; temp = temp->next; } tail = temp2; } } template bool list::IsEmpty() const { return head == NULL; } template bool list::IsFull() const { return false; } template void list::Insert(ItemType newItem) { listNode* temp = head; listNode* temp2 = NULL; if(head == NULL) { temp = tail = head = new listNode; } else { if(newItem < head->data) { temp2 = head; temp = head = new listNode; head->next = temp2; temp2 = NULL; } else { //temp = head; while(temp->next != NULL && newItem > temp->next->data) { temp = temp->next; } temp2 = temp->next; temp = temp->next = new listNode; temp->next = temp2; temp2 = NULL; } } temp->data = newItem; if(temp->next == NULL) { tail = temp; } } template void list::Remove(ItemType target) { if(head != NULL) { listNode* temp = head; listNode* temp2 = head; while(temp->next != NULL && temp->data != target) { temp2 = temp; temp = temp->next; } if(temp->data == target) { if(temp != head) { temp2->next = temp->next; delete temp; } else { if(tail == head) tail = NULL; head = head->next; delete temp2; } } } } template bool list::PrevPosition() { if(currPos != head) { tail->next = currPos; currPos = head; if(currPos != tail->next) { while(currPos->next != tail->next) { currPos = currPos->next; } } tail->next = NULL; return true; } else { return false; } } template bool list::NextPosition() { if(currPos != tail) { currPos = currPos->next; return true; } else { return false; } } template void list::Remove() { if(currPos != NULL) { tail->next = currPos; currPos = currPos->next; delete tail->next; tail->next = NULL; } } template ItemType list::Retrieve() const { if(currPos != NULL) return currPos->data; } template bool list::IsFirstPosition() const { return currPos == head; } template bool list::IsLastPosition() const { return currPos == tail; } template void list::FirstPosition() { currPos = head; } template void list::LastPosition() { currPos = tail; } template ItemType list::operator[](LIST_ARRAY_INDEX_TYPE index) const { if(head == NULL) { //We should throw an exception here but instead I'll just return shit guess for now if somebody does this they're just fucked. } else { listNode* temp = head; LIST_ARRAY_INDEX_TYPE current; for(current=0;current next == NULL) { temp->next = new listNode; } temp = temp->next; } return temp->data; } } template ItemType& list::operator[](LIST_ARRAY_INDEX_TYPE index) { if(head == NULL) { tail = currPos = head = new listNode; return (ItemType&)(head->data); } else { listNode* temp = head; LIST_ARRAY_INDEX_TYPE current; for(current=0;current next == NULL) { temp->next = new listNode; } temp = temp->next; } return (ItemType&)(temp->data); } } template unsigned int list::Length() { if (head == NULL) { return 0; } else { int len = 1; listNode temp = head->next; while (temp != NULL) { temp = temp->next; len++; } return len; } } template bool list::Contains(ItemType value) const { listNode* temp = head; while(temp != NULL) { if(temp->data == value) { return true; } temp = temp->next; } return false; } #endif