Lecture 4 List ADT Array
Lecture 4 List ADT Array
1
Learning Outcomes
Insert
Class data
members The stack segment
(private):
of memory may not
be the best place to
FriendsBook Search
Etc...
(client code) hold (potentially) large
array of Profile objects
amount of data
(Profile objects) … Hum!
public: public:
... // Destructor
heap delete [] elements;
insert(...) ~List();
}; ... in List.cpp
insert(...)
}; if (elementCount == 0)
elements = new Profile[SIZE];
static if (elements == nullptr) // error!
else // insert new element
8 code
Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University
Step 3 – Array-based implementation of List ADT
When Always implement methods optimizing their time efficient
implementing,
we need to
consider … Differentiate between stack-allocated and heap-allocated arrays
When to use stack-allocated memory?
When to use heap-allocated memory?
Head
Characteristics:
Adv:
Flexible/unbounded size: it grows or shrinks as needed
Operations on linked list do not require the shifting of elements
Disadv: Sequential access
Elements must be accessed in some specific order dictated
by the links
10
3 7 5 8
4
elementCount
6 pseudocode
... prepend(int newElement) // prepend into a List newNode
1. Node *newNode = new Node(newElement);
2. if (newNode != nullptr)
Order is 3. newNode->next = head; // Whether Head NULL or not
important 4. head = newNode;
12 5. elementCount++;
0
13 elementCount
Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University
REVIEW Traverse a linked list
head
3 7 5 8
4
elementCount
pseudocode
Local // Anchor head of linked list
variable: current 1. if ( head != nullptr )
2. Node* current = head;
3. while (current->next != nullptr)
4. current = current->next;
14
head
3 7 5 8
4
elementCount
pseudocode
// Traverse linked list
1. if ( head != nullptr )
2. while (head->next != nullptr)
3. head = head->next;
15
3 7 5
3 pseudocode
elementCount ... append(int newElement)
1. Node *newNode = new Node(newElement);
2. if (newNode != nullptr)
3. if (head == nullptr)
newNode
4. head = newNode;
Local else
variable: current // Move to the end of the list
5. Node* current = head; // Anchor
6. while (current->next != nullptr)
7. current = current->next;
8. current->next = newNode;
16
9. elementCount++;
Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University
A word about inserting an element into a linked list
@ specific location
When linked list is used as a data structure (CDT) for a position-
oriented data collection ADT class like a List, we can indicate at
which position we would like to insert an element
position is a parameter of the insert method
Class invariant for this OR
List class: the List is When linked list is used as a data structure (CDT) for a value-
always sorted by … oriented data collection ADT class like a List (which is kept
e.g. ascending or descending
alphabetical/numerical sort
sorted), in order to keep it sorted, we insert the element in sort Algorithm 1
order of search key … order into the List using a search key (i.e., an element’s attribute)
depending on the problem we
are solving.
Alternatively, we could first prepend the element into the List (this
is time efficient, i.e., O(1)), then we sort the List (sorting algorithms
can be O(n2) or O(n log(n)). As you can see, this 2nd way of Algorithm 2
“keeping the List sorted when we insert” is not time efficient!
data
List kept in descending 36 89 53 27 12
sort order of data: head
17
next Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University
REVIEW Remove an element from a linked list
3 7 5 8
4
elementCount
pseudocode
... removeAtFront( )
nodeToRemove 1. if (head != nullptr)
2. Node * nodeToRemove = head;
3. head = head->next;
// Return node to the system
4. nodeToRemove->next = nullptr;
5. delete nodeToRemove;
18 6. nodeToRemove = nullptr;
7. elementCount--;
Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University
Remove an element from a linked list
3 7 5 8
4
elementCount
pseudocode
current ... removeAtEnd( )
1. if (head != nullptr)
// Move to the end of the list
2. Node * current = head; // Anchor
3. while (current->next != nullptr)
4. current = current->next;
19 // Then what???
20