Professional Documents
Culture Documents
List Definitions: Linear Relationship Each Element Except
List Definitions: Linear Relationship Each Element Except
1 2
3 4
Data from 3 different levels
Remember ???
• Application (or user) level: modeling real-life • Constructor
data in a specific context.
5 6
#include “ItemType.h”
Class Constructor
RelationType ItemType::ComparedTo(ItemType otherItem)
const
{ A special member function of a class
if ( value < otherItem.value )
return LESS; that is implicitly invoked when a class
else if ( value > otherItem.value )
return GREATER; object is defined.
else return EQUAL;
}
void ItemType::Print ( ofstream& out) const
{
out << value << endl;
}
void ItemType::Initialize ( int number )
{
value = number;
}
13 14
Operations (provided by Unsorted List ADT): Postcondition Returns the number of elements in list.
Function Initializes list to empty state. Function Retrieves list element whose key matches item's key (if
present).
Precondition
Precondition List has been initialized. Key member of item is initialized.
Postcondition List is empty.
Postcondition If there is an element someItem whose key matches item's
Boolean IsFull key, then found = true and item is a copy of someItem;
Function Determines whether list is full. otherwise found = false and item is unchanged. List is
Precondition List has been initialized. unchanged.
Postcondition Returns true if list is full and false otherwise. PutItem (ItemType item)
Function Adds item to list.
Precondition List has been initialized. List is not full. item is not in list.
17 18
Postcondition item is in list.
} 21 22
23 24
Getting Ivan from an Unsorted List
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
// and a copy of that element is returned;
// otherwise, input item is returned.
{ length 4 moreToSearch: true
bool moreToSearch;
found: false
int location = 0;
O(N) info [0] Maxwell
found = false; location: 0
moreToSearch = ( location < length ); [1] Bradley
while ( moreToSearch && !found )
{ [2] Asad
switch ( item.ComparedTo( info[location] ) )
{ case LESS : [3] Hsing
case GREATER : location++; .
moreToSearch = ( location < length ); .
break;
case EQUAL : found = true;
[MAX_ITEMS-1]
item = info[ location ];
}
}
return item; 25
} 25 26
27 28
Getting Ivan from an Unsorted List Getting Ivan from an Unsorted List
length 4 moreToSearch: true
length 4 moreToSearch: false
found: false
info [0] Maxwell found: false
location: 3 info [0] Maxwell
location: 4
[1] Bradley
[1] Bradley
[2] Asad
[2] Asad
[3] Hsing
. [3] Hsing
. .
. .
[MAX_ITEMS-1] .
[MAX_ITEMS-1]
29 30
31 32
Deleting Bradley from an Unsorted List
Deleting Bradley from an Unsorted List
length 4 length 4
location: 1
info [0] Maxwell location: 1 info [0] Maxwell
33 34
35 36
UML Diagrams ItemType
What is a pointer variable?
+RelationType: {LESS, GREATER, EQUAL}
(Array) -value: int
• A pointer variable is a variable whose value is the address of a
+ComparedTo(otherItem:ItemType): RelationType
+Print(ofstream&): void
location in memory.
+Initialize(number:int): void
• To declare a pointer variable, you must specify the type of
value that the pointer will point to. For example,
+UnsortedType()
+MakeEmpty(): void
+GetLength(): int
+IsFull(): bool
+GetItem(item:ItemType, found: bool&): ItemType
+PutItem(item:ItemType): void
+DeleteItem(item:ItemType): void
+ResetList(): void
+GetNextItem(): ItemType
37 38
2000
char* ptr;
Simple
Structured ptr
ptr = new char;
Integral Floating
array struct union class *ptr = ‘B’;
char short int long enum std::cout << *ptr;
43 44
Dynamically Allocated Data Dynamically Allocated Data
45 46
47 48
The NULL Pointer Using operator delete
There is a pointer constant called NULL
available in cstddef.
NULL is not a memory address; it means that The object or array currently pointed to by the
the pointer variable points to nothing. pointer is deallocated, and the pointer is
considered unassigned. The memory is
It is an error to dereference a pointer whose value returned to the free store.
is NULL. It is the programmer’s job to check for
this. Square brackets are used with delete to deallocate
while (ptr != NULL) a dynamically allocated array of classes.
{
. . . // ok to use *ptr here
}
49 50
51 52
Causing a Memory Leak A Dangling Pointer
int* ptr = new int; • occurs when two pointers point to the same object and delete
8
is applied to one of them.
*ptr = 8; ptr
int* ptr2 = new int; int* ptr = new int;
-5
*ptr2 = -5; *ptr = 8;
ptr2 int* ptr2 = new int; 8
*ptr2 = -5;
ptr = ptr2; // here the 8 becomes ptr = ptr2;
ptr
inaccessible
8 -5
ptr ptr2
-5
FOR EXAMPLE,
ptr2
53 54
struct NodeType {
Transformers ItemType info;
NodeType* next;
• MakeEmpty };
change state
• PutItem class UnsortedType
• DeleteItem {
Observers public : // LINKED LIST IMPLEMENTATION
• IsFull // The public interface is the same
• GetLength observe state
private :
• GetItem // The private part is different
NodeType* listData;
int length;
Iterators NodeType* currentPos;
• ResetList process all
}; Do we have to keep a length field?
• GetNextItem Do we need an IsFull? 58
57 58
UnsortedType::UnsortedType ( ) // constructor
UnsortedType // Pre: None.
MakeEmpty
Private data: // Post: List is empty. O(1)
{
length 3 length = 0;
~UnsortedType listData = NULL;
listData ‘X’ ‘C’ ‘L’ currentPos = NULL;
GetItem
}
PutItem currentPos ?
int UnsortedType::GetLength( ) const
DeleteItem // Post: Function value = number of items in the list.
.
. {
. return length;
GetNextItem
} O(1)
60
59 60
ItemType UnsortedType::GetItem( ItemType item, bool& found )
// Pre: Key member of item is initialized.
void UnsortedType::PutItem ( ItemType item )
// Post: If found, item’s key matches an element’s key in the list
// a copy of that element is returned; otherwise, // Pre: list is not full and item is not in list.
// origianl item is returned. // Post: item is in the list; length has been incremented.
{ bool moreToSearch;
NodeType* location; {
location = listData;
found = false ;
O(N) NodeType* location; O(1)
// obtain and fill a node
moreToSearch = ( location != NULL );
while ( moreToSearch && !found ) location = new NodeType;
{ switch( item.ComparedTo( location->info ) location->info = item;
{ // match
case LESS: location->next = listData;
case GREATER: // advance pointer listData = location;
location = location->next;
moreToSearch = ( location != NULL );
length++;
break; }
case EQUAL:
found = true;
item = location->info;
}
61 62
}
return
61 item; 62
}
location
currentPos ? currentPos ?
63 64
item ‘B’ location->info = item ; item ‘B’ location->next = listData ;
currentPos ? currentPos ?
65 66
item
listData = location ; item
length++ ;
‘B’ ‘B’
currentPos ? currentPos ?
67 68
void UnsortedType::DeleteItem( ItemType item )
// Pre: item’s key has been initialized.
// An element of the list has a key that matches item’s
// Post: No element of the list has a key that matches item’s
{
item ‘M’ location = listData ;
NodeType* location = listData;
NodeType* tempLocation;
listData ‘X’ ‘C’ ‘M’ ‘L’ listData ‘X’ ‘C’ ‘M’ ‘L’
currentPos ? currentPos ?
71 72
item ‘M’ location = location->next; item ‘M’ location->next->info = ‘M’;
listData ‘X’ ‘C’ ‘M’ ‘L’ listData ‘X’ ‘C’ ‘M’ ‘L’
currentPos ? currentPos ?
73 74
listData ‘X’ ‘C’ ‘M’ ‘L’ listData ‘X’ ‘C’ ‘M’ ‘L’
currentPos ? currentPos ?
75 76
item ‘M’ delete tempLocation; item ‘M’ length--;
currentPos ? currentPos ?
77 78
+ComparedTo(otherItem:ItemType): RelationType
+Print(ofstream&): void
+Initialize(number:int): void
Operation Array LinkedList
UnsortedType O(1) O(1)
MakeEmpty O(1) O(n)
UnsortedType
GetLength O(1) O(1)
-listData: NodeType*
-length: int IsFull O(1) O(1)
-currentPos: NodeType*
GetItem O(n) O(n)
+UnsortedType()
+MakeEmpty(): void PutItem O(1) O(1)
+GetLength(): int
+IsFull(): bool
DeleteItem O(n) O(n)
+GetItem(item:ItemType, found: bool&): ItemType
+PutItem(item:ItemType): void
ResetList O(1) O(1)
+DeleteItem(item:ItemType): void GetNextItem O(1) O(1)
+ResetList(): void
+GetNextItem(): ItemType
81 82