Download Chapter Two: Abstract Data Types and Structures

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
1
Chapter Two: Abstract Data Types and Structures
The Ordered (Unsorted) Collection(List) Abstract Data Type

Abstract data type (ADT): a form of abstraction that arises from the
use of defined types. An ADT consists of a class of objects, a defined
set of properties of those objects, and a set of operation for processing
the objects.

Abstraction: the description of data structures at a conceptual level,
apart from their implementation using a particular technique &
language.

Physical size: the number of memory units (locations) available for
storing data items in a data structure.

Logical size: the number of data items actually available in a data
structure of a given time.

The importance of distinguishing between physical and logical size is
to prevent causing errors when users attempt to access data locations in
that portion of a vector where no data values have yet been stored.

Users can keep track of the data within a physical vector by
maintaining a separate integer variable (length) as a counter of the
number of data elements currently stored (logical size) in the vector.
Any indexed reference to a data element in a vector, should use an
index that satisfies the condition 0 <= index < length.
2

Ordered (unsorted) collection: a data structure that supports indexing
for retrieval or change of data items, the detection of the logical size of
the structure, & addition or removal of data items from the logical end
of the structure.

The ordered (unsorted) collection ADT is like a vector in that it
provides indexed access to data locations, and unlike a vector in
providing indexed access just to those data locations where elements
have been stored.

The ordered (unsorted) collection class provides its own logical size is
a property & provides a set of operations for adding or removing data
elements.

C++ Code:
#include < iostream.h >
#include “ordercol.h”
int main()
{
OrderedCollection< int > list;
int choice, value, index, k;
cout<<”[ 1 ] add first”<<endl;
cout<<”[ 2 ] add last”<<endl;
cout<<”[ 3 ] remove first”<<endl;
cout<<”[ 4 ] remove last”<<endl;
cout<<”[ 5 ] remove arbitrary”<<endl;
cout<<”[ 6 ] inspect index”<<endl;
cout<<”[ 7 ] change index”<<endl;
cout<<”[ 8 ] show collection”<<endl;
cout<<”[ 9 ] quit”<<endl;
do
{
cout<<”which operation should be tested?”;
cin>>choice;
switch( choice )
{
case 1:
cout<<”enter value:”;
cin>>value;
list.addFirst(value);
break;
case 2:
cout<<”enter value:”;
cin>>value;
3
list.addLast(value);
break;
case 3:
cout<<list.removeFirst()<<”has been
removed”<<endl;
break;
case 4:
cout<<list.removeLast()<<”has been
removed”<<endl;
break;
case 5:
cout<<”enter value to remove:”;
cin>>value;
if ( list.remove(value) )
cout<<value<<” has been removed”<<endl;
else
cout<<value<<” was not found”<<endl;
break;
case 6:
cout<<”enter index:”;
cin>>index;
cout<<”Value of index “<<index<<” is
“<<list[index];
break;
case 7:
cout<<”enter index and new value:”;
cin>>index>>value;
list[ index ] = value;
break;
case 8:
for ( k = 0 ; k < list.length() ; k++ )
cout<<list[ k ]<<” “;
cout<<endl;
break;
case 9:
break;
default:
cout<<”Invalid choice.. Please try again”<<endl;
break;
}
}
while ( choice != 9 );
return 0;
}
4

The operations for ordered (unsorted) collections
Declaring the ordered (unsorted) collection class in C++
const int MAX_VECTOR_SIZE = 50; //physical size
template < class E >
class OrderedCollection
{
public:
//class constructors
OrderedCollection();
OrderedCollection( const OrderedCollection &OC );//copy con
//function members
int length();
//assignment
OrderedCollection &operator=(const OrderedCollection &OC);
//indexing
E &operator[]( int index );
//Modifying
void addLast( E e );
E removeLast();
void addFirst( E e );
E removeFirst();
bool remove( E e );
Protected:
 Any derived classes of
//Data Members
Ordered Collection
apvector<E> data;
will have direct access to
int clength;
them, but client programs
};
will not.
 Function: What is the operation done?
 Preconditions: The state of the ADT before executes the
operation.
 Postconditions: The state of the ADT after executes the
operation.
 Implementation: The code in C++ for the operation.
Creation operation
 Function: To create an ordered (unsorted) collection.
 Preconditions: The OC is in an unpredicate state.
 Postconditions: An OC of length 0 is created.
 Implementation:
template< class E > //constructor
OrderedCollection<E>::OrderedCollection()  Member initializer to
:data( MAX_VECTOR_SIZE )
initialize data member data
{
to the value 50
clength = 0;
}
5
Copy operation
 Function: To copy an ordered collection to another one.
 Preconditions: The OC1 is created.
 Postconditions: An OC1 will be coped in OC2.
 Implementation:
