#ifndef __list_h__ #define __list_h__ #include #pragma warning(disable:4715) template class listNode { public: listNode(); listNode* next; ItemType data; }; template class list { public: list(); ~list(); list(const list&); bool IsEmpty(); bool IsFull(); void Insert(ItemType newItem); void Remove(ItemType target); void Remove(); ItemType Retrieve(); bool PrevPosition(); bool NextPosition(); bool IsFirstPosition(); bool IsLastPosition(); void FirstPosition(); void LastPosition(); void Clear(); void operator=(const list&); ItemType operator[](unsigned int); 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(); 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() { return head == NULL; } template bool list::IsFull() { return false; } template void list::Insert(ItemType newItem) { listNode* temp = head; if(head != NULL) { while(temp->next!= NULL && temp->data < newItem) { temp = temp->next; } if(temp == head) { tail->next = head; head = new listNode; head->next = tail->next; tail->next = NULL; temp = head; } else if(temp->data != newItem) { if(temp == tail) { tail->next = new listNode; tail = tail->next; temp = tail; } else { tail->next = temp->next; temp = temp->next = new listNode; temp->next = tail->next; tail->next = NULL; } } } else { temp = tail = head = new listNode; tail->next = NULL; } temp->data = newItem; } 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() { if(currPos != NULL) return currPos->data; } template bool list::IsFirstPosition() { return currPos == head; } template bool list::IsLastPosition() { return currPos == tail; } template void list::FirstPosition() { currPos = head; } template void list::LastPosition() { currPos = tail; } template ItemType list::operator[](unsigned int index) { if(head != NULL) { listNode* temp = head; while(index > 0 && temp != NULL) { temp = temp->next; --index; } if(temp != NULL) { return temp->data; } } } #include "list.cpp" #endif