OB_List01
Home ] Up ]

 

//Date:   2003.09.19
//File:   OB_List01.cpp
//Author: AOU

#include <iostream.h>
#include <stdlib.h>
#include <time.h>


/*
maintain an ordered list of integeres
*/


////////////////////////////////////////
//constants
////////////////////////////////////////
const int MAX_SIZE = 25;
const int TEST_COUNT = 19;
const int MAX_VALUE = 15;
const int UNDEFINED = -911;


////////////////////////////////////////
//prototypes
////////////////////////////////////////
void testSortBubble(void);
void testInsert(void);
void testShuffle(void);



////////////////////////////////////////
//class COList
////////////////////////////////////////
class COList
  {
  private:
    int a[MAX_SIZE];
    int n;
  public:
    COList(void);
    void display(void);
    void initialize(void);
    bool insert(int x);
    void swap(int &x, int &y)
      {
      int temp = x;
      x = y;
      y = temp;
      };
    bool isSorted(void);
    void shuffle(void);
    void populate(void);
    void sortBubble(void);
  };


////////////////////////////////////////
//main
////////////////////////////////////////
void main(void)
  {
  srand(time(NULL));

  COList myList, youList, hisList;
  myList.display();

  myList.insert(12);
  myList.display();

  myList.insert(16);
  myList.display();

  myList.insert(14);
  myList.display();
  //testInsert();
  //testShuffle();
  //testSortBubble();
  }


bool COList::insert(int x)
  {
  if (0 == n)
      {
      a[n] = x;
      n++;
      return true;
      };

  if (MAX_SIZE == n)
      return false;

  if (n > 0)
      {
      a[n] = x;
      n++;

      for (int i=n-1; i>=1; i--)
        {
        if (a[i] >= a[i-1]) return true;
        swap(a[i], a[i-1]);
        }

      return true;
      }

  return false;

  }


void COList::display(void)
  {
  cout << "a[" << n << "]: ";
  for (int i=0; i<=n-1; i++)
    cout << a[i] << ' ';

  cout << endl;
  }


COList::COList(void)
  {
  cout << "BEING INITIALIZED\n";
  for (int i=0; i<=MAX_SIZE-1; i++)
    a[i] = UNDEFINED;

  n = 0;
  }