copy( OC1,OC2 )
{
for ( int j=0 ; j < OC1.length ; ++j )
oc2.data[ j ] = OC1.data[ j ];
oc2.length = OC1.length;
}
Length Operation
 Function: To return the length of the OC.
 Preconditions: The OC is initialized.
 Postconditions: The number of data elements in the OC is
returned.
 Implementation:
template< class E >
int OrderedCollection<E>::length()
{
return clength;
}
Indexing Operation
 Function: To return the data element associated by that index.
 Preconditions: - The OC is initialized.
- The index parameter is an integer value in the
range 0 ≤ index < length of OC.
 Postconditions: The location of the data element as specified by
index, which can be used either to reference or
to store a value, is returned.
 Implementation:
6
template< class E >
E &OrderedCollection<E>::operator[]( int index )
{
assert ( ( index >= 0 ) && ( index < clength ) );
return data[ index ];
}
7
AddLast Operation
 Function: To add a new data element at the end of the collection.
 Preconditions: - The OC is initialized.
- The parameter is an object of type E(ORDER
COLLECTION TYPE).
- There is memory available to store the new item
in the collection.
 Postconditions: The length of the OC is incremented by 1, & the
parameter object is placed at the end of the OC.
 Implementation:
template< class E >
void OrderedCollection<E>::AddLast( E e )
{
assert( clength< MAX_VECTOR_SIZE );
data[ clength ] = e;
++clength;
}
RemoveLast Operation
 Function: To remove a data element from the end of the OC.
 Preconditions: The OC is initialized & the length of the OC > 0.
 Postconditions: The length of the OC is decreased by 1, & what
had been the last data element in the OC is returned.
 Implementation:
template< class E >
E OrderedCollection<E>::RemoveLast()
{
assert( clength > 0 );
--clength;
E removeItem = data[ clength ];
return removeItem;
}
8
AddFirst Operation
 Function: To add a new data element to the beginning of an OC.
 Preconditions: - The OC is initialized.
- The parameter is an object of type E.
- There is memory available to store the new item
in the OC.
 Postconditions: - The length of the OC is incremented by 1.
- The data elements in the OC are shifted up by
one index position.
- The parameter element is placed in the first
position in the OC.
 Implementation:
template< class E >
void OrderedCollection<E>::AddFirst( E e )
{
assert( clength < MAX_VECTOR_SIZE );
for ( int i=clength ; i > 0 ; --i )
data[ i ] = data[ i – 1 ];
data[0] = e;
++clength;
}
RemoveFirst Operation
 Function: To remove a data element from the beginning of an OC.
 Preconditions: The OC is initialized, and the length of the OC > 0.
 Postconditions: - The length of the OC is decreased by 1.
- The data elements in the OC that come after
the first element are shifted down by one index
position.
- What had been the first element in the OC is
returned.
9
 Implementation:
template< class E >
E OrderedCollection<E>::RemoveFirst()
{
assert( clength > 0 );
E removeItem = data[0];
for ( int i=0 ; i < clength - 1 ; ++i )
data[ i ] = data[ i + 1 ];
--clength;
return removeItem;
}
Assignment Operation
 Function: To assign the contents of the source object into the
target object.
 Preconditions: - The target is an OC, initialized.
- The source is an OC, initialized.
 Postconditions: The contents of the source object are copied into
the target object, and the target’s length is set to
the source’s length.
 Implementation:
template< class E >
OrderedCollection<E> &OrderedCollection<E>::operator=
( const OrderedCollection &oc )
{
if ( oc != this )
{
for ( int i=0 ; i < oc.clength ; ++i )
data[ i ] = oc.data[ i ];
clength = oc.clength;
}
return *this;
}
10
Remove Operation
 Function: To remove a data element from the OC.
 Preconditions: - The OC is initialized.
- The parameter is the data element to be
removed.
- The parameter must equal an element currently
in the list.
- The length of the OC > 0.
 Postconditions:
- The data elements in the OC that come after
the parameter element are shifted down by one
index position.
- The length of the OC is decreased by 1.
 Implementation:
template< class E >
E OrderedCollection<E>::remove( E e )
{
assert( clength > 0 );
for ( int j=0 ; j < clength ; ++j )
if ( data[ j ] == e )
{
E removeItem = data[ j ];
for ( int i=j ; i < clength - 1 ; ++i )
data[ i ] = data[ i + 1 ];
--clength;
}
return removeItem;
}
11
Evaluating the Implementation

ADT use rule: algorithms that use an ADT should access or modify the
ADT only through the operations provided in the ADT definition.

ADT implementation rule: An implementation of an ADT must
provide an interface that is entirely consistent with the operations
specified in the ADT’s formal definition.

Information hiding: the process of suppressing (hiding) the
implementation details of a function or data structure from higher-level
logic, so as to simplify its use in programming.