• Nie Znaleziono Wyników

Programming and Data Structures Programming and Data Structures

N/A
N/A
Protected

Academic year: 2021

Share "Programming and Data Structures Programming and Data Structures"

Copied!
23
0
0

Pełen tekst

(1)

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>

(2)

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)

(3)

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)

(4)

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

(5)

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

(6)

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)

(7)

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)

(8)

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

(9)

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

(10)

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

(11)

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)

(12)

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)

(13)

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

(14)

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

(15)

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

(16)

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

(17)

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)

(18)

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

(19)

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)

(20)

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

(21)

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

(22)

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

(23)

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

Cytaty

Powiązane dokumenty

● destructor, copy constructor and assignment operator (as for bounded) insertion/removal

(3) set a dedicated pointer (killer) to the last node using the address in the next pointer of the node pointed to by tmp (or the head pointer if tmp is empty, or the tail pointer

● during instantiation of a template with a specific type, the compiler replaces each instance of a parametric type with the specified type and compiles the data structure

(1) set the pred pointer on the node preceding the deleted node, and succ on the node to be removed (if there is no node we are looking for, throw an exception or abort the

(1) by comparing values, go from root to children and set the pred pointer on the parent node to be removed, and succ on the node to be removed (if there is no node we are looking

(1) invoke two auxiliary pointers set on the adjacent nodes pred and succ (assume that the pointers next in the preceding nodes pointed to by succ are already inverted, succ is

● input/output streams: iostream, fstream, sstream, iomanip. ● string

● sequence container – double-ended queue, dynamic array using non- contiguous storage, a list of arrays. ● fast addition/removal at the beginning and at the end of