Title: CS1001%20Lecture%2026
1CS1001 Lecture 26
- Static vs Dynamic Data Structures
- Array vs Link List
- Pointers
- Link List Operations
- Example
2Static vs Dynamic
- Static Data Structure -- structures whose sizes
are fixed once memory is allocated, e.g., arrays - type, DIMENSION (lu) list_of _array_names
- type, DIMENSION () , ALLOCATABLE list
- ALLOCATE (list, STAT status_variable)
- DEALLOCATE (list, STAT status_variable)
- Dynamic Data Structure -- expands and contracts
as needed during execution - collection of elements called nodes
- e.g., Link list
3Arrays
- PROs
- Used for list of data that can be naturally
organized into tables - Easy and fast to access each element by indices
- Easy and fast to operate on corresponding data
points - CONs
- Fixed size
- Hard to shift elements
1, 3, 5, 8,10,, 80, to insert 2 1, 2, 3, 5,
8, 10, , 80, ...
1, 2, 3, 5, 8, 10,, 80, to delete 3 1, 2, 5,
8, 10, , 80, ...
4Link List
- Link list -- collection of elements called nodes
and each node consists of data and link (or
pointer)
data
data
data
list
Brown ptr
CMU ptr
WPI null
5Adding to the linked list
Data next
data next
data next
list
Brown
CMU
WPI null
data
new
Prev_ptr
RICE
data next
data next
data next
list
Brown
CMU
WPI null
data
new
Prev_ptr
RICE
data next
data next
data next
list
Brown
CMU
WPI null
data
new
Prev_ptr
RICE
6Delete a node from a linked list
data next
data next
data next
list
Brown
CMU
WPI null
data
Prev_ptr
To_delete
RICE
data next
data next
data next
list
Brown
CMU
WPI null
data
To_delete
Prev_ptr
RICE
data next
data next
null
list
Brown
WPI null
To_delete
data
Prev_ptr
RICE
7Linked List Elements
- Pointer to the List
- Node -- defined as a derived type
- e.g., TYPE School_node
- CHARACTER(8) Name
- TYPE (School_node) POINTER next
- END TYPE School_node
- e.g., TYPE School
- CHARACTER(8) Name
- REAL Cost
- END TYPE School
- TYPE School_node1
- TYPE School data
- TYPE (School_node1) POINTER next
- END TYPE School_node1
-
School_node
Name next
School_node1
data next
8Pointers
- Declaration
- Type, attribute_list, POINTER pointer_variable
- e.g., CHARACTER(8), POINTER StringPtr
- TYPE (School_node1), POINTER NextPtr
- ALLOCATE
- Used to acquire memory locations to associate
with the pointer variable during execution - e.g., ALLOCATE(StringPtr)
- ALLOCATE (NextPtr)
Memory for data of type School_node1
Name -- Character(8) Cost -- Real Next -- pointer
to type School_node1
StringPtr
NextPtr
Character(8)
9Pointer
- Pointer variables
- undefined
- initially undefined
- associated -- when a pointer points to a target
- intrinsic function ASSOCIATED to test whether a
pointer variable is associated with a target.
E.g., ASSOCIATED (pointer) returns .TRUE. if
association exists, returns .FALSE. otherwise. - null or disassociated
- NULLIFY (list_of_pointers) change pointer
variables to null - the association between that pointer and the
target memory location is broken - DEALLOCATE statement is used to free the target
memory
10Pointer Assignment
- Form pointer1 gt pointer2
- if pointer1 and pointer2 have the same type
Pointer1 Pointer2
Need another pointer ?
Pointer1 gt Pointer2
Pointer1 Pointer2
ASSOCIATED ( Pointer1, Pointer2) .TRUE. If
pointer1 and pointer2 point to the same
target. .FALSE. Otherwise.
11Pointers in expressions
- CHARACTER (8), POINTER School
- TYPE School_node
- CHARACTER(8) Name
- TYPE (School_node) POINTER next
- END TYPE School_node
- TYPE (School_node), POINTER list
- ALLOCATE (School)
- ALLOCATE (list)
- School Stanford
- Print , School
School
CHARACTER(8)
list
CHARACTER(8) Pointer to School_node
School
Stanford
12Pointers in expressions
- CHARACTER (8), POINTER School1, School2
- ALLOCATE (School1)
- ALLOCATE (School2)
- School1 Stanford
- School2 Cornell
- School1 School2
- School1 gt School2
Stanford
School1
Cornell
School2
Cornell
School1
Cornell
School2
Stanford
School1
Cornell
School2
13Constructing a Linked List
- TYPE School_node
- CHARACTER(8) Name
- TYPE (School_node) POINTER next
- END TYPE School_node
- TYPE (School_node), POINTER list, temp
- ALLOCATE (temp)
- tempName Stanford
- NULLIFY(tempnext)
- list gt temp
- ALLOCATE(temp)
- tempName WPI
- NULLIFY(tempnext)
- listnext gt temp
CHARACTER(8) Pointer to School_node
Stanford Null
temp
list
list
Stanford Null
WPI Null
temp
temp
list
Stanford
WPI Null
14Traversing a linked list
WPI
Cornell Null
list
Stanford
Curr_ptr
Curr_ptr gt list Curr_ptr gt Curr_ptrnext Curr_pt
r gt Curr_ptrnext Curr_ptr gt Curr_ptrnext
Curr_ptr
Curr_ptr
Curr_ptr is null -- end of list
Curr_ptr gt list DO IF (.NOT. ASSOCIATED
(Curr_ptr) EXIT PRINT , Curr_ptrname
Curr_ptr gt Curr_ptrnext END DO
15Example
- Get a list of TCP/IP address from a file
- Use linked list to keep track of how many times
the addresses was used - traverse the list and display each TCP/IP
address and its counts