SOURCE FILE: list.cpp
#include <string>
#include <iostream>
using namespace std;
const int MAX_LIST = 100 ;
typedef string ListItemType;
class List
{
public:
List(); // default constructor
// destructor is supplied by compiler
// list operations:
bool isEmpty() const;
// Determines whether a list is empty.
// Precondition: None.
// Postcondition: Returns true if the list is empty;
// otherwise returns false.
int getLength() const;
// Determines the length of a list.
// Precondition: None.
// Postcondition: Returns the number of items
// that are currently in the list.
void insert(int index, ListItemType newItem,
bool& success);
// Inserts an item into the list at position index.
// Precondition: index indicates the position at which
// the item should be inserted in the list.
// Postcondition: If insertion is successful, newItem is
// at position index in the list, and other items are
// renumbered accordingly, and success is true;
// otherwise success is false.
// Note: Insertion will not be successful if
// index < 1 or index > getLength()+1.
void remove(int index, bool& success);
// Deletes an item from the list at a given position.
// Precondition: index indicates where the deletion
// should occur.
// Postcondition: If 1 <= index <= getLength(),
// the item at position index in the list is
// deleted, other items are renumbered accordingly,
// and success is true; otherwise success is false.
void retrieve(int index, ListItemType& dataItem,
bool& success) const;
// Retrieves a list item by position.
// Precondition: index is the number of the item to
// be retrieved.
// Postcondition: If 1 <= index <= getLength(),
// dataItem is the value of the desired item and
// success is true; otherwise success is false.
void print() const;
/* output the elements of the list to standard output */
int seek(ListItemType key, bool & success) const ;
/* look in the list for an item (key) and, if found, report its
location, else report failure. Postconditions: If the key is found
the value returned is its logical position in the list (between 1 and
size) and success is set to 'true'. If the key is not found, then
success is set to 'false' and the function returns 0. */
void sort () ;
/*
Sort the items in the list, from smallest to largest.
*/
private:
ListItemType items[MAX_LIST]; // array of list items
int size; // number of items in list
int translate(int index) const;
// Converts the position of an item in a list to the
// correct index within its array representation.
int seekMax (int endPos) const ;
/* Locate a maximum item in the list between position 1 and position
endPos, and return its (logical) postition in the list.
*/
void swap (ListItemType & x, ListItemType & y) ;
/* interchange the two items. */
}; // end List class
List::List() : size(0)
{
} // end default constructor
bool List::isEmpty() const
{
return size == 0;
} // end isEmpty
int List::getLength() const
{
return size;
} // end getLength
void List::insert(int index, ListItemType newItem,
bool& success)
{
success = (index >= 1) &&
(index <= size + 1) &&
(size < MAX_LIST);
if (success)
{ // make room for new item by shifting all items at
// positions >= index toward the end of the
// list (no shift if index == size+1)
for (int pos = size; pos >= index; --pos)
items[translate(pos+1)] = items[translate(pos)];
// insert new item
items[translate(index)] = newItem;
++size; // increase the size of the list by one
} // end if
} // end insert
void List::remove(int index, bool& success)
{
success = (index >= 1) && (index <= size) ;
if (success)
{ // delete item by shifting all items at positions >
// index toward the beginning of the list
// (no shift if index == size)
for (int fromPosition = index+1;
fromPosition <= size; ++fromPosition)
items[translate(fromPosition-1)] = items[translate(fromPosition)];
--size; // decrease the size of the list by one
} // end if
} // end remove
void List::retrieve(int index, ListItemType& dataItem,
bool& success) const
{
success = bool( (index >= 1) && (index <= size) );
if (success)
dataItem = items[translate(index)];
} // end retrieve
int List::translate(int index) const
{
return index-1;
} // end translate
int List::seekMax (int endPos) const
{
int maxLoc = 1 ;
for (int i=2; i<=endPos; i++)
if (items[translate(i)] > items[translate(maxLoc)]) maxLoc = i ;
return maxLoc ;
}
void List::swap (ListItemType & x, ListItemType & y)
{
ListItemType temp ;
temp = x ;
x = y ;
y = temp ;
}
void List::print() const
{
ListItemType item ;
bool success ;
cout << endl ;
for (int j=1; j<=size; j++)
{
retrieve(j, item, success) ;
if (success) cout << item ;
else cout << "retreval failure" ;
cout << endl ;
}
cout << endl ;
}
int List::seek(ListItemType key, bool & success) const
{
int position = 0;
ListItemType item ;
bool ok ;
success = false ;
while ( !success && (position < size) )
{
position++ ;
retrieve (position, item, ok) ;
if (!ok)
{
cout << "retrieval failure\n" ;
exit(1) ;
}
else success = (item == key ) ;
}
return position ;
}
void List::sort ()
{
int maxLoc ;
for (int i=size; i>1; i--)
{
maxLoc = seekMax(i) ;
swap(items[translate(i)], items[translate(maxLoc)]) ;
}
}
// End of implementation code
int main ()
{
List my_list ;
bool success ;
my_list.insert(1, "Hello", success) ;
my_list.insert(1, "Hey", success) ;
my_list.insert(1, "Zymurgy", success) ;
my_list.insert(1, "Yawning", success) ;
my_list.print() ;
my_list.sort() ;
my_list.print() ;
int location = my_list.seek("Hello",success) ;
if (success)
cout << "The 'Hello' string is in position " << location ;
else cout << "The 'Hello' string could not be found" ;
cout << endl ;
location = my_list.seek("Yawning",success) ;
if (success)
cout << "The 'Yawning' string is in position " << location ;
else cout << "The 'Yawning' string could not be found" ;
cout << endl ;
location = my_list.seek("Jello",success) ;
if (success)
cout << "The 'Jello' string is in position " << location ;
else cout << "The 'Jello' string could not be found" ;
cout << endl ;
return 0 ;
}