Linked List
Linked List
Department: Mathematics
LAOUICI.Z
1
Linked List : Dynamic variable
Syntax
Function Allocate:
name_pointer Allocate() ;
Procedure Free :
Free(name_pointer) ;
3
Linked List : Creation of a dynamic variable of
integer type
Example 1
5
(*P) = 5 ;
@10
Free(P);
4
Linked List : Creation of a dynamic variable of
vector type
Example 2
Type Vec = array [20] real;
P
Type PVec = pointer Vec;
@2
Variable P : PVec ;
x: real; 6 4
P Allocate() ;
@2
(*P) 6 ;
*(P+2) 4 ;
Free(P);
// Now the array no longer exists, we no longer have the right to manipulate5 *p
Linked List : Creation of a
Example 3 structure
We manipulate the structure with
Type Date = structure the primitives Aff_A, Aff_M, and Aff_J
J, M, A : int ; end through the pointer p
Type pDate = pointer Date;
Variable P : pDate ;
P
P Allocate() ; @20
20 J
Aff_A(p, 2004); 1 M
Aff_M(p, 1); 2004 A
Aff_J(p, 20);
@20
Free(P); // The variable no longer exists. 6
Linked List
7
Linked List
8
Linked List : Data structures
L
@M1 X E1 @M2 E2 @M3 E3
X
@M1 @M2 @M3
VAL Field Next Field
L
E1 E2 E3
X
VAL Next
Field Field
Type Node = structure // Node is not a
VAL : typeqlq ; list
Next : * Node;
end ;
Type List = * Node ; // A pointer to a node of the list
Linked List : linked linear lists
Model
Free(P); P
@
val Next
@
Delete the node pointed to by P (P is not deleted)
11
Linked List : linked linear lists
Model
P Allocate() ;
P
Free(P);
val Next
6
Procedure Aff_Val:
Aff_val(p, 6);
Affect a value to the Val field of the
node pointed by P.
12
Linked List : linked linear lists
Model
P Allocate(); P
Q
Free(P);
val Next val Next val Next
Aff_Val:
Aff_val(P, 6);
6 X
Procedure Aff_Next:
13
Linked List : linked linear lists
Model
P Allocate() ;
P Q
Free(P);
val Next val Next val Next
Aff_val(P, 6);
Aff_Next(P, Q);
6 5 X
Function Val():
X val(P);
Return the content of the field Val of
Aff_val(Q, val(P)-1); the node pointed by P
15
Linked List : linked linear lists
Model
P Allocate() ;
P
Free(P); Q
16