#include #include "ListT.h" using namespace std; ListT::ListT(){ size = 0; current = 0; } bool ListT::IsEmpty(void) const{ return size==0; } bool ListT::IsFull(void) const{ return size==MAX_ITEMS; } size_t ListT::Length(void) const{ return size; } size_t ListT::Find(int key) const { size_t pos=size; bool found = false; int small = 0; int large = static_cast(size)-1; int mid; while ( small <= large and not found ) { mid = (large+small) /2; if (data[mid] == key) { found = true; pos = mid; } else if (data[mid] > key) { large = mid-1; } else { small = mid+1; } } return pos; } bool ListT::IsPresent(int key) const{ size_t pos; pos = Find(key); return pos != size; } void ListT::Delete(int key){ bool found; size_t pos = Find(key); found = pos < size; while (found and pos < size-1) { data[pos] = data[pos+1]; pos++; } if (found) { size--; current = 0; } } void ListT::Insert(int key){ int pos; if (size < MAX_ITEMS) { // the position of the last data item pos = static_cast(size)-1; while (data[pos] > key and pos >= 0) { // it is bigger than key so move it down one to make room fo key data[pos+1] = data[pos]; pos --; } // put key where it belons data[pos+1] = key; size++; } } void ListT::Reset(void){ current = 0; } bool ListT::HasNext(void) const{ return current < size; } void ListT::Next(void){ if (current < size) { current ++; } } int ListT::GetItem(void) const{ int rv = 0; if (current < size) { rv = data[current]; } return rv; }