//sortedList02.cpp
//date: 09/11/2002
//author: AOU
#include<iostream.h>
#include<math.h>
#include<stdlib.h>
#include<time.h>
const int ARRAY_SIZE = 10;
const int UNDEFINED = -9999;
class CSortedList
{
private:
int array[ARRAY_SIZE];
int count;
int min;
int max;
void sortBubble(void);
public:
CSortedList(void);
CSortedList(char ch);
void display(void);
bool insert(int x);
};
void main(void)
{
/*
{
CSortedList myList;
myList.display();
}
*/
/*
{
CSortedList myList('r');
myList.display();
}
*/
{
CSortedList myList;
myList.display();
myList.insert(64);
myList.display();
myList.insert(14);
myList.display();
myList.insert(16);
myList.display();
myList.insert(78);
myList.display();
//myList.sortBubble();
}
}
CSortedList::CSortedList(void)
{
count = 0;
min = UNDEFINED;
max = UNDEFINED;
}
CSortedList::CSortedList(char ch)
{
count = 2;
array[0] = 23;
array[1] = 31;
min = array[0];
max = array[count-1];
}
void CSortedList::display(void)
{
cout << "SortedList(" << count << ")= ";
for (int i=0; i<count; i++)
cout << array[i] << ' ';
cout << endl;
}
bool CSortedList::insert(int x)
{
//allow duplicate values
/*
there is no room then return false
there is room then add to the end, sort, return true
*/
if (ARRAY_SIZE == count)
return false;
else
{
array[count] = x;
count++;
sortBubble();
return true;
}
}
void CSortedList::sortBubble(void)
{
int j;
for (int i=1; i<=count; i++)
{
for (j=0; j<=count-2; j++)
{
if (array[j] > array[j+1])
{
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
}
|