Lesson 2
Lesson 2
Figure 3.4 Deleting an item in an unsorted list (a) Original list (b) Deleting Judy
(c) Deleting Bobby (move up) (d) Deleting Bobby (swap)
Implementation: Iterators
l The field currentPos indicates the current
position of iteration in the list
l GetNextItem increments currentPos and
returns the item at that position
l ResetList must set currentPos to the first
item’s predecessor
- For arrays, that’s -1
Unsorted List: Test Plan
l Preconditions and postconditions determine the
black-box tests
l Code of functions determines clear-box tests
l We make sure the tests touch upon edge
cases, such as the first and last items in the list,
or a list with only one item
Pointer Types: Logical Level
l A pointer variable contains the memory
address of another variable
l They are used for indirect addressing of data
and for dynamic allocation of memory
l Pointers are declared using an asterisk:
int* intPointer;
Pointer Operators
l The prefix ampersand (&) or “address of”
operator returns the address of a variable:
- int alpha = 10;
- intPointer = α
- intPointer now contains alpha’s memory address
l The asterisk dereference operator (*) denotes
the variable to which a pointer points:
- *intPointer = 25; // alpha is now 25
Dynamic Allocation
l Dynamic Allocation: Allocation of memory
space for a variable at run time, as opposed to
static allocation at compile time
l Dynamic allocation creates variables on the
heap (or free store), a section of memory
reserved for dynamic allocation
Dynamic Allocation (cont.)
l Dynamic allocation uses the keyword new:
- int* ptr = new int;
l new returns a pointer to the newly allocated int
on the heap, and the value can only be
accessed via this pointer
l Pointers can point to nothing using NULL
l If no memory is available on the heap, new
returns NULL
Memory Leak
l A memory leak is the loss of available memory
space that occurs when some dynamically
allocated memory is never deallocated
l Dynamically allocated memory that can’t be
accessed is called garbage
Memory Leak Example
float* money = new float;
*money = 33.46;
float* myMoney = new float;
Memory Leak Example (cont.)
This copies the value pointed to by money into the
memory pointed to by myMoney:
*myMoney = *money;
Memory Leak Example (cont.)
This makes myMoney point to the same address
that money points to:
myMoney = money;
Memory Leak Example (cont.)
The memory cell originally used by myMoney is
now inaccessible. Since there’s no way to collect
the garbage, it is a small memory leak.
The Delete Operator
l The delete operator deallocates the memory
pointed to by the argument
l Using delete myMoney would safely clean up
the memory allocated to myMoney
l C++ requires manual memory management
Pointers: Application Level
l The name of an array variable when used
without brackets is a pointer constant
l Pointers can be used with constant types,
allowing objects to link to each other
l If myPtr is a pointer to an object, the fields of
the object are accessed using the arrow
operator: myPtr->field
Pointers: Implementation Level
l Pointers and dynamic memory are thankfully
handled entirely by the operating system,
compiler, and run-time system
l Except you must remember to delete
dynamically allocated memory when you are
finished with it!
UnsortedType as a Linked List
l Linked List: A collection of nodes that are
linked together in a chain using pointers
l Node: The basic component of a linked list;
stores data and a pointer to the next node
l Nodes are created when needed using
dynamically allocated memory
l The last node in the list has a NULL pointer
Nodes
Figure 3.12 The second PutItem operation (a) new(location) (b) info(location)
<- newElement (c) Make next (location) point to List's tope node (d) Make List
point to the new node
Length and Linked Lists
l For array-based lists, the length field must be
present in order to define the extent of the list
within the array
l Linked lists don’t have this restriction
l Instead of a field, GetLength could walk the list
and count the number of nodes
Iterators
l The array-based implementation used
currentPos, an array index
l Linked lists use a pointer to a node instead
l GetNextItem advances it: currentPos->next
l ResetList sets it to NULL
- GetNextItem checks if it’s NULL and sets it to the
first item of the list
PutItem
1) Create a new node
2) Set the node’s info to the input data
3) Set the node’s next pointer to the listData, the
first item in the list
4) Set listData to point to the new node
Figure 3.17 Retrieving an item in an unsorted linked list (a) Get Kit (b) Get Lila
DeleteItem
l Deleting an item requires updating the pointer
of its predecessor
l Algorithm is the same, but search looks at the
item of location->next in order to have access to
the predecessor (location)
DeleteItem (cont.)
Figure 3.18 Deleting an interior node and deleting the first node (a) Delete Lila
(b) Delete Kate
Lifetime of a Variable
l Lifetime: The time during execution that a
variable has memory assigned to it
l Global variable: The entire execution of a
program
l Local Variable: The execution of the block it is
in
l Dynamically allocated variable: From when it is
allocated to when it is deallocated
l The static keyword allows local variables to live
outside the time of their blocks
Class Destructors
l An object is deallocated when it leaves scope,
but any data it points to is not – memory leak!
l A class destructor is a method that is implicitly
invoked when an object leave scope
l The linked list destructor (~UnsortedList) must
clean up the object’s memory by deallocating all
the nodes in the list
Comparing Implementations
l The array-based list allocates enough memory
for the max list size, no matter how many items
are actually in the list
l The linked list-based list only uses enough
memory for the items in the list
l Both have operations that are O(1)
l Linked list’s MakeEmpty is O(N)
Comparing Implementations (cont.)