Programming and Data Structures Programming and Data Structures
Lecture 7 Lecture 7
Dr Piotr Cybula <piotr.cybula@wmii.uni.lodz.pl>
Dr Piotr Cybula <piotr.cybula@wmii.uni.lodz.pl>
Insertion Insertion
A next
B next head C
(1) create two auxiliary pointers: set pred to an empty address, and succ on to the address of the first node (head)
Insertion Insertion
A next
B next head C
pred succ
(1) create two auxiliary pointers: set pred to an empty address, and succ on to the address of the first node (head)
Insertion Insertion
A next
B next head C
pred succ
(1) create two auxiliary pointers: set pred to an empty address, and succ on to the address of the first node (head)
(2) pass the auxiliary pointers towards the end of the list so that the pred pointer stops at the node we insert after and succ at the node we put before
Insertion Insertion
A next
B next head C
pred succ
(1) create two auxiliary pointers: set pred to an empty address, and succ on to the address of the first node (head)
(2) pass the auxiliary pointers towards the end of the list so that the pred pointer stops at the node we insert after and succ at the node we put before
Insertion Insertion
A next
B next head C
pred succ
(1) create two auxiliary pointers: set pred to an empty address, and succ on to the address of the first node (head)
(2) pass the auxiliary pointers towards the end of the list so that the pred pointer stops at the node we insert after and succ at the node we put before
(3) create a new node (creator) with the given value and the value of succ as the next address (next)
Insertion Insertion
A next
B next head C
V creator next
succ pred
(1) create two auxiliary pointers: set pred to an empty address, and succ on to the address of the first node (head)
(2) pass the auxiliary pointers towards the end of the list so that the pred pointer stops at the node we insert after and succ at the node we put before
(3) create a new node (creator) with the given value and the value of succ as the next address (next)
Insertion Insertion
A next
B next head C
V creator next
succ pred
(1) create two auxiliary pointers: set pred to an empty address, and succ on to the address of the first node (head)
(2) pass the auxiliary pointers towards the end of the list so that the pred pointer stops at the node we insert after and succ at the node we put before
(3) create a new node (creator) with the given value and the value of succ as the next address (next)
(4) set the next pointer in the node pointed to by pred (or the head pointer when pred is empty) to the new node
Insertion Insertion
A next
B next head C
V creator next
succ pred
(1) create two auxiliary pointers: set pred to an empty address, and succ on to the address of the first node (head)
(2) pass the auxiliary pointers towards the end of the list so that the pred pointer stops at the node we insert after and succ at the node we put before
(3) create a new node (creator) with the given value and the value of succ as the next address (next)
(4) set the next pointer in the node pointed to by pred (or the head pointer when pred is empty) to the new node
Insertion Insertion
A next
B next head C
V next
(1) create two auxiliary pointers: set pred to an empty address, and succ on to the address of the first node (head)
(2) pass the auxiliary pointers towards the end of the list so that the pred pointer stops at the node we insert after and succ at the node we put before
(3) create a new node (creator) with the given value and the value of succ as the next address (next)
(4) set the next pointer in the node pointed to by pred (or the head pointer when pred is empty) to the new node
Removal Removal
A next
B C
head
next
(1) create two auxiliary pointers: set pred to an empty address, and succ on to the address of the first node (head) and pass the auxiliary pointers towards the end of the list so that the pred pointer stops at the node preceding the deleted node, and succ at the node to be removed (if there is no node we are looking for, throw an exception or abort the operation)
Removal Removal
A next
B C
head
next
pred succ
(1) create two auxiliary pointers: set pred to an empty address, and succ on to the address of the first node (head) and pass the auxiliary pointers towards the end of the list so that the pred pointer stops at the node preceding the deleted node, and succ at the node to be removed (if there is no node we are looking for, throw an exception or abort the operation)
Removal Removal
A next
B C
head
next
pred succ
(1) create two auxiliary pointers: set pred to an empty address, and succ on to the address of the first node (head) and pass the auxiliary pointers towards the end of the list so that the pred pointer stops at the node preceding the deleted node, and succ at the node to be removed (if there is no node we are looking for, throw an exception or abort the operation)
(2) set a dedicated (killer) to the node pointed to by succ and set the succ pointer to the next node
Removal Removal
A next
B C
head
next
pred killer succ
(1) create two auxiliary pointers: set pred to an empty address, and succ on to the address of the first node (head) and pass the auxiliary pointers towards the end of the list so that the pred pointer stops at the node preceding the deleted node, and succ at the node to be removed (if there is no node we are looking for, throw an exception or abort the operation)
(2) set a dedicated (killer) to the node pointed to by succ and set the succ pointer to the next node
Removal Removal
A next
B C
head
next
pred killer succ
(1) create two auxiliary pointers: set pred to an empty address, and succ on to the address of the first node (head) and pass the auxiliary pointers towards the end of the list so that the pred pointer stops at the node preceding the deleted node, and succ at the node to be removed (if there is no node we are looking for, throw an exception or abort the operation)
(2) set a dedicated (killer) to the node pointed to by succ and set the succ pointer to the next node
(3) set the next pointer in the node pointed to by pred (or the head pointer when pred is empty) to an address stored in succ
Removal Removal
A next
B C
head
next
pred killer succ
(1) create two auxiliary pointers: set pred to an empty address, and succ on to the address of the first node (head) and pass the auxiliary pointers towards the end of the list so that the pred pointer stops at the node preceding the deleted node, and succ at the node to be removed (if there is no node we are looking for, throw an exception or abort the operation)
(2) set a dedicated (killer) to the node pointed to by succ and set the succ pointer to the next node
(3) set the next pointer in the node pointed to by pred (or the head pointer when pred is empty) to an address stored in succ
Removal Removal
A next
B C
head
next
pred killer succ
(1) create two auxiliary pointers: set pred to an empty address, and succ on to the address of the first node (head) and pass the auxiliary pointers towards the end of the list so that the pred pointer stops at the node preceding the deleted node, and succ at the node to be removed (if there is no node we are looking for, throw an exception or abort the operation)
(2) set a dedicated (killer) to the node pointed to by succ and set the succ pointer to the next node
(3) set the next pointer in the node pointed to by pred (or the head pointer when pred is empty) to an address stored in succ
(4) release the node pointed by the dedicated pointer (killer)
Removal Removal
(1) create two auxiliary pointers: set pred to an empty address, and succ on to the address of the first node (head) and pass the auxiliary pointers towards the end of the list so that the pred pointer stops at the node preceding the deleted node, and succ at the node to be removed (if there is no node we are looking for, throw an exception or abort the operation)
(2) set a dedicated (killer) to the node pointed to by succ and set the succ pointer to the next node
(3) set the next pointer in the node pointed to by pred (or the head pointer when pred is empty) to an address stored in succ
(4) release the node pointed by the dedicated pointer (killer)
A next
C
head pred killer succ
Removal Removal
A next
C head
(1) create two auxiliary pointers: set pred to an empty address, and succ on to the address of the first node (head) and pass the auxiliary pointers towards the end of the list so that the pred pointer stops at the node preceding the deleted node, and succ at the node to be removed (if there is no node we are looking for, throw an exception or abort the operation)
(2) set a dedicated (killer) to the node pointed to by succ and set the succ pointer to the next node
(3) set the next pointer in the node pointed to by pred (or the head pointer when pred is empty) to an address stored in succ
(4) release the node pointed by the dedicated pointer (killer)
Sorted list Sorted list
● abstract data type that allows you to arrange the inserted elements on an ongoing basis (implements insertion sort)
● requires the presence of a comparison operator (operator< lub operator>) for the ordered elements
● array implementation requires moving elements in the array (low efficiency due to the necessity of frequent data copying or array reallocation)
● list implementation eliminates this problem (a new element is inserted into the list in the appropriate place without changing the location in the memory of the current ones)
A next
D head
B next
Sorted list - insertion Sorted list - insertion
● wandering with the auxiliary pointers pred and succ from the beginning of the list, we stop the succ pointer on the first node with a value greater than or equal to the inserted (in ascending order) or less than or equal to the inserted (in descending order)
● an empty pred pointer means inserting at the beginning
● an empty succ pointer means inserting at the end
A next
C next
D head
B next
pred succ
Sorted list - removal Sorted list - removal
A next
C next
D head
B next
pred succ
● wandering with the auxiliary pointers pred and succ from the beginning of the list, we stop the succ pointer on the first node with a value greater than or equal to the removed omitting values that are smaller (in ascending order) or greater (in descending order)
● if the succ pointer is empty or points to a node containing a different value, then the value you are looking for is not in the list
Sorted list - removal Sorted list - removal
● wandering with the auxiliary pointers pred and succ from the beginning of the list, we stop the succ pointer on the first node with a value greater than or equal to the removed omitting values that are smaller (in ascending order) or greater (in descending order)
● if the succ pointer is empty or points to a node containing a different value, then the value you are looking for is not in the list
● after deleting an item, the succ pointer points to the next node (or is empty) and the list looks as if the item being deleted has never been there (this allows the deletion to continue in the case of a sequence of equal values)
A next
D head
B next
pred succ