Chapter3 Adt PDF
Chapter3 Adt PDF
Chapter3 Adt PDF
Outline
• What is a list?
C++ Data Types
Simple
Composite
Integral Floating
array struct union class
pointer reference
Concept: ADT List
• Abstract Data Type: A data type whose properties
(domain and operations) are specified independently
of any particular implementation.
• ADT list is a homogeneous collection of elements,
with linear relationship among elements
• each element except the first has a unique
predecessor, and each element except the last has a
unique successor.)
• Each element in list can be of any type: simple,
composite, address, or list, …
• Often times, element is a struct or a class type
• one field/attribute of the struct/class is used as key
field (used to search for element, sort elements)
Examples
You anticipate a busy day ahead, and decide to write down tasks
that you need to finish:
Typically you write down things as they occur to you…
class ItemType
ComparedTo
Private data
Print
value
Initialize
// SPECIFICATION FILE ( unsorted.h )
#include “ItemType.h”
private :
// 3 private data members
int length;
ItemType info[MAX_ITEMS];
int currentPos;
};
Class Constructor Rules
1 A constructor cannot return a function value, and has
no return value type.
2 A class may have several constructors. The compiler
chooses the appropriate constructor by the number and
types of parameters used.
3 Constructor parameters are placed in a parameter list in
the declaration of the class object.
4 The parameterless constructor is the default
constructor.
5 If a class has at least one constructor, and an array of
class objects is declared, then one of the constructors
must be the default constructor, which is invoked for
each element in the array.
Class Interface Diagram
UnsortedType class
UnsortedType
Private data:
IsFull
length
GetLength
info [0]
GetItem [1]
[2]
PutItem
[MAX_ITEMS-1]
DeleteItem
currentPos this (using static
ResetList array) is just one
GetNextItem way to
implement it…
// IMPLEMENTATION FILE ARRAY-BASED LIST ( unsorted.cpp )
#include “itemtype.h”
void UnsortedType::UnsortedType ( )
// Pre: None.
// Post: List is empty.
{
length = 0;
}
[MAX_ITEMS-1]
After Inserting Hsing into an
Unsorted List
length 4
[1] Bradley
[2] Asad
[3] Hsing
.
.
.
[MAX_ITEMS-1]
int UnsortedType::GetLength( ) const
// Pre: List has been inititalized.
// Post: Function value == ( number of elements in
// list ).
{
return length;
}
found = false;
moreToSearch = ( location < length );
while ( moreToSearch && !found )
{ switch ( item.ComparedTo( info[location] ) )
{ case LESS :
case GREATER : location++;
moreToSearch = ( location < length );
case EQUAL : found = true;
item = info[ location ];
break;
}
}
return item
}
18
Getting Ivan from an Unsorted List
[MAX_ITEMS-1]
Getting Ivan from an Unsorted List
[MAX_ITEMS-1]
Getting Ivan from an Unsorted List
length 4 moreToSearch: true
found: false
info [0] Maxwell
location: 2
[1] Bradley
[2] Asad
[3] Hsing
.
.
.
[MAX_ITEMS-1]
Getting Ivan from an Unsorted List
length 4 moreToSearch: true
found: false
info [0] Maxwell
location: 3
[1] Bradley
[2] Asad
[3] Hsing
.
.
.
[MAX_ITEMS-1]
Getting Ivan from an Unsorted List
[MAX_ITEMS-1]
void UnsortedType::DeleteItem ( ItemType item )
// Pre: item’s key has been inititalized.
// An element in the list has a key that matches item’s.
// Post: No element in the list has a key that matches item’s.
{
int location = 0 ;
[1] Bradley
[2] Asad Key Bradley has
not been matched.
[3] Hsing
.
.
.
[MAX_ITEMS-1]
Deleting Bradley from an Unsorted List
length 4
[1] Bradley
[2] Asad
[3] Hsing Key Bradley has
. been matched.
.
.
[MAX_ITEMS-1]
Deleting Bradley from an Unsorted List
length 4
location: 1
info [0] Maxwell
[1] Hsing
[2] Asad Placed copy of
[3] Hsing last list element
into the position
.
where the key Bradley
.
was before.
.
[MAX_ITEMS-1]
Deleting Bradley from an
Unsorted List
length 3
location: 1
info [0] Maxwell
[1] Hsing
[2] Asad Decremented length.
[3] Hsing
.
.
.
[MAX_ITEMS-1]
void UnsortedType::ResetList ( )
// Pre: List has been inititalized.
// Post: Current position is prior to first element in list.
{
currentPos = -1;
}
ItemType UnsortedType::GetNextItem ()
// Pre: List has been initialized. Current position is defined.
// Element at current position is not last in list.
// Post: Current position is updated to next position.
// item is a copy of element at current position.
{
currentPos++;
item = info [currentPos];
}
Example: Iterating through an unsorted list
#include “itemtype.h”
#include <iostream>
• cpUnsortedList
• Now we can use any data type that have overloaded comparison
operators with unsorted list:
int* ptr;
// ptr will hold the address of an int
int x; 2000
x = 12; 12
x
3000
int* ptr;
ptr = &x; 2000
ptr
std::cout << *ptr;
int x; 2000
x = 12; 12 5
x
char* q; ch
Simple
Structured
Integral Floating
array struct union class
pointer reference 38
Dynamically Allocated Data
2000
char* ptr;
ptr
ptr = new char;
*ptr = ‘B’;
std::cout << *ptr;
New is an operator
Dynamically Allocated Data
2000
char* ptr;
ptr
2000
char* ptr;
ptr
3
ptr
4
Memory Leak
A memory leak occurs when dynamic memory (that was created
using operator new) has been left without a pointer to it by the
programmer, and so is inaccessible.
8
ptr
-5
ptr2
A Dangling Pointer
• occurs when two pointers point to the same object and delete is
applied to one of them.
-5
ptr2
FOR EXAMPLE,
Leaving a Dangling Pointer
NULL
ptr2
Transformers
• MakeEmpty
• PutItem change state
• DeleteItem
Observers
• IsFull
• GetLength observe state
• GetItem
Iterators
• ResetList
process all
• GetNextItem
#include “ItemType.h” // unsorted.h
. . .
class UnsortedType
{
public : // LINKED LIST IMPLEMENTATION
// The public interface is the same
private :
// The private part is different
NodeType<ItemType>* listData;
int length;
NodeType<ItemType>* currentPos;
};
53
class UnsortedType<char>
UnsortedType
Private data:
MakeEmpty
length 3
~UnsortedType
listData ‘X’ ‘C’ ‘L’
GetItem
currentPos ?
PutItem
DeleteItem
.
.
.
GetNextItem
// LINKED LIST IMPLEMENTATION ( unsorted.cpp )
#include “itemtype.h”
UnsortedType::UnsortedType ( ) // constructor
// Pre: None.
// Post: List is empty.
{
length = 0;
listData = NULL;
}
55
ItemType UnsortedType::GetItem( ItemType item, bool& found )
// Pre: Key member of item is initialized.
// Post: If found, item’s key matches an element’s key in the list
// a copy of that element is returned; otherwise,
// origianl item is returned.
{ bool moreToSearch;
NodeType<ItemType>* location;
location = listData;
found = false ;
moreToSearch = ( location != NULL );
while ( moreToSearch && !found )
{ if ( item == location->info ) // match here
{ found = true;
item = location->info;
}
else // advance pointer
{ location = location->next;
moreToSearch = ( location != NULL );
}
}
return item;
}
56
void UnsortedType::PutItem ( ItemType item )
// Pre: list is not full and item is not in list.
// Post: item is in the list; length has been incremented.
{
NodeType<ItemType>* location;
// obtain and fill a node
location = new NodeType<ItemType>;
location->info = item;
location->next = listData;
listData = location;
length++;
}
57
Inserting ‘B’ into an Unsorted List
Private data:
length 3
currentPos ?
location = new NodeType;
item ‘B’
location
Private data:
length 3
location ‘B’
Private data:
length 3
currentPos ?
item ‘B’ location->next = listData ;
location ‘B’
Private data:
length 3
currentPos ?
item
listData = location ;
‘B’
location ‘B’
Private data:
length 3
currentPos ?
item
length++ ;
‘B’
location ‘B’
Private data:
length 4
currentPos ?