
//*****************************************************************************
////  Course:             COMS 2203 Foundations of Computer Programming II Sect. 2
////  Semester:           Fall 2004
////  Assignment Number:  #8                             
////  Author Name:        DarC KonQuesT                        
////  Date Written:       October 28, 2004
////                                                         
////  Description of Program:                              
////  Implementation file of OrderedListType.h, allows for construction,   
////  destruction, insertion, removal, and printing.                             
////*****************************************************************************

#include "OrderedListType.h"
#include <iostream>

using namespace std;

OrderedListType::OrderedListType()
{
  a = new int[MAX];
  n = 0;
  size = MAX;
  for(int i = 0; i <= MAX; i++)
      a[i] = 0;
  
}


OrderedListType::OrderedListType(int max)
{
  a = new int[max];
  n = 0;
  size = max;
  for(int i = 0; i <= max; i++)
    a[i] = 0;
}

OrderedListType::~OrderedListType()
{
  delete [] a;
}


void OrderedListType::insert(int x)
{
  int index1, index2;
  if (n == 0 || a[n - 1] < x)
    a[n] = x;
  else
    for (index1 = 0; index1 < n; index1++)
      if(a[index1] > x)
      {
        for(index2 = (n - 1); index2 >= index1; index2--)
        a[index2 + 1] = a[index2];
        a[index1] = x; 
        break;
      }
       n++;
}

void OrderedListType::remove(int x)
{
   int loc = 0;
   while(loc < n && a[loc] != x) ++loc;
   if(loc < n) 
   {
     for(int i = loc; i < n - 1; ++i)
     a[i] = a[i + 1];
     --n;
    }
}

void  OrderedListType::print()
{
  for(int i = 0; i < n; ++i)
    cout << a[i] << " ";
  cout << endl << endl;
    
}

