unsignedint seed; //a random seed
int num; //to hold an int value
arrayListType<int> list(100); //create a list of int type
cout<<"Enter a random seed => "; //get the random seed
cin >> seed;
srand(seed); //set the randon seed
for (int i=0; i < 10; i++) //insert 50 random numbers into the list
{
num = rand();
if (i%2==0)
list.insertFirst(num);
else
list.insertLast(num);
}
list.print();
do
{
cout<<"Enter a number to search or stop when -99 is entered => ";
cin >> num;
if (list.search(num))
cout << "found " <<num << " in the list. " <<endl;
else
cout << "failed to find " <<num << " in the list. " <<endl;
}while (num!=-99);
Enter a random seed => 12
11981 12947 1558 6232 77
5628 29052 26150 29926 22371
Enter a number to search or stop when -99 is entered => 77
found 77 in the list.
Enter a number to search or stop when -99 is entered =>
arrayListType<string> list(100);
srand((unsignedint)time(0));
string word;
int len;
for (int i=0; i < 5; i++)
{
word = " ";
len = 3 + rand()%10;
for(int j = 0; j < len; j++)
{
word = word + (char)('A' + rand()%26);
}
if (i%2==0)
list.insertFirst(word);
else
list.insertLast(word);
}
list.print();
do
{
cout<<"Enter a word to search or stop when No is entered => ";
cin >> word;
if (list.search(word))
cout << "found " <<word << " in the list. " <<endl;
else
cout << "failed to find " <<word << " in the list. " <<endl;
}while (word!= "No");
JXB BXYZRKJCR XDDNP AZZ ALHVOCK
Enter a word to search or stop when No is entered => JXB
failed to find JXB in the list.
Enter a word to search or stop when No is entered =>
template <class Type>
bool arrayListType<Type>::search(const Type & searchItem)
{
bool found = false; //flag for founding status
int i = 0; //point to first
while (!found && i < count)
{
if (list[i] == searchItem)
found = true; //find the searchItem
else //otherwise move to the next node
i++;
}
return found;
}